Start writing unit tests
This commit is contained in:
@@ -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' => '']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user