Validate args
This commit is contained in:
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pcm\IconBundle\Twig\Functions;
|
namespace Pcm\IconBundle\Twig\Functions;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use Twig\Extension\AbstractExtension;
|
use Twig\Extension\AbstractExtension;
|
||||||
use Twig\TwigFunction;
|
use Twig\TwigFunction;
|
||||||
@@ -18,7 +19,7 @@ final class IconExtension extends AbstractExtension
|
|||||||
'size' => self::DEFAULT_SIZE
|
'size' => self::DEFAULT_SIZE
|
||||||
];
|
];
|
||||||
|
|
||||||
public function __construct(private array $directories)
|
public function __construct(private array $directories, private array $palletes)
|
||||||
{
|
{
|
||||||
if (empty($this->directories))
|
if (empty($this->directories))
|
||||||
throw new InvalidArgumentException('Directories array must contain at least one path!');
|
throw new InvalidArgumentException('Directories array must contain at least one path!');
|
||||||
@@ -28,6 +29,21 @@ final class IconExtension extends AbstractExtension
|
|||||||
|
|
||||||
if ($dirsContainNonString)
|
if ($dirsContainNonString)
|
||||||
throw new \TypeError('Directories array must only contain strings!');
|
throw new \TypeError('Directories array must only contain strings!');
|
||||||
|
|
||||||
|
if (empty($this->palletes))
|
||||||
|
throw new InvalidArgumentException('Palletes array must contain at least one pallet!');
|
||||||
|
|
||||||
|
$pelletesContainNonarray = array_reduce($this->palletes,
|
||||||
|
fn($notArray, $path) => $notArray || !is_array($path));
|
||||||
|
|
||||||
|
if ($pelletesContainNonarray)
|
||||||
|
throw new \TypeError('Palletes array must only contain arrays!');
|
||||||
|
|
||||||
|
foreach ($this->palletes as $pallete) {
|
||||||
|
if (!(array_key_exists('stroke', $pallete) && array_key_exists('fill', $pallete))) {
|
||||||
|
throw new \Exception('Palletes must contain a "stroke" and "fill" key!');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -10,11 +10,24 @@ use Pcm\IconBundle\Tests\TestKernel;
|
|||||||
use Pcm\IconBundle\Twig\Functions\IconExtension;
|
use Pcm\IconBundle\Twig\Functions\IconExtension;
|
||||||
use Pcm\IconBundle\Twig\Functions\IconNotFound;
|
use Pcm\IconBundle\Twig\Functions\IconNotFound;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use TypeError;
|
||||||
|
|
||||||
class IconExtensionTest extends TestCase
|
class IconExtensionTest extends TestCase
|
||||||
{
|
{
|
||||||
private const ICON = 'test';
|
private const ICON = 'test';
|
||||||
|
|
||||||
|
private const PALLETES = [
|
||||||
|
'primary' => [
|
||||||
|
'stroke' => 'stroke-primary',
|
||||||
|
'fill' => 'fill-primary'
|
||||||
|
],
|
||||||
|
|
||||||
|
'white' => [
|
||||||
|
'stroke' => 'stroke-white',
|
||||||
|
'fill' => 'fill-white'
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var IconExtension
|
* @var IconExtension
|
||||||
*/
|
*/
|
||||||
@@ -22,7 +35,7 @@ class IconExtensionTest extends TestCase
|
|||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->icon = new IconExtension(['tests/icons']);
|
$this->icon = new IconExtension(['tests/icons'], self::PALLETES);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInstanceOf(): void
|
public function testInstanceOf(): void
|
||||||
@@ -33,20 +46,19 @@ class IconExtensionTest extends TestCase
|
|||||||
public function testThrowsIfDirectoriesIsEmpty(): void
|
public function testThrowsIfDirectoriesIsEmpty(): void
|
||||||
{
|
{
|
||||||
$this->expectException(\InvalidArgumentException::class);
|
$this->expectException(\InvalidArgumentException::class);
|
||||||
new IconExtension([]);
|
new IconExtension([], self::PALLETES);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testThrowsIfDirectoriesContainsNonString(): void
|
public function testThrowsIfDirectoriesContainsNonString(): void
|
||||||
{
|
{
|
||||||
$this->expectException(\TypeError::class);
|
$this->expectException(\TypeError::class);
|
||||||
new IconExtension([99]);
|
new IconExtension([99], self::PALLETES);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function testThrowsIfDirectoriesContainsNonStringAmongStrings(): void
|
public function testThrowsIfDirectoriesContainsNonStringAmongStrings(): void
|
||||||
{
|
{
|
||||||
$this->expectException(\TypeError::class);
|
$this->expectException(\TypeError::class);
|
||||||
new IconExtension(['string', 99, 'string']);
|
new IconExtension(['string', 99, 'string'], self::PALLETES);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testThrowsWhenPassedAnInvalidIconName(): void
|
public function testThrowsWhenPassedAnInvalidIconName(): void
|
||||||
@@ -152,4 +164,34 @@ class IconExtensionTest extends TestCase
|
|||||||
$timesMatched = preg_match_all($heightRegex, $content);
|
$timesMatched = preg_match_all($heightRegex, $content);
|
||||||
$this->assertSame(1, $timesMatched);
|
$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' => '']]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user