Skip to content

Commit

Permalink
bug #6 Adding functional test for AssetMapper integration (weaverryan)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the main branch.

Discussion
----------

Adding functional test for AssetMapper integration

Closes #5

Commits
-------

7c0b614 Adding functional test for AssetMapper integration
  • Loading branch information
weaverryan committed Jun 28, 2023
2 parents 18d38ba + 7c0b614 commit 6733a17
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 8 deletions.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<php>
<ini name="display_errors" value="1" />
<ini name="error_reporting" value="-1" />
<server name="KERNEL_CLASS" value="Symfonycasts\TailwindBundle\Tests\fixtures\TailwindTestKernel" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
Expand Down
2 changes: 1 addition & 1 deletion src/AssetMapper/TailwindCssAssetCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions tests/AssetMapper/TailwindCssAssetCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
47 changes: 47 additions & 0 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the SymfonyCasts TailwindBundle package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* 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);
}
}
17 changes: 15 additions & 2 deletions tests/TailwindBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
68 changes: 68 additions & 0 deletions tests/fixtures/TailwindTestKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the SymfonyCasts TailwindBundle package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* 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__;
}
}
File renamed without changes.
Empty file.

0 comments on commit 6733a17

Please sign in to comment.