From 13c166d104ced1c3bc5293cee8a774103128b264 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Tue, 11 Jun 2024 16:15:55 +0200 Subject: [PATCH] Throw exception if font file is not found --- src/Typography/Font.php | 5 +++++ tests/Unit/Typography/FontFactoryTest.php | 10 +++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Typography/Font.php b/src/Typography/Font.php index e8a098e4..58400485 100644 --- a/src/Typography/Font.php +++ b/src/Typography/Font.php @@ -72,10 +72,15 @@ class Font implements FontInterface /** * {@inheritdoc} * + * @throws FontException * @see FontInterface::setFilename() */ public function setFilename(string $filename): FontInterface { + if (!file_exists($filename)) { + throw new FontException('Font file ' . $filename . ' does not exist.'); + } + $this->filename = $filename; return $this; diff --git a/tests/Unit/Typography/FontFactoryTest.php b/tests/Unit/Typography/FontFactoryTest.php index 826970a9..7afd9e80 100644 --- a/tests/Unit/Typography/FontFactoryTest.php +++ b/tests/Unit/Typography/FontFactoryTest.php @@ -13,17 +13,17 @@ final class FontFactoryTest extends BaseTestCase { public function testBuildWithFont(): void { - $factory = new FontFactory(new Font('foo.ttf')); + $font_file = $this->getTestResourcePath('test.ttf'); + $factory = new FontFactory(new Font($font_file)); $result = $factory(); $this->assertInstanceOf(FontInterface::class, $result); - $this->assertEquals('foo.ttf', $result->filename()); + $this->assertEquals($font_file, $result->filename()); } public function testBuildWithCallback(): void { $factory = new FontFactory(function (FontFactory $font) { - $font->filename('foo.ttf'); - $font->file('bar.ttf'); + $font->filename($this->getTestResourcePath('test.ttf')); $font->color('#b01735'); $font->size(70); $font->align('center'); @@ -36,7 +36,7 @@ final class FontFactoryTest extends BaseTestCase $result = $factory(); $this->assertInstanceOf(FontInterface::class, $result); - $this->assertEquals('bar.ttf', $result->filename()); + $this->assertEquals($this->getTestResourcePath('test.ttf'), $result->filename()); $this->assertEquals('#b01735', $result->color()); $this->assertEquals(70, $result->size()); $this->assertEquals('center', $result->alignment());