diff --git a/src/Twig/Runtime/IconRuntime.php b/src/Twig/Runtime/IconRuntime.php index e2c92df..086f844 100644 --- a/src/Twig/Runtime/IconRuntime.php +++ b/src/Twig/Runtime/IconRuntime.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Pcm\IconBundle\Twig\Runtime; use Pcm\IconBundle\Exception\ColourNotFound; +use Pcm\IconBundle\Exception\EmptyFileException; use Pcm\IconBundle\Exception\IconNotFound; use Twig\Extension\RuntimeExtensionInterface; @@ -44,6 +45,10 @@ final class IconRuntime implements RuntimeExtensionInterface $svg = $this->getSvg($options['icon']); + if (empty($svg)) { + throw new EmptyFileException(\sprintf("The file %s.svg was found, but it was empty!", $options['icon'])); + } + $this->sanitiseSvg($svg); $colourClasses = $this->getColourClasses($options['colour'], $options['hover']); diff --git a/tests/Twig/Runtime/IconRuntimeTest.php b/tests/Twig/Runtime/IconRuntimeTest.php index 0ea2abf..9ebfc05 100644 --- a/tests/Twig/Runtime/IconRuntimeTest.php +++ b/tests/Twig/Runtime/IconRuntimeTest.php @@ -6,6 +6,7 @@ namespace Pcm\IconBundle\Tests\Twig\Functions; use Pcm\IconBundle\DependencyInjection\Configuration; use Pcm\IconBundle\Exception\ColourNotFound; +use Pcm\IconBundle\Exception\EmptyFileException; use Pcm\IconBundle\Exception\IconNotFound; use Pcm\IconBundle\Twig\Runtime\IconRuntime; use PHPUnit\Framework\TestCase; @@ -50,6 +51,12 @@ class IconRuntimeTest extends TestCase $this->icon->renderIcon(['icon' => random_bytes(8)]); } + public function testThrowsIfPassedInAnEmptyFile(): void + { + $this->expectException(EmptyFileException::class); + $this->icon->renderIcon(['icon' => 'empty']); + } + public function testNoTitleExistsIfNotPassedIn(): void { $content = $this->icon->renderIcon(['icon' => self::ICON]);