From c2d1e848be61224d0abd87e9ab2b073f228d604c Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Tue, 16 Jan 2024 09:56:52 +0100 Subject: [PATCH] Add tests --- src/Drivers/AbstractDecoder.php | 6 +-- tests/Drivers/AbstractDecoderTest.php | 55 ++++++++++++++++++++++ tests/Drivers/AbstractInputHandlerTest.php | 11 +++++ tests/Typography/FontFactoryTest.php | 3 +- 4 files changed, 71 insertions(+), 4 deletions(-) diff --git a/src/Drivers/AbstractDecoder.php b/src/Drivers/AbstractDecoder.php index 9ffc394c..f17f4c97 100644 --- a/src/Drivers/AbstractDecoder.php +++ b/src/Drivers/AbstractDecoder.php @@ -106,15 +106,15 @@ abstract class AbstractDecoder extends DriverSpecialized implements DecoderInter /** * Parse data uri * - * @param mixed $value + * @param mixed $input * @return object */ - protected function parseDataUri($value): object + protected function parseDataUri(mixed $input): object { $pattern = "/^data:(?P\w+\/[-+.\w]+)?" . "(?P(;[-\w]+=[-\w]+)*)(?P;base64)?,(?P.*)/"; - $result = preg_match($pattern, $value, $matches); + $result = preg_match($pattern, $input, $matches); return new class ($matches, $result) { diff --git a/tests/Drivers/AbstractDecoderTest.php b/tests/Drivers/AbstractDecoderTest.php index 6ad61e87..7dfb505f 100644 --- a/tests/Drivers/AbstractDecoderTest.php +++ b/tests/Drivers/AbstractDecoderTest.php @@ -4,9 +4,11 @@ declare(strict_types=1); namespace Intervention\Image\Tests\Drivers; +use Exception; use Intervention\Image\Drivers\AbstractDecoder; use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\ColorInterface; +use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Tests\TestCase; use Mockery; @@ -48,4 +50,57 @@ class AbstractDecoderTest extends TestCase $decoder->handle('test input'); } + + public function testParseDataUri(): void + { + $decoder = new class () extends AbstractDecoder + { + public function parse(mixed $input): object + { + return parent::parseDataUri($input); + } + + public function decode(mixed $input): ImageInterface|ColorInterface + { + throw new Exception(''); + } + }; + + $result = $decoder->parse( + 'data:image/gif;base64,R0lGODdhAwADAKIAAAQyrKTy/ByS7AQytLT2/AAAAAAAAAAAACwAAAAAAwADAAADBhgU0gMgAQA7' + ); + + $this->assertTrue($result->isValid()); + $this->assertEquals('image/gif', $result->mediaType()); + $this->assertTrue($result->hasMediaType()); + $this->assertTrue($result->isBase64Encoded()); + $this->assertEquals([], $result->parameters()); + $this->assertEquals( + 'R0lGODdhAwADAKIAAAQyrKTy/ByS7AQytLT2/AAAAAAAAAAAACwAAAAAAwADAAADBhgU0gMgAQA7', + $result->data() + ); + } + + public function testIsValidBase64(): void + { + $decoder = new class () extends AbstractDecoder + { + public function isValid(mixed $input): bool + { + return parent::isValidBase64($input); + } + + public function decode(mixed $input): ImageInterface|ColorInterface + { + throw new Exception(''); + } + }; + + $this->assertTrue( + $decoder->isValid('R0lGODdhAwADAKIAAAQyrKTy/ByS7AQytLT2/AAAAAAAAAAAACwAAAAAAwADAAADBhgU0gMgAQA7') + ); + $this->assertFalse( + $decoder->isValid('foo') + ); + } } diff --git a/tests/Drivers/AbstractInputHandlerTest.php b/tests/Drivers/AbstractInputHandlerTest.php index 86b4496c..150d26ca 100644 --- a/tests/Drivers/AbstractInputHandlerTest.php +++ b/tests/Drivers/AbstractInputHandlerTest.php @@ -6,6 +6,7 @@ namespace Intervention\Image\Tests\Drivers; use Intervention\Image\Drivers\AbstractDecoder; use Intervention\Image\Drivers\AbstractInputHandler; +use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Tests\TestCase; use Mockery; @@ -27,6 +28,16 @@ final class AbstractInputHandlerTest extends TestCase $modifier->handle('test image'); } + public function testChainNoItems(): void + { + $handler = new class () extends AbstractInputHandler + { + }; + + $this->expectException(DecoderException::class); + $handler->handle('test'); + } + private function getModifier(AbstractDecoder $chain): AbstractInputHandler { return new class ([$chain]) extends AbstractInputHandler diff --git a/tests/Typography/FontFactoryTest.php b/tests/Typography/FontFactoryTest.php index 7f319227..08614702 100644 --- a/tests/Typography/FontFactoryTest.php +++ b/tests/Typography/FontFactoryTest.php @@ -21,6 +21,7 @@ class FontFactoryTest extends TestCase { $factory = new FontFactory(function ($font) { $font->filename('foo.ttf'); + $font->file('bar.ttf'); $font->color('#b01735'); $font->size(70); $font->align('center'); @@ -31,7 +32,7 @@ class FontFactoryTest extends TestCase $result = $factory(); $this->assertInstanceOf(FontInterface::class, $result); - $this->assertEquals('foo.ttf', $result->filename()); + $this->assertEquals('bar.ttf', $result->filename()); $this->assertEquals('#b01735', $result->color()); $this->assertEquals(70, $result->size()); $this->assertEquals('center', $result->alignment());