From 7c0b6148cdec374a2394a89afef9d9ed80e4cd4d Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Wed, 28 Jun 2023 13:15:34 -0400 Subject: [PATCH] Adding functional test for AssetMapper integration --- phpunit.xml.dist | 1 + src/AssetMapper/TailwindCssAssetCompiler.php | 2 +- .../TailwindCssAssetCompilerTest.php | 10 +-- tests/FunctionalTest.php | 47 +++++++++++++ tests/TailwindBuilderTest.php | 17 ++++- tests/fixtures/TailwindTestKernel.php | 68 +++++++++++++++++++ tests/fixtures/{ => assets/styles}/app.css | 0 tests/fixtures/assets/styles/other.css | 0 8 files changed, 137 insertions(+), 8 deletions(-) create mode 100644 tests/FunctionalTest.php create mode 100644 tests/fixtures/TailwindTestKernel.php rename tests/fixtures/{ => assets/styles}/app.css (100%) create mode 100644 tests/fixtures/assets/styles/other.css diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 1ceefe1..d580fd5 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -11,6 +11,7 @@ + diff --git a/src/AssetMapper/TailwindCssAssetCompiler.php b/src/AssetMapper/TailwindCssAssetCompiler.php index 85c2a2f..edd7264 100644 --- a/src/AssetMapper/TailwindCssAssetCompiler.php +++ b/src/AssetMapper/TailwindCssAssetCompiler.php @@ -25,7 +25,7 @@ public function __construct(private TailwindBuilder $tailwindBuilder) public function supports(MappedAsset $asset): bool { - return $asset->sourcePath === $this->tailwindBuilder->getInputCssPath(); + return realpath($asset->sourcePath) === realpath($this->tailwindBuilder->getInputCssPath()); } public function compile(string $content, MappedAsset $asset, AssetMapperInterface $assetMapper): string diff --git a/tests/AssetMapper/TailwindCssAssetCompilerTest.php b/tests/AssetMapper/TailwindCssAssetCompilerTest.php index d56a0de..32f9f55 100644 --- a/tests/AssetMapper/TailwindCssAssetCompilerTest.php +++ b/tests/AssetMapper/TailwindCssAssetCompilerTest.php @@ -22,17 +22,17 @@ public function testCompile() $builder = $this->createMock(TailwindBuilder::class); $builder->expects($this->any()) ->method('getInputCssPath') - ->willReturn('/foo/bar/input.css'); + ->willReturn(__DIR__.'/../fixtures/assets/styles/app.css'); $builder->expects($this->once()) - ->method('getInternalOutputCssPath') - ->willReturn('/foo/bar/output.css'); + ->method('getInternalOutputCssPath'); $builder->expects($this->once()) ->method('getOutputCssContent') ->willReturn('output content from Tailwind'); $compiler = new TailwindCssAssetCompiler($builder); - $asset1 = new MappedAsset('other/file.css', '/some/other/file.css'); - $asset2 = new MappedAsset('bar/input.css', '/foo/bar/input.css'); + $asset1 = new MappedAsset('styles/other.css', __DIR__.'/../fixtures/assets/styles/other.css'); + // extra ../ added so the path doesn't exactly match the string used above + $asset2 = new MappedAsset('styles/app.css', __DIR__.'/../../tests/fixtures/assets/styles/app.css'); $this->assertFalse($compiler->supports($asset1)); $this->assertTrue($compiler->supports($asset2)); diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php new file mode 100644 index 0000000..6b840b1 --- /dev/null +++ b/tests/FunctionalTest.php @@ -0,0 +1,47 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfonycasts\TailwindBundle\Tests; + +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\Component\AssetMapper\AssetMapperInterface; +use Symfony\Component\AssetMapper\MappedAsset; +use Symfony\Component\Filesystem\Filesystem; + +class FunctionalTest extends KernelTestCase +{ + protected function setUp(): void + { + $fs = new Filesystem(); + $tailwindVarDir = __DIR__.'/fixtures/var/tailwind'; + if (is_dir($tailwindVarDir)) { + $fs->remove($tailwindVarDir); + } + $fs->mkdir($tailwindVarDir); + file_put_contents($tailwindVarDir.'/tailwind.built.css', 'the built css'); + } + + protected function tearDown(): void + { + if (is_file(__DIR__.'/fixtures/var/tailwind/tailwind.built.css')) { + unlink(__DIR__.'/fixtures/var/tailwind/tailwind.built.css'); + } + } + + public function testBuiltCSSFileIsUsed(): void + { + self::bootKernel(); + $assetMapper = self::getContainer()->get('asset_mapper'); + \assert($assetMapper instanceof AssetMapperInterface); + + $asset = $assetMapper->getAsset('styles/app.css'); + $this->assertInstanceOf(MappedAsset::class, $asset); + $this->assertSame('the built css', $asset->content); + } +} diff --git a/tests/TailwindBuilderTest.php b/tests/TailwindBuilderTest.php index 6cd9d9d..06d0048 100644 --- a/tests/TailwindBuilderTest.php +++ b/tests/TailwindBuilderTest.php @@ -11,21 +11,34 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Finder\Finder; use Symfonycasts\TailwindBundle\TailwindBuilder; class TailwindBuilderTest extends TestCase { - public function testIntegration(): void + protected function setUp(): void { $fs = new Filesystem(); if (file_exists(__DIR__.'/fixtures/var/tailwind')) { $fs->remove(__DIR__.'/fixtures/var/tailwind'); } $fs->mkdir(__DIR__.'/fixtures/var/tailwind'); + } + + protected function tearDown(): void + { + $finder = new Finder(); + $finder->in(__DIR__.'/fixtures/var/tailwind')->files(); + foreach ($finder as $file) { + unlink($file->getRealPath()); + } + } + public function testIntegration(): void + { $builder = new TailwindBuilder( __DIR__.'/fixtures', - __DIR__.'/fixtures/app.css', + __DIR__.'/fixtures/assets/styles/app.css', __DIR__.'/fixtures/var/tailwind' ); $process = $builder->runBuild(false); diff --git a/tests/fixtures/TailwindTestKernel.php b/tests/fixtures/TailwindTestKernel.php new file mode 100644 index 0000000..1e1b8dc --- /dev/null +++ b/tests/fixtures/TailwindTestKernel.php @@ -0,0 +1,68 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfonycasts\TailwindBundle\Tests\fixtures; + +use Symfony\Bundle\FrameworkBundle\FrameworkBundle; +use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; +use Symfony\Component\Config\Loader\LoaderInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\HttpKernel\Kernel; +use Symfonycasts\TailwindBundle\SymfonycastsTailwindBundle; + +class TailwindTestKernel extends Kernel +{ + use MicroKernelTrait; + + public function __construct() + { + parent::__construct('test', true); + } + + public function registerBundles(): array + { + return [ + new FrameworkBundle(), + new SymfonycastsTailwindBundle(), + ]; + } + + protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void + { + $container->loadFromExtension('framework', [ + 'secret' => 'foo', + 'test' => true, + 'http_method_override' => true, + 'asset_mapper' => [ + 'paths' => [ + __DIR__.'/assets', + ], + ], + ]); + + $container->loadFromExtension('symfonycasts_tailwind', [ + 'input_css' => __DIR__.'/assets/styles/app.css', + ]); + } + + public function getCacheDir(): string + { + return sys_get_temp_dir().'/cache'.spl_object_hash($this); + } + + public function getLogDir(): string + { + return sys_get_temp_dir().'/logs'.spl_object_hash($this); + } + + public function getProjectDir(): string + { + return __DIR__; + } +} diff --git a/tests/fixtures/app.css b/tests/fixtures/assets/styles/app.css similarity index 100% rename from tests/fixtures/app.css rename to tests/fixtures/assets/styles/app.css diff --git a/tests/fixtures/assets/styles/other.css b/tests/fixtures/assets/styles/other.css new file mode 100644 index 0000000..e69de29