mirror of
https://github.com/Intervention/image.git
synced 2025-08-14 01:44:03 +02:00
Add tests
This commit is contained in:
@@ -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<mediatype>\w+\/[-+.\w]+)?" .
|
||||
"(?P<parameters>(;[-\w]+=[-\w]+)*)(?P<base64>;base64)?,(?P<data>.*)/";
|
||||
|
||||
$result = preg_match($pattern, $value, $matches);
|
||||
$result = preg_match($pattern, $input, $matches);
|
||||
|
||||
return new class ($matches, $result)
|
||||
{
|
||||
|
@@ -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')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
|
@@ -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());
|
||||
|
Reference in New Issue
Block a user