Start writing unit tests
This commit is contained in:
@@ -7,8 +7,10 @@ services:
|
|||||||
alias: Pcm\IconBundle\Twig\Functions\IconExtension
|
alias: Pcm\IconBundle\Twig\Functions\IconExtension
|
||||||
public: true
|
public: true
|
||||||
|
|
||||||
|
|
||||||
Pcm\IconBundle\Twig\Functions\IconExtension:
|
Pcm\IconBundle\Twig\Functions\IconExtension:
|
||||||
|
tags: ['twig.extension']
|
||||||
public: false
|
public: false
|
||||||
arguments:
|
arguments:
|
||||||
# $dir:
|
$directories:
|
||||||
# - '%kernel.project_dir%/public/icons'
|
- '%kernel.project_dir%/public/icons'
|
||||||
|
|||||||
@@ -4,22 +4,98 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pcm\IconBundle\Twig\Functions;
|
namespace Pcm\IconBundle\Twig\Functions;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use InvalidArgumentException;
|
||||||
use Twig\Extension\AbstractExtension;
|
use Twig\Extension\AbstractExtension;
|
||||||
use Twig\TwigFunction;
|
use Twig\TwigFunction;
|
||||||
|
use TypeError;
|
||||||
|
|
||||||
final class IconExtension extends AbstractExtension
|
final class IconExtension extends AbstractExtension
|
||||||
{
|
{
|
||||||
|
public function __construct(private array $directories) {}
|
||||||
|
|
||||||
|
private const DEFAULT_OPTIONS = [
|
||||||
|
'icon' => null,
|
||||||
|
'title' => null,
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
public function getFunctions(): array
|
public function getFunctions(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
new TwigFunction('icon', [$this, 'render'], [
|
new TwigFunction('icon', [$this, 'renderIcon'], [
|
||||||
'is_safe' => ['html']
|
'is_safe' => ['html']
|
||||||
])
|
])
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $options
|
||||||
|
*/
|
||||||
|
public function renderIcon(array $userOptions): string
|
||||||
|
{
|
||||||
|
$options = $this->mergeUserOptionsWithDefaults($userOptions);
|
||||||
|
|
||||||
|
$iconFilepath = $this->findSvgFilepath($options['icon']);
|
||||||
|
$rawSvgMarkup = $this->getSvgMarkup($iconFilepath);
|
||||||
|
$cleanSvgMarkup = $this->cleanSvgMarkup($rawSvgMarkup);
|
||||||
|
|
||||||
|
if ($this->titleIsANonEmptyString($options['title'])) {
|
||||||
|
$markup = $this->addTitleToMarkup($cleanSvgMarkup, $options['title']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $markup;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function mergeUserOptionsWithDefaults(array $userOptions): array
|
||||||
|
{
|
||||||
|
return array_merge(self::DEFAULT_OPTIONS, $userOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function findSvgFilepath(string $iconName): string
|
||||||
|
{
|
||||||
|
foreach ($this->directories as $directory) {
|
||||||
|
$potentialFilepath = sprintf('%s/%s.svg', $directory, $iconName);
|
||||||
|
if (file_exists($potentialFilepath)) {
|
||||||
|
return $potentialFilepath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IconNotFound(sprintf('File "%s.svg" not found in %s', $iconName, implode(', ', $this->directories)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getSvgMarkup(string $filepath): string
|
||||||
|
{
|
||||||
|
return file_get_contents($filepath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function cleanSvgMarkup(string $markup): string
|
||||||
|
{
|
||||||
|
return preg_replace('/<title>.*<\/title>/', '', $markup);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function titleIsANonEmptyString(mixed $title): bool
|
||||||
|
{
|
||||||
|
if (!is_string($title) && null !== $title)
|
||||||
|
throw new TypeError('Title must be a string!');
|
||||||
|
|
||||||
|
|
||||||
|
if ('' === $title)
|
||||||
|
throw new InvalidArgumentException('Title string must not be empty!');
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function addTitleToMarkup(string $markup, ?string $title): string
|
||||||
|
{
|
||||||
|
if (null === $title) {
|
||||||
|
return $markup;
|
||||||
|
}
|
||||||
|
|
||||||
|
return preg_replace('/(<svg(.|\n)*?>\n?)/', "$1<title>$title</title>", $markup);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class IconNotFound extends Exception {};
|
||||||
|
|||||||
32
tests/AppKernel.php
Normal file
32
tests/AppKernel.php
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pcm\IconBundle\Tests;
|
||||||
|
|
||||||
|
use Pcm\IconBundle\PcmIconBundle;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
|
||||||
|
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||||
|
use Symfony\Component\HttpKernel\Kernel;
|
||||||
|
|
||||||
|
class AppKernel extends Kernel
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function registerBundles(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
new FrameworkBundle(),
|
||||||
|
new PcmIconBundle()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param LoaderInterface $loader
|
||||||
|
*/
|
||||||
|
public function registerContainerConfiguration(LoaderInterface $loader): void
|
||||||
|
{
|
||||||
|
$loader->load(__DIR__.'/services_'.$this->getEnvironment().'.yml');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,11 +4,15 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pcm\IconBundle\Tests\Twig\Functions;
|
namespace Pcm\IconBundle\Tests\Twig\Functions;
|
||||||
|
|
||||||
|
use Pcm\IconBundle\Tests\AppKernel;
|
||||||
use Pcm\IconBundle\Twig\Functions\IconExtension;
|
use Pcm\IconBundle\Twig\Functions\IconExtension;
|
||||||
|
use Pcm\IconBundle\Twig\Functions\IconNotFound;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
class IconExtensionTest extends TestCase
|
class IconExtensionTest extends TestCase
|
||||||
{
|
{
|
||||||
|
private const ICON = 'test';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var IconExtension
|
* @var IconExtension
|
||||||
*/
|
*/
|
||||||
@@ -16,7 +20,9 @@ class IconExtensionTest extends TestCase
|
|||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->icon = new IconExtension();
|
$kernel = new AppKernel('test', false);
|
||||||
|
$kernel->boot();
|
||||||
|
$this->icon = $kernel->getContainer()->get('pcm_icon.icon_extension');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInstanceOf(): void
|
public function testInstanceOf(): void
|
||||||
@@ -24,4 +30,45 @@ class IconExtensionTest extends TestCase
|
|||||||
$this->assertInstanceOf(IconExtension::class, $this->icon);
|
$this->assertInstanceOf(IconExtension::class, $this->icon);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDirectoriesArrayGetsInjected(): 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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testThrowsWhenPassedAnInvalidIconName(): void
|
||||||
|
{
|
||||||
|
$this->expectException(IconNotFound::class);
|
||||||
|
$this->icon->renderIcon(['icon' => random_bytes(8)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNoTitleExistsIfNotPassedIn(): void
|
||||||
|
{
|
||||||
|
$content = $this->icon->renderIcon(['icon' => self::ICON]);
|
||||||
|
$this->assertStringNotContainsString('<title>', $content);
|
||||||
|
$this->assertStringNotContainsString('</title>', $content);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testTitleGetsAddedIfSpecified(): void
|
||||||
|
{
|
||||||
|
$title = 'test_title';
|
||||||
|
$content = $this->icon->renderIcon(['icon' => self::ICON, 'title' => $title]);
|
||||||
|
$this->assertMatchesRegularExpression("/<title>{$title}<\/title>/", $content);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testTitleThrowsIfNotPassedAString(): void
|
||||||
|
{
|
||||||
|
$this->expectException(\TypeError::class);
|
||||||
|
$this->icon->renderIcon(['icon' => self::ICON, 'title' => 99]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testTitleThrowsIfPassedAnEmptyString():void
|
||||||
|
{
|
||||||
|
$this->expectException(\InvalidArgumentException::class);
|
||||||
|
$this->icon->renderIcon(['icon' => self::ICON, 'title' => '']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
5
tests/icons/test.svg
Normal file
5
tests/icons/test.svg
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" width="512" height="512" viewBox="96 96 320 320">
|
||||||
|
<title>Some cross lol</title>
|
||||||
|
<line x1="256" y1="112" x2="256" y2="400" style="fill: none; stroke: rgb(0,0, 0); stroke-linecap: round; stroke-linejoin: round; stroke-width: 32px;"></line>
|
||||||
|
<line x1="400" y1="256" x2="112" y2="256" style="fill: none; stroke: #000; stroke-linecap: round; stroke-linejoin: round; stroke-width: 32px;"></line>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 448 B |
13
tests/services_test.yml
Normal file
13
tests/services_test.yml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# This config is only here to stop a deprecation notice for Symfony 7
|
||||||
|
framework:
|
||||||
|
http_method_override: false
|
||||||
|
|
||||||
|
services:
|
||||||
|
pcm_icon.icon_extension:
|
||||||
|
alias: Pcm\IconBundle\Twig\Functions\IconExtension
|
||||||
|
public: true
|
||||||
|
|
||||||
|
Pcm\IconBundle\Twig\Functions\IconExtension:
|
||||||
|
arguments:
|
||||||
|
$directories:
|
||||||
|
- '%kernel.project_dir%/tests/icons'
|
||||||
Reference in New Issue
Block a user