Split extension into extension and runtime classes to match how the maker bundle creates twig extensions

This commit is contained in:
Brabli
2023-05-31 11:51:36 +01:00
parent 08d906260a
commit 493f0c275b
4 changed files with 38 additions and 31 deletions

View File

@@ -0,0 +1,251 @@
<?php
declare(strict_types=1);
namespace Pcm\IconBundle\Tests\Twig\Functions;
use Pcm\IconBundle\Exception\ColourNotFound;
use Pcm\IconBundle\Exception\IconNotFound;
use Pcm\IconBundle\Twig\Runtime\IconRuntime;
use PHPUnit\Framework\TestCase;
class IconRuntimeTest extends TestCase
{
private const ICON = 'test';
private const COLOURS = [
'primary' => [
'fill' => 'fill-primary',
'stroke' => 'stroke-primary',
'fill-hover' => 'hover:fill-primary',
'stroke-hover' => 'hover:stroke-primary',
'fill-group-hover' => 'group-hover:fill-primary',
'stroke-group-hover' => 'group-hover:stroke-primary',
],
'white' => [
'fill' => 'fill-white',
'stroke' => 'stroke-white',
'fill-hover' => 'hover:fill-white',
'stroke-hover' => 'hover:stroke-white',
'fill-group-hover' => 'group-hover:fill-white',
'stroke-group-hover' => 'group-hover:stroke-white',
]
];
private IconRuntime $icon;
protected function setUp(): void
{
$this->icon = new IconRuntime(
directories: ['tests/icons'],
colours: self::COLOURS
);
}
public function testThrowsWhenPassedAnInvalidIconName(): void
{
$this->expectException(IconNotFound::class);
$this->icon->renderIcon(['icon' => random_bytes(8)]);
}
public function testNoTitleExistsIfNotPassedIn(): void
{
$content = $this->icon->renderIcon(['icon' => self::ICON]);
$this->assertStringNotContainsString('<title>', $content);
$this->assertStringNotContainsString('</title>', $content);
}
public function testTitleGetsAddedIfSpecified(): void
{
$title = 'test_title';
$content = $this->icon->renderIcon(['icon' => self::ICON, 'title' => $title]);
$this->assertMatchesRegularExpression("/<title>{$title}<\/title>/", $content);
}
public function testTitleThrowsIfNotPassedAString(): void
{
$this->expectException(\TypeError::class);
$this->icon->renderIcon(['icon' => self::ICON, 'title' => 99]);
}
public function testTitleThrowsIfPassedAnEmptyString():void
{
$this->expectException(\InvalidArgumentException::class);
$this->icon->renderIcon(['icon' => self::ICON, 'title' => '']);
}
public function testThrowsWhenPassedNegativeSizeValue(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->icon->renderIcon(['icon' => self::ICON, 'size' => -1]);
}
public function testThrowsWhenSizeIsNotAnInt(): void
{
$this->expectException(\TypeError::class);
$this->icon->renderIcon(['icon' => self::ICON, 'size' => 1.0]);
}
public function testSvgWidthIsSet(): void
{
$regex = '/^<svg.+?width="99"/';
$content = $this->icon->renderIcon(['icon' => self::ICON, 'size' => 99]);
$this->assertMatchesRegularExpression($regex, $content);
}
public function testSvgHeightIsSet(): void
{
$regex = '/^<svg.+?width="99"/';
$content = $this->icon->renderIcon(['icon' => self::ICON, 'size' => 99]);
$this->assertMatchesRegularExpression($regex, $content);
}
public function testNoXmlDeclarationIsAdded(): void
{
$content = $this->icon->renderIcon(['icon' => self::ICON, 'size' => 99]);
$this->assertStringNotContainsString('<xml', $content);
}
public function testOnlyOneWidthAttributeIsSet(): void
{
$content = $this->icon->renderIcon(['icon' => self::ICON, 'size' => 99]);
$timesMatched = preg_match_all('/width="99"/', $content);
$this->assertSame(1, $timesMatched);
}
public function testOnlyOneHeightAttributeIsSet(): void
{
$content = $this->icon->renderIcon(['icon' => self::ICON, 'size' => 99]);
$timesMatched = preg_match_all('/height="99"/', $content);
$this->assertSame(1, $timesMatched);
}
public function testDefaultSizeIsSetOnSvgIfNoSizeOptionPassed(): void
{
$defaultSize = IconRuntime::DEFAULT_SIZE;
$content = $this->icon->renderIcon(['icon' => self::ICON]);
$regex = "/^<svg.+?width=\"{$defaultSize}\"/";
$this->assertMatchesRegularExpression($regex, $content);
$regex = "/^<svg.+?height=\"{$defaultSize}\"/";
$this->assertMatchesRegularExpression($regex, $content);
}
public function testDefaultSizeIsOnlySetOnce(): void
{
$defaultSize = IconRuntime::DEFAULT_SIZE;
$content = $this->icon->renderIcon(['icon' => self::ICON]);
$widthRegex = "/width=\"{$defaultSize}\"/";
$timesMatched = preg_match_all($widthRegex, $content);
$this->assertSame(1, $timesMatched);
$heightRegex = "/height=\"{$defaultSize}\"/";
$timesMatched = preg_match_all($heightRegex, $content);
$this->assertSame(1, $timesMatched);
}
public function testBlackStrokeAttributeValuesAreRemoved(): void
{
$content = $this->icon->renderIcon(['icon' => self::ICON]);
$this->assertDoesNotMatchRegularExpression('/stroke="\s*#000\s*"/', $content);
$this->assertDoesNotMatchRegularExpression('/stroke="\s*#000000\s*"/', $content);
$this->assertDoesNotMatchRegularExpression('/stroke="\s*#black\s*"/', $content);
$this->assertDoesNotMatchRegularExpression('/stroke="\s*rgb\(0,\s*0,\s*0\)\s*"/', $content);
}
public function testBlackStrokeStylesAreRemoved(): void
{
$content = $this->icon->renderIcon(['icon' => self::ICON]);
$this->assertDoesNotMatchRegularExpression('/stroke:\s*#000\s*/', $content);
$this->assertDoesNotMatchRegularExpression('/stroke:\s*#000000\s*/', $content);
$this->assertDoesNotMatchRegularExpression('/stroke:\s*black\s*/', $content);
$this->assertDoesNotMatchRegularExpression('/stroke:\s*rgb\(0,\s*0,\s*0\)\s*/', $content);
}
public function testBlackFillAttributeValuesAreRemoved(): void
{
$content = $this->icon->renderIcon(['icon' => self::ICON]);
$this->assertDoesNotMatchRegularExpression('/fill="\s*#000\s*"/', $content);
$this->assertDoesNotMatchRegularExpression('/fill="\s*#000000\s*"/', $content);
$this->assertDoesNotMatchRegularExpression('/fill="\s*#black\s*"/', $content);
$this->assertDoesNotMatchRegularExpression('/fill="\s*rgb\(0,\s*0,\s*0\)\s*"/', $content);
}
public function testBlackFillStylesAreRemoved(): void
{
$content = $this->icon->renderIcon(['icon' => self::ICON]);
$this->assertDoesNotMatchRegularExpression('/fill:\s*#000\s*/', $content);
$this->assertDoesNotMatchRegularExpression('/fill:\s*#000000\s*/', $content);
$this->assertDoesNotMatchRegularExpression('/fill:\s*black\s*/', $content);
$this->assertDoesNotMatchRegularExpression('/fill:\s*rgb\(0,\s*0,\s*0\)\s*/', $content);
}
public function testThrowsIfColourIsNotFound(): void
{
$this->expectException(ColourNotFound::class);
$this->icon->renderIcon(['icon' => self::ICON, 'colour' => 'red']);
}
public function testSvgClassContainsColourClasses(): void
{
$contents = $this->icon->renderIcon(['icon' => self::ICON, 'colour' => 'white']);
$this->assertMatchesRegularExpression('/<svg.*?class=".*?fill-white.*?>/', $contents);
$this->assertMatchesRegularExpression('/<svg.*?class=".*?stroke-white?.*>/', $contents);
}
public function testThrowsIfHoverColourIsNotFound(): void
{
$this->expectException(ColourNotFound::class);
$this->icon->renderIcon(['icon' => self::ICON, 'hover' => 'red']);
}
public function testSvgClassContainsHoverColourClasses(): void
{
$contents = $this->icon->renderIcon(['icon' => self::ICON, 'hover' => 'white']);
$this->assertMatchesRegularExpression('/<svg.+class=".*cursor-pointer.*".*>/', $contents);
$this->assertMatchesRegularExpression('/<svg.+class=".*hover:fill-white.*".*>/', $contents);
$this->assertMatchesRegularExpression('/<svg.+class=".*hover:stroke-white.*".*>/', $contents);
$this->assertMatchesRegularExpression('/<svg.+class=".*group-hover:fill-white.*".*>/', $contents);
$this->assertMatchesRegularExpression('/<svg.+class=".*group-hover:stroke-white.*".*>/', $contents);
}
public function testSvgClassContainsHoverAndColourClasses(): void
{
$contents = $this->icon->renderIcon(['icon' => self::ICON, 'hover' => 'white', 'colour' => 'primary']);
$this->assertMatchesRegularExpression('/<svg.+class=".*fill-primary.*".*>/', $contents);
$this->assertMatchesRegularExpression('/<svg.+class=".*stroke-primary.*".*>/', $contents);
$this->assertMatchesRegularExpression('/<svg.+class=".*cursor-pointer.*".*>/', $contents);
$this->assertMatchesRegularExpression('/<svg.+class=".*hover:fill-white.*".*>/', $contents);
$this->assertMatchesRegularExpression('/<svg.+class=".*hover:stroke-white.*".*>/', $contents);
$this->assertMatchesRegularExpression('/<svg.+class=".*group-hover:fill-white.*".*>/', $contents);
$this->assertMatchesRegularExpression('/<svg.+class=".*group-hover:stroke-white.*".*>/', $contents);
}
public function testExtraClassesThrowsIfNotAnArray(): void
{
$this->expectException(\TypeError::class);
$this->icon->renderIcon(['icon' => self::ICON, 'classes' => 'string_value']);
}
public function testExtraClassesGetAdded(): void
{
$contents = $this->icon->renderIcon(['icon' => self::ICON, 'classes' => ['abc', 'def']]);
$this->assertMatchesRegularExpression('/<svg.+class=".*abc.*".*>/', $contents);
$this->assertMatchesRegularExpression('/<svg.+class=".*def.*".*>/', $contents);
}
public function testAddingExtraClassesDoesntStripAwayColourClasses(): void
{
$contents = $this->icon->renderIcon(['icon' => self::ICON, 'classes' => ['abc']]);
$this->assertMatchesRegularExpression('/<svg.+class=".*abc.*".*>/', $contents);
$this->assertMatchesRegularExpression('/<svg.+class=".*fill-primary.*".*>/', $contents);
}
public function testThrowsIfInvalidOptionPassed(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->icon->renderIcon(['icon' => self::ICON, 'fake-key-should-throw' => null]);
}
}