Start writing unit tests
This commit is contained in:
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;
|
||||
|
||||
use Pcm\IconBundle\Tests\AppKernel;
|
||||
use Pcm\IconBundle\Twig\Functions\IconExtension;
|
||||
use Pcm\IconBundle\Twig\Functions\IconNotFound;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class IconExtensionTest extends TestCase
|
||||
{
|
||||
private const ICON = 'test';
|
||||
|
||||
/**
|
||||
* @var IconExtension
|
||||
*/
|
||||
@@ -16,7 +20,9 @@ class IconExtensionTest extends TestCase
|
||||
|
||||
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
|
||||
@@ -24,4 +30,45 @@ class IconExtensionTest extends TestCase
|
||||
$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