Adjust tests to bit boot kernel

This commit is contained in:
Brabli
2022-08-14 18:31:32 +01:00
parent a668ed2b84
commit 55c61558c7
2 changed files with 31 additions and 15 deletions

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Pcm\IconBundle\Twig\Functions;
use InvalidArgumentException;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
@@ -17,7 +18,17 @@ final class IconExtension extends AbstractExtension
'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
@@ -43,9 +54,8 @@ final class IconExtension extends AbstractExtension
$rawSvgMarkup = $this->getSvgMarkup($iconFilepath);
$cleanSvgMarkup = $this->cleanSvgMarkup($rawSvgMarkup);
if ($this->isNonEmptyString($options['title'])) {
if ($this->isNonEmptyString($options['title']))
$markup = $this->addTitleToMarkup($cleanSvgMarkup, $options['title']);
}
if ($options['size'] < 0)
throw new \InvalidArgumentException('Size must not be negative');
@@ -53,10 +63,8 @@ final class IconExtension extends AbstractExtension
if (!is_int($options['size']))
throw new \TypeError('Size value must be an integer');
$markup = $this->setSize($markup, $options['size']);
return $markup;
}

View File

@@ -4,6 +4,7 @@ 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;
@@ -21,9 +22,7 @@ class IconExtensionTest extends TestCase
protected function setUp(): void
{
$kernel = new TestKernel('test', true);
$kernel->boot();
$this->icon = $kernel->getContainer()->get('pcm_icon.icon_extension');
$this->icon = new IconExtension(['tests/icons']);
}
public function testInstanceOf(): void
@@ -31,14 +30,23 @@ class IconExtensionTest extends TestCase
$this->assertInstanceOf(IconExtension::class, $this->icon);
}
public function testDirectoriesArrayGetsInjected(): void
public function testThrowsIfDirectoriesIsEmpty(): void
{
$reflection = new \ReflectionClass($this->icon);
$property = $reflection->getProperty('directories');
$directories = $property->getValue($this->icon);
$this->assertIsArray($directories);
$this->assertCount(1, $directories);
$this->assertStringContainsString('tests/icons', $directories[0]);
$this->expectException(\InvalidArgumentException::class);
new IconExtension([]);
}
public function testThrowsIfDirectoriesContainsNonString(): void
{
$this->expectException(\TypeError::class);
new IconExtension([99]);
}
public function testThrowsIfDirectoriesContainsNonStringAmongStrings(): void
{
$this->expectException(\TypeError::class);
new IconExtension(['string', 99, 'string']);
}
public function testThrowsWhenPassedAnInvalidIconName(): void