Test for palletes not existing
This commit is contained in:
@@ -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>.*<\/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
|
||||
|
||||
@@ -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']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user