Skip to content

Commit

Permalink
Add docker, lint and gitlab-ci
Browse files Browse the repository at this point in the history
  • Loading branch information
alenpokos committed Jul 10, 2018
1 parent d9c9ca7 commit fe3d035
Show file tree
Hide file tree
Showing 22 changed files with 280 additions and 82 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@
### Bundle ###
/vendor/
composer.lock
.php_cs.cache
26 changes: 26 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
stages:
- test
- lint

before_script:
- source bin/gitlab/prepare.sh

after_script:
- source bin/gitlab/stop.sh

test:
stage: test
tags:
- shell
- docker-compose
script:
- rm -f composer.lock
- bin/dev/composer install --no-interaction --no-ansi --prefer-dist
- bin/dev/php vendor/bin/phpunit --debug --colors=never --coverage-text=php://stdout --coverage-html=logs/coverage
lint:
stage: lint
tags:
- shell
- docker-compose
script:
- bin/dev/phpcsfixer fix --dry-run --using-cache=no .
38 changes: 38 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use PhpCsFixer\Fixer\Import\OrderedImportsFixer;

$finder = PhpCsFixer\Finder::create()
->name('console')
;

return PhpCsFixer\Config::create()
->setUsingCache(true)
->setRules([
'@PSR2' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
'@DoctrineAnnotation' => true,
'concat_space' => ['spacing' => 'one'],
'array_syntax' => ['syntax' => 'short'],
'list_syntax' => ['syntax' => 'short'],
'phpdoc_align' => '',
'phpdoc_no_empty_return' => false,
'phpdoc_summary' => false,
'ordered_imports' => [
'sortAlgorithm' => OrderedImportsFixer::SORT_ALPHA,
'importsOrder' => [
OrderedImportsFixer::IMPORT_TYPE_CONST,
OrderedImportsFixer::IMPORT_TYPE_FUNCTION,
OrderedImportsFixer::IMPORT_TYPE_CLASS,
],
],
'class_definition' => ['multiLineExtendsEachSingleLine' => true],
'ternary_to_null_coalescing' => true,
'yoda_style' => true,
'compact_nullable_typehint' => true,
'visibility_required' => true,
])
->setRiskyAllowed(true)
->setFinder($finder)
;
9 changes: 4 additions & 5 deletions DependencyInjection/TrikoderManifestAssetExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace Trikoder\ManifestAssetBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* This is the class that loads and manages your bundle configuration.
*
* @link http://symfony.com/doc/current/cookbook/bundles/extension.html
* @see http://symfony.com/doc/current/cookbook/bundles/extension.html
*/
class TrikoderManifestAssetExtension extends Extension
{
Expand All @@ -25,7 +24,7 @@ public function load(array $configs, ContainerBuilder $container)
$rootPath = $container->getParameter('kernel.root_dir');
$container->setParameter('trikoder_manifest_asset.web_path',
realpath($rootPath . '/../' . $config['web_dir']));
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
}
}
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ Then, you can use :
Note: Twig namespace references does not contain separator `:` like bundles. See [this link](https://symfony.com/doc/current/templating/namespaced_paths.html) for more details.


## Running tests and linter

For running tests and linter, there is a complete docker enviroment with php and all required tools.
To setup environment use:

```
bin/dev/docker-compose build
bin/dev/composer install
```

To run tests (after environment is built):

```
bin/dev/composer test
```

To run linter:

```
bin/dev/phpcsfixer fix --dry-run .
```
To automaticaly fix coding standards, just ommit `--dry-run` parameter.



## Credits

Expand Down
15 changes: 10 additions & 5 deletions Service/ManifestReaderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ class ManifestReaderService
/** @var $manifest array */
protected $manifestRegistry = [];

/** @var KernelInterface $kernel */
/** @var KernelInterface $kernel */
protected $kernel;

/** @var Twig_Loader_Filesystem $twigLoaderFilesystem */
/** @var Twig_Loader_Filesystem $twigLoaderFilesystem */
protected $twigLoaderFilesystem;

/** @var string $manifestFileName */
protected $manifestFileName;

/**
* ManifestReaderService constructor.
*
* @param KernelInterface $kernel
* @param string $manifestFileName
*/
Expand All @@ -34,6 +35,7 @@ public function __construct(KernelInterface $kernel, Twig_Loader_Filesystem $twi

/**
* @param $reference
*
* @return mixed
*/
public function getManifest($reference)
Expand All @@ -47,6 +49,7 @@ public function getManifest($reference)

/**
* @param $reference
*
* @return mixed
*/
protected function loadManifest($reference)
Expand All @@ -61,7 +64,7 @@ protected function loadManifest($reference)
}

// Twig namespace reference
else if (preg_match('/^@(\w+)/', $reference, $matches)) {
elseif (preg_match('/^@(\w+)/', $reference, $matches)) {
$manifestPath = $this->getManifestPathFromTwigNamespace($reference, $this->manifestFileName);
$assetRoot = strtolower($matches[1]) . '/';
}
Expand All @@ -77,10 +80,10 @@ protected function loadManifest($reference)
$manifest = json_decode(file_get_contents($manifestPath), true);

// check if we got contents
if (true === is_null($manifest)) {
if (null === $manifest) {
throw new InvalidArgumentException(
sprintf(
'Manifest file %1$s/%2$s does not have valid contents',
'Manifest file %1$s/%2$s does not have valid content!',
$reference,
$this->manifestFileName
)
Expand All @@ -99,6 +102,7 @@ protected function loadManifest($reference)
/**
* @param $reference
* @param $manifestFileName
*
* @return string
*/
protected function getManifestPathFromBundle($reference, $manifestFileName)
Expand All @@ -117,6 +121,7 @@ protected function getManifestPathFromBundle($reference, $manifestFileName)
/**
* @param $reference
* @param $manifestFileName
*
* @return string
*/
protected function getManifestPathFromTwigNamespace($reference, $manifestFileName)
Expand Down
6 changes: 3 additions & 3 deletions Tests/Service/ManifestReaderServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class ManifestReaderServiceTest extends TestCase
{
/**
* @var Twig_Loader_Filesystem $twigLoaderFilesystem
* @var Twig_Loader_Filesystem
*/
protected $twigLoaderFilesystem;

Expand All @@ -30,7 +30,7 @@ public function validateManifestLoadingAndFormat()
->disableOriginalConstructor()
->getMock();

$kernelMock->method('locateResource')->willReturn(realpath(__DIR__.'/test-manifest.json'));
$kernelMock->method('locateResource')->willReturn(realpath(__DIR__ . '/test-manifest.json'));

$service = new ManifestReaderService($kernelMock, $this->twigLoaderFilesystem, 'test-manifest.json');
$result = $service->getManifest('@TrikoderManifestAssetBundle');
Expand All @@ -55,7 +55,7 @@ public function validateManifestLoadingFromTwigNamespace()
->getMock();

$kernelMock->method('getRootDir')
->willReturn(realpath(__DIR__.'/../'));
->willReturn(realpath(__DIR__ . '/../'));

$service = new ManifestReaderService($kernelMock, $this->twigLoaderFilesystem, 'test-manifest.json');

Expand Down
65 changes: 33 additions & 32 deletions Tests/Twig/ManifestAssetExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
namespace Tests\Twig;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Trikoder\ManifestAssetBundle\Twig\ManifestAssetExtension;
use Symfony\Component\HttpKernel\KernelInterface;
use Trikoder\ManifestAssetBundle\Service\ManifestReaderService;
use Trikoder\ManifestAssetBundle\Twig\ManifestAssetExtension;
use Twig_Loader_Filesystem;

class ManifestAssetExtensionTest extends TestCase
{
/**
* @var KernelInterface $kernelMock
* @var KernelInterface
*/
private $kernelMock;

/**
* @var Twig_Loader_Filesystem $twigLoaderFilesystem
* @var Twig_Loader_Filesystem
*/
protected $twigLoaderFilesystem;

Expand All @@ -27,7 +27,7 @@ protected function setUp()
->disableOriginalConstructor()
->getMock();

$this->kernelMock->method('getRootDir')->willReturn(realpath(__DIR__.'/../'));
$this->kernelMock->method('getRootDir')->willReturn(realpath(__DIR__ . '/../'));
$this->twigLoaderFilesystem = new Twig_Loader_Filesystem();
$this->twigLoaderFilesystem->addPath('./Tests/Service', 'TestNamespace');
}
Expand All @@ -37,38 +37,38 @@ public function testCorrectAbsolutePathToAssetIsReturned()
$this->kernelMock->expects($this->any())
->method('locateResource')
->will($this->returnCallback(function (...$args) {
if ($args[0] === '@TrikoderManifestAssetBundle/test-manifest.json') {
return realpath(__DIR__ .'/../Service/test-manifest.json');
if ('@TrikoderManifestAssetBundle/test-manifest.json' === $args[0]) {
return realpath(__DIR__ . '/../Service/test-manifest.json');
}
}));

$service = new ManifestReaderService($this->kernelMock, $this->twigLoaderFilesystem, 'test-manifest.json');
$requestStack = new RequestStack();
$extension = new ManifestAssetExtension($service, $requestStack, __DIR__.'/../public');
$extension = new ManifestAssetExtension($service, $requestStack, __DIR__ . '/../public');

$this->assertSame('This is Sparta.', $extension->manifestAssetInlineFilter("@TrikoderManifestAssetBundle:test.txt"));
$this->assertSame('This is Sparta.', $extension->manifestAssetInlineFilter("@TestNamespace/test.txt"));
$this->assertSame('This is Sparta.', $extension->manifestAssetInlineFilter('@TrikoderManifestAssetBundle:test.txt'));
$this->assertSame('This is Sparta.', $extension->manifestAssetInlineFilter('@TestNamespace/test.txt'));

$this->assertSame('/bundles/trikodermanifestasset/dev/test.txt', $extension->manifestAssetFilter("@TrikoderManifestAssetBundle:test.txt"));
$this->assertSame('/testnamespace/dev/test.txt', $extension->manifestAssetFilter("@TestNamespace/test.txt"));
$this->assertSame('/bundles/trikodermanifestasset/dev/test.txt', $extension->manifestAssetFilter('@TrikoderManifestAssetBundle:test.txt'));
$this->assertSame('/testnamespace/dev/test.txt', $extension->manifestAssetFilter('@TestNamespace/test.txt'));
}

public function testExceptionIsThrownIfFileDoesNotExist()
{
$this->kernelMock->expects($this->any())
->method('locateResource')
->will($this->returnCallback(function (...$args) {
if ($args[0] === '@TrikoderManifestAssetBundle/test-manifest.json') {
return realpath(__DIR__ .'/../Service/test-manifest.json');
if ('@TrikoderManifestAssetBundle/test-manifest.json' === $args[0]) {
return realpath(__DIR__ . '/../Service/test-manifest.json');
}
}));

$service = new ManifestReaderService($this->kernelMock, $this->twigLoaderFilesystem, 'test-manifest.json');
$requestStack = new RequestStack();
$extension = new ManifestAssetExtension($service, $requestStack, __DIR__.'/../public');
$extension = new ManifestAssetExtension($service, $requestStack, __DIR__ . '/../public');

$this->expectException(\InvalidArgumentException::class);
$extension->manifestAssetInlineFilter("@TrikoderManifestAssetBundle:doesNotExist.css");
$extension->manifestAssetInlineFilter('@TrikoderManifestAssetBundle:doesNotExist.css');
}

/**
Expand All @@ -79,24 +79,25 @@ public function publicPathIsUsedIfExists()
$this->kernelMock->expects($this->any())
->method('locateResource')
->will($this->returnCallback(function (...$args) {
if ($args[0] === '@TrikoderManifestAssetBundle/test-manifest.json') {
return realpath(__DIR__ .'/../Service/test-manifest-publicPath.json');
if ('@TrikoderManifestAssetBundle/test-manifest.json' === $args[0]) {
return realpath(__DIR__ . '/../Service/test-manifest-publicPath.json');
}
}));

$service = new ManifestReaderService($this->kernelMock, $this->twigLoaderFilesystem, 'test-manifest.json');
$service = new ManifestReaderService($this->kernelMock, $this->twigLoaderFilesystem, 'test-manifest.json');
$requestStack = new RequestStack();
$extension = new ManifestAssetExtension($service, $requestStack, __DIR__.'/../public');
$extension = new ManifestAssetExtension($service, $requestStack, __DIR__ . '/../public');

$this->assertEquals('https://www.google.com/images/dev/test.txt', $extension->manifestAssetFilter("@TrikoderManifestAssetBundle:test.txt"));
$this->assertEquals('https://www.google.com/images/dev/test.txt', $extension->manifestAssetFilter("@TrikoderManifestAssetBundle:test.txt", ['absolute' => true]));
$this->assertEquals('https://www.google.com/images/dev/test.txt', $extension->manifestAssetFilter('@TrikoderManifestAssetBundle:test.txt'));
$this->assertEquals('https://www.google.com/images/dev/test.txt', $extension->manifestAssetFilter('@TrikoderManifestAssetBundle:test.txt', ['absolute' => true]));

# Now, test it with twig namespace
$service = new ManifestReaderService($this->kernelMock, $this->twigLoaderFilesystem, 'test-manifest-publicPath.json');
$extension = new ManifestAssetExtension($service, $requestStack, __DIR__.'/../public');
$this->assertEquals('https://www.google.com/images/dev/test.txt', $extension->manifestAssetFilter("@TestNamespace/test.txt"));
$this->assertEquals('https://www.google.com/images/dev/test.txt', $extension->manifestAssetFilter("@TestNamespace/test.txt", ['absolute' => true]));
// Now, test it with twig namespace
$service = new ManifestReaderService($this->kernelMock, $this->twigLoaderFilesystem, 'test-manifest-publicPath.json');
$extension = new ManifestAssetExtension($service, $requestStack, __DIR__ . '/../public');
$this->assertEquals('https://www.google.com/images/dev/test.txt', $extension->manifestAssetFilter('@TestNamespace/test.txt'));
$this->assertEquals('https://www.google.com/images/dev/test.txt', $extension->manifestAssetFilter('@TestNamespace/test.txt', ['absolute' => true]));
}

/**
* @test
*/
Expand All @@ -105,16 +106,16 @@ public function publicPathIsntUsedForInlineAssets()
$this->kernelMock->expects($this->any())
->method('locateResource')
->will($this->returnCallback(function (...$args) {
if ($args[0] === '@TrikoderManifestAssetBundle/test-manifest.json') {
return realpath(__DIR__ .'/../Service/test-manifest-publicPath.json');
if ('@TrikoderManifestAssetBundle/test-manifest.json' === $args[0]) {
return realpath(__DIR__ . '/../Service/test-manifest-publicPath.json');
}
}));

$service = new ManifestReaderService($this->kernelMock, $this->twigLoaderFilesystem, 'test-manifest.json');
$requestStack = new RequestStack();
$extension = new ManifestAssetExtension($service, $requestStack, __DIR__.'/../public');
$extension = new ManifestAssetExtension($service, $requestStack, __DIR__ . '/../public');

$this->assertEquals('This is Sparta.', $extension->manifestAssetInlineFilter("@TrikoderManifestAssetBundle:test.txt"));
$this->assertEquals('This is Sparta.', $extension->manifestAssetInlineFilter("@TestNamespace/test.txt"));
$this->assertEquals('This is Sparta.', $extension->manifestAssetInlineFilter('@TrikoderManifestAssetBundle:test.txt'));
$this->assertEquals('This is Sparta.', $extension->manifestAssetInlineFilter('@TestNamespace/test.txt'));
}
}
1 change: 0 additions & 1 deletion TrikoderManifestAssetBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

class TrikoderManifestAssetBundle extends Bundle
{

}
Loading

0 comments on commit fe3d035

Please sign in to comment.