Adjust tests to bit boot kernel
This commit is contained in:
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pcm\IconBundle\Twig\Functions;
|
namespace Pcm\IconBundle\Twig\Functions;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
use Twig\Extension\AbstractExtension;
|
use Twig\Extension\AbstractExtension;
|
||||||
use Twig\TwigFunction;
|
use Twig\TwigFunction;
|
||||||
|
|
||||||
@@ -17,7 +18,17 @@ final class IconExtension extends AbstractExtension
|
|||||||
'size' => self::DEFAULT_SIZE
|
'size' => self::DEFAULT_SIZE
|
||||||
];
|
];
|
||||||
|
|
||||||
public function __construct(private array $directories, private array $palletes) {}
|
public function __construct(private array $directories)
|
||||||
|
{
|
||||||
|
if (empty($this->directories))
|
||||||
|
throw new InvalidArgumentException('Directories array must contain at least one path!');
|
||||||
|
|
||||||
|
$dirsContainNonString = array_reduce($this->directories,
|
||||||
|
fn($notString, $path) => $notString || !is_string($path));
|
||||||
|
|
||||||
|
if ($dirsContainNonString)
|
||||||
|
throw new \TypeError('Directories array must only contain strings!');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
@@ -43,9 +54,8 @@ final class IconExtension extends AbstractExtension
|
|||||||
$rawSvgMarkup = $this->getSvgMarkup($iconFilepath);
|
$rawSvgMarkup = $this->getSvgMarkup($iconFilepath);
|
||||||
$cleanSvgMarkup = $this->cleanSvgMarkup($rawSvgMarkup);
|
$cleanSvgMarkup = $this->cleanSvgMarkup($rawSvgMarkup);
|
||||||
|
|
||||||
if ($this->isNonEmptyString($options['title'])) {
|
if ($this->isNonEmptyString($options['title']))
|
||||||
$markup = $this->addTitleToMarkup($cleanSvgMarkup, $options['title']);
|
$markup = $this->addTitleToMarkup($cleanSvgMarkup, $options['title']);
|
||||||
}
|
|
||||||
|
|
||||||
if ($options['size'] < 0)
|
if ($options['size'] < 0)
|
||||||
throw new \InvalidArgumentException('Size must not be negative');
|
throw new \InvalidArgumentException('Size must not be negative');
|
||||||
@@ -53,10 +63,8 @@ final class IconExtension extends AbstractExtension
|
|||||||
if (!is_int($options['size']))
|
if (!is_int($options['size']))
|
||||||
throw new \TypeError('Size value must be an integer');
|
throw new \TypeError('Size value must be an integer');
|
||||||
|
|
||||||
|
|
||||||
$markup = $this->setSize($markup, $options['size']);
|
$markup = $this->setSize($markup, $options['size']);
|
||||||
|
|
||||||
|
|
||||||
return $markup;
|
return $markup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pcm\IconBundle\Tests\Twig\Functions;
|
namespace Pcm\IconBundle\Tests\Twig\Functions;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
use Pcm\IconBundle\DependencyInjection\PcmIconExtension;
|
use Pcm\IconBundle\DependencyInjection\PcmIconExtension;
|
||||||
use Pcm\IconBundle\Tests\TestKernel;
|
use Pcm\IconBundle\Tests\TestKernel;
|
||||||
use Pcm\IconBundle\Twig\Functions\IconExtension;
|
use Pcm\IconBundle\Twig\Functions\IconExtension;
|
||||||
@@ -21,9 +22,7 @@ class IconExtensionTest extends TestCase
|
|||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$kernel = new TestKernel('test', true);
|
$this->icon = new IconExtension(['tests/icons']);
|
||||||
$kernel->boot();
|
|
||||||
$this->icon = $kernel->getContainer()->get('pcm_icon.icon_extension');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInstanceOf(): void
|
public function testInstanceOf(): void
|
||||||
@@ -31,14 +30,23 @@ class IconExtensionTest extends TestCase
|
|||||||
$this->assertInstanceOf(IconExtension::class, $this->icon);
|
$this->assertInstanceOf(IconExtension::class, $this->icon);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDirectoriesArrayGetsInjected(): void
|
public function testThrowsIfDirectoriesIsEmpty(): void
|
||||||
{
|
{
|
||||||
$reflection = new \ReflectionClass($this->icon);
|
$this->expectException(\InvalidArgumentException::class);
|
||||||
$property = $reflection->getProperty('directories');
|
new IconExtension([]);
|
||||||
$directories = $property->getValue($this->icon);
|
}
|
||||||
$this->assertIsArray($directories);
|
|
||||||
$this->assertCount(1, $directories);
|
public function testThrowsIfDirectoriesContainsNonString(): void
|
||||||
$this->assertStringContainsString('tests/icons', $directories[0]);
|
{
|
||||||
|
$this->expectException(\TypeError::class);
|
||||||
|
new IconExtension([99]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testThrowsIfDirectoriesContainsNonStringAmongStrings(): void
|
||||||
|
{
|
||||||
|
$this->expectException(\TypeError::class);
|
||||||
|
new IconExtension(['string', 99, 'string']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testThrowsWhenPassedAnInvalidIconName(): void
|
public function testThrowsWhenPassedAnInvalidIconName(): void
|
||||||
|
|||||||
Reference in New Issue
Block a user