diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..cb445a2 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +/.github export-ignore +/tests export-ignore +/phpunit.xml.dist export-ignore diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..e517eeb --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,46 @@ +name: Tests + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +permissions: + contents: read + +jobs: + build: + name: PHP ${{ matrix.php }} / Symfony ${{ matrix.symfony }}${{ matrix.deps == 'lowest' && ' / lowest deps' || '' }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + php: [ '8.1', '8.2', '8.3', '8.4' ] + symfony: [ '^5.4', '^6.4' ] + deps: [ highest, lowest ] + steps: + - uses: actions/checkout@v5 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: json + coverage: none + tools: composer:v2 + + - name: Install dependencies + run: | + composer require \ + 'symfony/framework-bundle:${{ matrix.symfony }}' \ + 'symfony/yaml:${{ matrix.symfony }}' \ + --no-update + composer require --dev 'symfony/twig-bundle:${{ matrix.symfony }}' --no-update + composer update --prefer-dist --no-progress --no-security-blocking ${{ matrix.deps == 'lowest' && '--prefer-lowest --prefer-stable' || '' }} + + - name: Installed versions + run: composer show | grep -E '^(symfony/(framework-bundle|http-kernel|twig-bundle)|twig/twig|phpunit/phpunit) ' + + - name: PHPUnit + run: composer test diff --git a/.gitignore b/.gitignore index 0dca145..6d935b9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /vendor/ .idea +/composer.lock +/.phpunit.result.cache diff --git a/AssetsBundle/Controller/AssetsController.php b/AssetsBundle/Controller/AssetsController.php deleted file mode 100644 index f717b92..0000000 --- a/AssetsBundle/Controller/AssetsController.php +++ /dev/null @@ -1,35 +0,0 @@ -get('docplanner_assets_bundle.service.assets_loader'); - - foreach($assetsLoader->assets($assetsType, false, $forRoute) as $script) - { - $scripts[] = sprintf('', $script); - } - - $rsp = new Response(implode("\n", $scripts)); - $rsp->setPrivate(); - $rsp->setTtl(5); - - return $rsp; - } - -} \ No newline at end of file diff --git a/AssetsBundle/DependencyInjection/Configuration.php b/AssetsBundle/DependencyInjection/Configuration.php index 501ecc1..9147572 100644 --- a/AssetsBundle/DependencyInjection/Configuration.php +++ b/AssetsBundle/DependencyInjection/Configuration.php @@ -14,7 +14,7 @@ class Configuration implements ConfigurationInterface /** * {@inheritdoc} */ - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('docplanner_assets'); /** @noinspection PhpUndefinedMethodInspection */ diff --git a/composer.json b/composer.json index 1026bd3..783f8d9 100644 --- a/composer.json +++ b/composer.json @@ -19,18 +19,33 @@ "require": { "php": "^7.4 || ^8.0", "ext-json": "*", - "symfony/framework-bundle": "^2.8 || ^3.0 || ^4.0 || ^5.4", + "symfony/framework-bundle": "^5.4 || ^6.4", + "symfony/yaml": "^5.4 || ^6.4", "twig/twig": "^1.38 || ^2 || ^3" }, + "require-dev": { + "phpunit/phpunit": "^9.6", + "symfony/twig-bundle": "^5.4 || ^6.4" + }, "autoload": { "psr-4": { "Docplanner\\AssetsBundle\\": "AssetsBundle/" } }, + "autoload-dev": { + "psr-4": { + "Docplanner\\AssetsBundle\\Tests\\": "tests/" + } + }, + "scripts": { + "test": [ + "vendor/bin/phpunit -c phpunit.xml.dist" + ] + }, "minimum-stability": "stable", "extra": { "branch-alias": { - "dev-master": "2.0.x-dev" + "dev-master": "3.0.x-dev" } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..909723a --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,12 @@ + + + + + tests + + + diff --git a/tests/BundleTest.php b/tests/BundleTest.php new file mode 100644 index 0000000..7aade4c --- /dev/null +++ b/tests/BundleTest.php @@ -0,0 +1,191 @@ +kernels as $kernel) { + $kernel->shutdown(); + (new Filesystem())->remove($kernel->getTmpDir()); + } + $this->kernels = []; + } + + public function testBundleBootsAndRegistersServices(): void + { + $container = $this->bootKernel()->getContainer(); + + self::assertInstanceOf(AssetsLoader::class, $container->get('docplanner_assets_bundle.service.assets_loader')); + self::assertInstanceOf(AssetsProvider::class, $container->get('docplanner_assets.service.assets_provider')); + self::assertInstanceOf(AssetsPicker::class, $container->get('docplanner_assets.service.assets_picker')); + self::assertInstanceOf(AssetsRepository::class, $container->get('docplanner_assets.service.assets_repository')); + self::assertInstanceOf(AssetsExtension::class, $container->get('docplanner_assets_bundle.twig.assets')); + self::assertContains(Kernel::MAJOR_VERSION, [5, 6]); + } + + public function testConfigIsResolvedIntoUrlsAndPaths(): void + { + $config = $this->bootKernel()->getContainer()->getParameter('docplanner_assets.config'); + $common = $config['types']['style']['assets']['common']; + $cdn = $config['types']['style']['assets']['cdn']; + + $path = $this->fixturesDir() . 'css/common.css'; + self::assertSame($path, $common['path']); + self::assertSame('//static.example.com/css/common.css?v=' . crc32(file_get_contents($path)), $common['url']); + self::assertNull($cdn['path']); + self::assertSame('https://cdn.example.com/lib.css', $cdn['url']); + } + + public function testRevisionsCanBeDisabled(): void + { + $config = $this->defaultConfig(); + $config['use_revisions'] = false; + + $loader = $this->bootKernel($config)->getContainer()->get('docplanner_assets_bundle.service.assets_loader'); + + self::assertSame('//static.example.com/css/common.css', $loader->assets('style')['common']); + } + + public function testLoaderReturnsDefaultGroupWithoutRequest(): void + { + $loader = $this->bootKernel()->getContainer()->get('docplanner_assets_bundle.service.assets_loader'); + + self::assertSame(['common', 'cdn'], array_keys($loader->assets('style'))); + self::assertSame([], $loader->assets('style', true)); + } + + public function testLoaderPicksGroupForCurrentRoute(): void + { + $container = $this->bootKernel()->getContainer(); + $container->get('request_stack')->push(new Request([], [], ['_route' => 'homepage'])); + + $loader = $container->get('docplanner_assets_bundle.service.assets_loader'); + + self::assertSame(['common'], array_keys($loader->assets('style'))); + self::assertSame(['home' => 'h1{color:blue}'], $loader->assets('style', true)); + } + + public function testLoaderHonoursForcedRoute(): void + { + $loader = $this->bootKernel()->getContainer()->get('docplanner_assets_bundle.service.assets_loader'); + + self::assertSame(['home' => 'h1{color:blue}'], $loader->assets('style', true, 'homepage')); + } + + public function testManifestAssetsAreRegistered(): void + { + $loader = $this->bootKernel()->getContainer()->get('docplanner_assets_bundle.service.assets_loader'); + + self::assertSame( + ['app' => '//static.example.com/js/app.js?v=' . crc32('console.log(1);'), 'app.js' => '//static.example.com/build/app.abc123.js'], + $loader->assets('script') + ); + } + + public function testUnknownTypeIsRejected(): void + { + $loader = $this->bootKernel()->getContainer()->get('docplanner_assets_bundle.service.assets_loader'); + + $this->expectException(LogicException::class); + $this->expectExceptionMessage('Type "font" is not defined!'); + $loader->assets('font'); + } + + public function testMissingAssetFileFailsTheBuild(): void + { + $config = $this->defaultConfig(); + $config['types']['style']['assets']['missing'] = ['src' => 'css/missing.css']; + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('asset "missing"'); + $this->bootKernel($config); + } + + public function testTwigFunctionsRenderAssets(): void + { + $container = $this->bootKernel()->getContainer(); + $container->get('request_stack')->push(new Request([], [], ['_route' => 'homepage'])); + + $twig = $container->get('twig'); + $template = $twig->createTemplate( + '{% for s in assets_style() %}{% endfor %}' + ); + + self::assertSame( + '', + $template->render([]) + ); + } + + public function testLoaderClassCanBeOverriddenByParameter(): void + { + $kernel = $this->bootKernel(null, ['docplanner_assets_bundle.service.assets_loader.class' => CustomAssetsLoader::class]); + + self::assertInstanceOf(CustomAssetsLoader::class, $kernel->getContainer()->get('docplanner_assets_bundle.service.assets_loader')); + } + + private function bootKernel(?array $config = null, array $parameters = []): TestKernel + { + $kernel = new TestKernel($config ?? $this->defaultConfig(), $parameters); + $this->kernels[] = $kernel; + (new Filesystem())->remove($kernel->getTmpDir()); + $kernel->boot(); + + return $kernel; + } + + private function fixturesDir(): string + { + return __DIR__ . '/Fixtures/public/'; + } + + private function defaultConfig(): array + { + return [ + 'base_host' => '//static.example.com/', + 'base_path' => $this->fixturesDir(), + 'types' => [ + 'style' => [ + 'assets' => [ + 'common' => ['src' => 'css/common.css'], + 'home' => ['src' => 'css/home.css', 'inline' => true], + 'cdn' => ['src' => 'https://cdn.example.com/lib.css'], + ], + 'groups' => [ + 'default' => ['assets' => ['common', 'cdn'], 'default' => true], + 'homepage' => ['assets' => ['common', 'home'], 'routes' => ['homepage']], + ], + ], + 'script' => [ + 'manifest_file' => $this->fixturesDir() . 'build/manifest.json', + 'manifest_assets' => ['app.js'], + 'assets' => [ + 'app' => ['src' => 'js/app.js'], + ], + 'groups' => [ + 'default' => ['assets' => ['app', 'app.js'], 'default' => true], + ], + ], + ], + ]; + } +} diff --git a/tests/Fixtures/CustomAssetsLoader.php b/tests/Fixtures/CustomAssetsLoader.php new file mode 100644 index 0000000..cc1609e --- /dev/null +++ b/tests/Fixtures/CustomAssetsLoader.php @@ -0,0 +1,9 @@ +getDefinitions() as $id => $definition) { + if (0 === strpos($id, 'docplanner_assets') || 'twig' === $id || 'request_stack' === $id) { + $definition->setPublic(true); + } + } + } +} diff --git a/tests/Fixtures/TestKernel.php b/tests/Fixtures/TestKernel.php new file mode 100644 index 0000000..52e535c --- /dev/null +++ b/tests/Fixtures/TestKernel.php @@ -0,0 +1,81 @@ +assetsConfig = $assetsConfig; + $this->parameters = $parameters; + + parent::__construct('test', true); + } + + public function registerBundles(): iterable + { + return [ + new FrameworkBundle(), + new TwigBundle(), + new DocplannerAssetsBundle(), + ]; + } + + public function registerContainerConfiguration(LoaderInterface $loader): void + { + $loader->load(function (ContainerBuilder $container): void { + $container->loadFromExtension('framework', [ + 'secret' => 'test', + 'http_method_override' => false, + ]); + $container->loadFromExtension('twig', [ + 'strict_variables' => true, + ]); + $container->loadFromExtension('docplanner_assets', $this->assetsConfig); + + foreach ($this->parameters as $name => $value) { + $container->setParameter($name, $value); + } + }); + } + + protected function build(ContainerBuilder $container): void + { + // expose the bundle services so tests can fetch them directly + $container->addCompilerPass(new PublicServicesPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION); + } + + public function getProjectDir(): string + { + return __DIR__; + } + + public function getCacheDir(): string + { + return $this->getTmpDir() . '/cache'; + } + + public function getLogDir(): string + { + return $this->getTmpDir() . '/logs'; + } + + public function getTmpDir(): string + { + return sys_get_temp_dir() . '/assets-bundle-tests/' . md5(serialize([$this->assetsConfig, $this->parameters])); + } +} diff --git a/tests/Fixtures/public/build/app.abc123.js b/tests/Fixtures/public/build/app.abc123.js new file mode 100644 index 0000000..4239a8a --- /dev/null +++ b/tests/Fixtures/public/build/app.abc123.js @@ -0,0 +1 @@ +console.log(2); \ No newline at end of file diff --git a/tests/Fixtures/public/build/manifest.json b/tests/Fixtures/public/build/manifest.json new file mode 100644 index 0000000..99d1341 --- /dev/null +++ b/tests/Fixtures/public/build/manifest.json @@ -0,0 +1 @@ +{"app.js":"build/app.abc123.js"} \ No newline at end of file diff --git a/tests/Fixtures/public/css/common.css b/tests/Fixtures/public/css/common.css new file mode 100644 index 0000000..363a063 --- /dev/null +++ b/tests/Fixtures/public/css/common.css @@ -0,0 +1 @@ +body{color:red} \ No newline at end of file diff --git a/tests/Fixtures/public/css/home.css b/tests/Fixtures/public/css/home.css new file mode 100644 index 0000000..0181c0e --- /dev/null +++ b/tests/Fixtures/public/css/home.css @@ -0,0 +1 @@ +h1{color:blue} \ No newline at end of file diff --git a/tests/Fixtures/public/js/app.js b/tests/Fixtures/public/js/app.js new file mode 100644 index 0000000..4e2e0f5 --- /dev/null +++ b/tests/Fixtures/public/js/app.js @@ -0,0 +1 @@ +console.log(1); \ No newline at end of file diff --git a/tests/Service/AssetsPickerTest.php b/tests/Service/AssetsPickerTest.php new file mode 100644 index 0000000..7a8b258 --- /dev/null +++ b/tests/Service/AssetsPickerTest.php @@ -0,0 +1,56 @@ + [ + 'style' => [ + 'groups' => [ + 'base' => ['assets' => ['a', 'b'], 'default' => true, 'routes' => []], + 'extra' => ['assets' => ['b', 'c'], 'default' => true, 'routes' => []], + 'one' => ['assets' => ['a', 'd'], 'default' => false, 'routes' => ['r1', 'r2']], + 'two' => ['assets' => ['d', 'e'], 'default' => false, 'routes' => ['r2']], + ], + ], + ], + ]; + + public function testDefaultsAreMergedAndUnique(): void + { + $picker = new AssetsPicker(new RequestStack(), self::CONFIG); + + self::assertSame(['a', 'b', 'c'], array_values($picker->pickAssets('style'))); + } + + public function testRouteGroupsWinOverDefaults(): void + { + $stack = new RequestStack(); + $stack->push(new Request([], [], ['_route' => 'r2'])); + $picker = new AssetsPicker($stack, self::CONFIG); + + self::assertSame(['a', 'd', 'e'], array_values($picker->pickAssets('style'))); + } + + public function testForcedRouteIgnoresCurrentRequest(): void + { + $stack = new RequestStack(); + $stack->push(new Request([], [], ['_route' => 'r2'])); + $picker = new AssetsPicker($stack, self::CONFIG); + + self::assertSame(['a', 'd'], array_values($picker->pickAssets('style', 'r1'))); + } + + public function testUnknownRouteFallsBackToDefaults(): void + { + $picker = new AssetsPicker(new RequestStack(), self::CONFIG); + + self::assertSame(['a', 'b', 'c'], array_values($picker->pickAssets('style', 'nope'))); + } +}