Throw specific error if file is found to be empty

This commit is contained in:
2024-01-11 16:22:14 +00:00
parent a19f1f4438
commit 3da5e00361
2 changed files with 12 additions and 0 deletions

View File

@@ -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']);

View File

@@ -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]);