198 lines
6.1 KiB
PHP
198 lines
6.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pcm\IconBundle\Tests\Twig\Functions;
|
|
|
|
use InvalidArgumentException;
|
|
use Pcm\IconBundle\DependencyInjection\PcmIconExtension;
|
|
use Pcm\IconBundle\Tests\TestKernel;
|
|
use Pcm\IconBundle\Twig\Functions\IconExtension;
|
|
use Pcm\IconBundle\Twig\Functions\IconNotFound;
|
|
use PHPUnit\Framework\TestCase;
|
|
use TypeError;
|
|
|
|
class IconExtensionTest extends TestCase
|
|
{
|
|
private const ICON = 'test';
|
|
|
|
private const PALLETES = [
|
|
'primary' => [
|
|
'stroke' => 'stroke-primary',
|
|
'fill' => 'fill-primary'
|
|
],
|
|
|
|
'white' => [
|
|
'stroke' => 'stroke-white',
|
|
'fill' => 'fill-white'
|
|
],
|
|
];
|
|
|
|
/**
|
|
* @var IconExtension
|
|
*/
|
|
private IconExtension $icon;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->icon = new IconExtension(['tests/icons'], self::PALLETES);
|
|
}
|
|
|
|
public function testInstanceOf(): void
|
|
{
|
|
$this->assertInstanceOf(IconExtension::class, $this->icon);
|
|
}
|
|
|
|
public function testThrowsIfDirectoriesIsEmpty(): void
|
|
{
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
new IconExtension([], self::PALLETES);
|
|
}
|
|
|
|
public function testThrowsIfDirectoriesContainsNonString(): void
|
|
{
|
|
$this->expectException(\TypeError::class);
|
|
new IconExtension([99], self::PALLETES);
|
|
}
|
|
|
|
public function testThrowsIfDirectoriesContainsNonStringAmongStrings(): void
|
|
{
|
|
$this->expectException(\TypeError::class);
|
|
new IconExtension(['string', 99, 'string'], self::PALLETES);
|
|
}
|
|
|
|
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 = IconExtension::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 = IconExtension::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 testThrowsIfPalletsIsEmpty(): void
|
|
{
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
new IconExtension(['/'], []);
|
|
}
|
|
|
|
public function testThrowsIfPalletesContainsNonArray(): void
|
|
{
|
|
$this->expectException(\TypeError::class);
|
|
new IconExtension(['/'], [99]);
|
|
}
|
|
|
|
public function testThrowsIfPalletesContainsNonArrayInbetweenArrays(): void
|
|
{
|
|
$this->expectException(\TypeError::class);
|
|
new IconExtension(['/'], [[], 99, []]);
|
|
}
|
|
|
|
public function testThrowsIfChildArrayDoesntContainStrokeKey(): void
|
|
{
|
|
$this->expectException(\Exception::class);
|
|
new IconExtension(['/'], [['fill' => '']]);
|
|
}
|
|
|
|
public function testThrowsIfChildArrayDoesntContainFillKey(): void
|
|
{
|
|
$this->expectException(\Exception::class);
|
|
new IconExtension(['/'], [['stroke' => '']]);
|
|
}
|
|
}
|