From 10c252ea6dde34ffd275c61991b0594603aeaf66 Mon Sep 17 00:00:00 2001 From: Brabli <67018167+Brabli@users.noreply.github.com> Date: Sun, 14 Aug 2022 21:37:19 +0100 Subject: [PATCH] Test for palletes not existing --- src/Twig/Functions/IconExtension.php | 24 ++++++++++++++++++++-- tests/Twig/Functions/IconExtensionTest.php | 19 ++++++++--------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/Twig/Functions/IconExtension.php b/src/Twig/Functions/IconExtension.php index 8602d34..4fc6ee4 100644 --- a/src/Twig/Functions/IconExtension.php +++ b/src/Twig/Functions/IconExtension.php @@ -15,7 +15,8 @@ final class IconExtension extends AbstractExtension private const DEFAULT_OPTIONS = [ 'icon' => null, 'title' => null, - 'size' => self::DEFAULT_SIZE + 'size' => self::DEFAULT_SIZE, + 'colour' => 'primary' ]; public function __construct(private array $directories, private array $palletes) @@ -59,7 +60,14 @@ final class IconExtension extends AbstractExtension /** * @param array $options - * [ options here ] + * ``` + * $options = [ + * 'icon' => (string) Which icon to use + * 'title' => (?string) Text to appear on mouse hover + * 'size' => (int) Height and width in px + * 'colour' => (string) Main colour pallete + * ] + * ``` */ public function renderIcon(array $userOptions): string { @@ -69,6 +77,8 @@ final class IconExtension extends AbstractExtension $rawSvgMarkup = $this->getSvgMarkup($iconFilepath); $cleanSvgMarkup = $this->cleanSvgMarkup($rawSvgMarkup); + $mainColour = $this->getMainPallete($options['colour']); + if ($this->isNonEmptyString($options['title'])) $markup = $this->addTitleToMarkup($cleanSvgMarkup, $options['title']); @@ -112,6 +122,14 @@ final class IconExtension extends AbstractExtension return preg_replace('/.*<\/title>/', '', $markup); } + private function getMainPallete(string $palleteName): array + { + if (array_key_exists($palleteName, $this->palletes)) + return $this->palletes[$palleteName]; + + throw new PalleteNotFound("The pallete '$palleteName' was not found!"); + } + private function isNonEmptyString(mixed $title): bool { if (!is_string($title) && null !== $title) @@ -168,6 +186,8 @@ final class IconExtension extends AbstractExtension class IconNotFound extends \Exception {}; +class PalleteNotFound extends \Exception {}; + // class="stroke-neutral-400 fill-neutral-400" // hover:stroke-ses-highlight diff --git a/tests/Twig/Functions/IconExtensionTest.php b/tests/Twig/Functions/IconExtensionTest.php index 2791ca6..78716af 100644 --- a/tests/Twig/Functions/IconExtensionTest.php +++ b/tests/Twig/Functions/IconExtensionTest.php @@ -4,13 +4,10 @@ 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 Pcm\IconBundle\Twig\Functions\PalleteNotFound; use PHPUnit\Framework\TestCase; -use TypeError; class IconExtensionTest extends TestCase { @@ -20,12 +17,7 @@ class IconExtensionTest extends TestCase 'primary' => [ 'stroke' => 'stroke-primary', 'fill' => 'fill-primary' - ], - - 'white' => [ - 'stroke' => 'stroke-white', - 'fill' => 'fill-white' - ], + ] ]; /** @@ -212,4 +204,11 @@ class IconExtensionTest extends TestCase $this->assertDoesNotMatchRegularExpression('/fill="\s*#black\s*"/', $content); $this->assertDoesNotMatchRegularExpression('/fill="\s*rgb\(0,\s*0,\s*0\)\s*"/', $content); } + + public function testThrowsIfPalleteIsNotFound(): void + { + $this->expectException(PalleteNotFound::class); + $this->icon->renderIcon(['icon' => self::ICON, 'colour' => 'red']); + } + }