1
0
mirror of https://github.com/Intervention/image.git synced 2025-01-17 20:28:21 +01:00

Add tests

This commit is contained in:
Oliver Vogel 2024-01-16 10:29:23 +01:00
parent f305cbd3bc
commit 828e3a204e
25 changed files with 362 additions and 14 deletions

View File

@ -3,9 +3,9 @@
namespace Intervention\Image\Tests\Drivers\Gd\Decoders;
use Intervention\Image\Drivers\Gd\Decoders\Base64ImageDecoder;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Image;
use Intervention\Image\Tests\TestCase;
use Intervention\Image\Tests\Traits\CanCreateGdTestImage;
/**
* @requires extension gd
@ -13,14 +13,25 @@ use Intervention\Image\Tests\Traits\CanCreateGdTestImage;
*/
class Base64ImageDecoderTest extends TestCase
{
use CanCreateGdTestImage;
protected Base64ImageDecoder $decoder;
public function setUp(): void
{
$this->decoder = new Base64ImageDecoder();
}
public function testDecode(): void
{
$decoder = new Base64ImageDecoder();
$result = $decoder->decode(
$result = $this->decoder->decode(
base64_encode($this->getTestImageData('blue.gif'))
);
$this->assertInstanceOf(Image::class, $result);
}
public function testDecoderInvalid(): void
{
$this->expectException(DecoderException::class);
$this->decoder->decode('test');
}
}

View File

@ -3,9 +3,10 @@
namespace Intervention\Image\Tests\Drivers\Gd\Decoders;
use Intervention\Image\Drivers\Gd\Decoders\DataUriImageDecoder;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Image;
use Intervention\Image\Tests\TestCase;
use Intervention\Image\Tests\Traits\CanCreateGdTestImage;
use stdClass;
/**
* @requires extension gd
@ -13,14 +14,37 @@ use Intervention\Image\Tests\Traits\CanCreateGdTestImage;
*/
class DataUriImageDecoderTest extends TestCase
{
use CanCreateGdTestImage;
protected DataUriImageDecoder $decoder;
public function setUp(): void
{
$this->decoder = new DataUriImageDecoder();
}
public function testDecode(): void
{
$decoder = new DataUriImageDecoder();
$result = $decoder->decode(
$result = $this->decoder->decode(
sprintf('data:image/jpeg;base64,%s', base64_encode($this->getTestImageData('blue.gif')))
);
$this->assertInstanceOf(Image::class, $result);
}
public function testDecoderNonString(): void
{
$this->expectException(DecoderException::class);
$this->decoder->decode(new stdClass());
}
public function testDecoderInvalid(): void
{
$this->expectException(DecoderException::class);
$this->decoder->decode('invalid');
}
public function testDecoderNonImage(): void
{
$this->expectException(DecoderException::class);
$this->decoder->decode('data:text/plain;charset=utf-8,test');
}
}

View File

@ -3,9 +3,10 @@
namespace Intervention\Image\Tests\Drivers\Gd\Decoders;
use Intervention\Image\Drivers\Gd\Decoders\FilePathImageDecoder;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Image;
use Intervention\Image\Tests\TestCase;
use Intervention\Image\Tests\Traits\CanCreateGdTestImage;
use stdClass;
/**
* @requires extension gd
@ -13,14 +14,37 @@ use Intervention\Image\Tests\Traits\CanCreateGdTestImage;
*/
class FilePathImageDecoderTest extends TestCase
{
use CanCreateGdTestImage;
protected FilePathImageDecoder $decoder;
public function setUp(): void
{
$this->decoder = new FilePathImageDecoder();
}
public function testDecode(): void
{
$decoder = new FilePathImageDecoder();
$result = $decoder->decode(
$result = $this->decoder->decode(
$this->getTestImagePath()
);
$this->assertInstanceOf(Image::class, $result);
}
public function testDecoderNonString(): void
{
$this->expectException(DecoderException::class);
$this->decoder->decode(new stdClass());
}
public function testDecoderNoPath(): void
{
$this->expectException(DecoderException::class);
$this->decoder->decode('no-path');
}
public function testDecoderTooLongPath(): void
{
$this->expectException(DecoderException::class);
$this->decoder->decode(str_repeat('x', PHP_MAXPATHLEN + 1));
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Intervention\Image\Tests\Drivers\Gd\Decoders;
use Intervention\Image\Drivers\Gd\Decoders\SplFileInfoImageDecoder;
use Intervention\Image\Image;
use Intervention\Image\Tests\TestCase;
use SplFileInfo;
/**
* @requires extension gd
* @covers \Intervention\Image\Drivers\Gd\Decoders\SplFileInfoImageDecoder
*/
class SplFileInfoImageDecoderTest extends TestCase
{
public function testDecode(): void
{
$decoder = new SplFileInfoImageDecoder();
$result = $decoder->decode(
new SplFileInfo($this->getTestImagePath('blue.gif'))
);
$this->assertInstanceOf(Image::class, $result);
}
}

View File

@ -11,6 +11,7 @@ use Intervention\Image\Tests\TestCase;
/**
* @requires extension gd
* @covers \Intervention\Image\Encoders\AvifEncoder
* @covers \Intervention\Image\Drivers\Gd\Encoders\AvifEncoder
*/
class AvifEncoderTest extends TestCase

View File

@ -11,6 +11,7 @@ use Intervention\Image\Tests\TestCase;
/**
* @requires extension gd
* @covers \Intervention\Image\Encoders\BmpEncoder
* @covers \Intervention\Image\Drivers\Gd\Encoders\BmpEncoder
*/
class BmpEncoderTest extends TestCase

View File

@ -12,6 +12,7 @@ use Intervention\Image\Tests\Traits\CanCreateGdTestImage;
/**
* @requires extension gd
* @covers \Intervention\Image\Encoders\GifEncoder
* @covers \Intervention\Image\Drivers\Gd\Encoders\GifEncoder
*/
class GifEncoderTest extends TestCase

View File

@ -11,6 +11,7 @@ use Intervention\Image\Tests\TestCase;
/**
* @requires extension gd
* @covers \Intervention\Image\Encoders\JpegEncoder
* @covers \Intervention\Image\Drivers\Gd\Encoders\JpegEncoder
*/
class JpegEncoderTest extends TestCase

View File

@ -12,6 +12,7 @@ use Intervention\Image\Tests\Traits\CanCreateGdTestImage;
/**
* @requires extension gd
* @covers \Intervention\Image\Encoders\PngEncoder
* @covers \Intervention\Image\Drivers\Gd\Encoders\PngEncoder
*/
class PngEncoderTest extends TestCase

View File

@ -13,6 +13,8 @@ use Intervention\Image\Tests\TestCase;
/**
* @requires extension gd
* @covers \Intervention\Image\Encoders\WebpEncoder
* @covers \Intervention\Image\Drivers\Gd\Encoders\WebpEncoder
*/
final class WebpEncoderTest extends TestCase
{

View File

@ -7,7 +7,7 @@ use Intervention\Image\Tests\TestCase;
use Intervention\Image\Tests\Traits\CanCreateGdTestImage;
/**
* @requires extension imagick
* @requires extension gd
* @covers \Intervention\Image\Modifiers\ContainModifier
* @covers \Intervention\Image\Drivers\Gd\Modifiers\ContainModifier
*/

View File

@ -0,0 +1,37 @@
<?php
namespace Intervention\Image\Tests\Drivers\Imagick\Decoders;
use Intervention\Image\Drivers\Imagick\Decoders\Base64ImageDecoder;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Image;
use Intervention\Image\Tests\TestCase;
/**
* @requires extension imagick
* @covers \Intervention\Image\Drivers\Imagick\Decoders\Base64ImageDecoder
*/
class Base64ImageDecoderTest extends TestCase
{
protected Base64ImageDecoder $decoder;
public function setUp(): void
{
$this->decoder = new Base64ImageDecoder();
}
public function testDecode(): void
{
$result = $this->decoder->decode(
base64_encode($this->getTestImageData('blue.gif'))
);
$this->assertInstanceOf(Image::class, $result);
}
public function testDecoderInvalid(): void
{
$this->expectException(DecoderException::class);
$this->decoder->decode('test');
}
}

View File

@ -5,8 +5,10 @@ namespace Intervention\Image\Tests\Drivers\Imagick\Decoders;
use Intervention\Image\Colors\Cmyk\Colorspace as CmykColorspace;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Drivers\Imagick\Decoders\BinaryImageDecoder;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Image;
use Intervention\Image\Tests\TestCase;
use stdClass;
class BinaryImageDecoderTest extends TestCase
{
@ -59,4 +61,11 @@ class BinaryImageDecoderTest extends TestCase
$this->assertInstanceOf(Image::class, $image);
$this->assertInstanceOf(CmykColorspace::class, $image->colorspace());
}
public function testDecodeNonString(): void
{
$decoder = new BinaryImageDecoder();
$this->expectException(DecoderException::class);
$decoder->decode(new stdClass());
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace Intervention\Image\Tests\Drivers\Imagick\Decoders;
use Intervention\Image\Drivers\Imagick\Decoders\DataUriImageDecoder;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Image;
use Intervention\Image\Tests\TestCase;
use stdClass;
/**
* @requires extension imagick
* @covers \Intervention\Image\Drivers\Imagick\Decoders\DataUriImageDecoder
*/
class DataUriImageDecoderTest extends TestCase
{
protected DataUriImageDecoder $decoder;
public function setUp(): void
{
$this->decoder = new DataUriImageDecoder();
}
public function testDecode(): void
{
$result = $this->decoder->decode(
sprintf('data:image/jpeg;base64,%s', base64_encode($this->getTestImageData('blue.gif')))
);
$this->assertInstanceOf(Image::class, $result);
}
public function testDecoderNonString(): void
{
$this->expectException(DecoderException::class);
$this->decoder->decode(new stdClass());
}
public function testDecoderInvalid(): void
{
$this->expectException(DecoderException::class);
$this->decoder->decode('invalid');
}
public function testDecoderNonImage(): void
{
$this->expectException(DecoderException::class);
$this->decoder->decode('data:text/plain;charset=utf-8,test');
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace Intervention\Image\Tests\Drivers\Imagick\Decoders;
use Intervention\Image\Drivers\Imagick\Decoders\FilePathImageDecoder;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Image;
use Intervention\Image\Tests\TestCase;
use stdClass;
/**
* @requires extension imagick
* @covers \Intervention\Image\Drivers\Imagick\Decoders\FilePathImageDecoder
*/
class FilePathImageDecoderTest extends TestCase
{
protected FilePathImageDecoder $decoder;
public function setUp(): void
{
$this->decoder = new FilePathImageDecoder();
}
public function testDecode(): void
{
$result = $this->decoder->decode(
$this->getTestImagePath()
);
$this->assertInstanceOf(Image::class, $result);
}
public function testDecoderNonString(): void
{
$this->expectException(DecoderException::class);
$this->decoder->decode(new stdClass());
}
public function testDecoderNoPath(): void
{
$this->expectException(DecoderException::class);
$this->decoder->decode('no-path');
}
public function testDecoderTooLongPath(): void
{
$this->expectException(DecoderException::class);
$this->decoder->decode(str_repeat('x', PHP_MAXPATHLEN + 1));
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Intervention\Image\Tests\Drivers\Imagick\Decoders;
use Intervention\Image\Drivers\Imagick\Decoders\SplFileInfoImageDecoder;
use Intervention\Image\Image;
use Intervention\Image\Tests\TestCase;
use SplFileInfo;
/**
* @requires extension imagick
* @covers \Intervention\Image\Drivers\Imagick\Decoders\SplFileInfoImageDecoder
*/
class SplFileInfoImageDecoderTest extends TestCase
{
public function testDecode(): void
{
$decoder = new SplFileInfoImageDecoder();
$result = $decoder->decode(
new SplFileInfo($this->getTestImagePath('blue.gif'))
);
$this->assertInstanceOf(Image::class, $result);
}
}

View File

@ -12,6 +12,7 @@ use Intervention\Image\Tests\TestCase;
/**
* @requires extension imagick
* @covers \Intervention\Image\Encoders\AvifEncoder
* @covers \Intervention\Image\Drivers\Imagick\Encoders\AvifEncoder
*/
class AvifEncoderTest extends TestCase

View File

@ -13,7 +13,8 @@ use Intervention\Image\Tests\Traits\CanCreateImagickTestImage;
/**
* @requires extension imagick
* @covers \Intervention\Image\Drivers\Imagick\Encoders\JpegEncoder
* @covers \Intervention\Image\Encoders\BmpEncoder
* @covers \Intervention\Image\Drivers\Imagick\Encoders\BmpEncoder
*/
class BmpEncoderTest extends TestCase
{

View File

@ -13,6 +13,7 @@ use Intervention\Image\Tests\Traits\CanCreateImagickTestImage;
/**
* @requires extension imagick
* @covers \Intervention\Image\Encoders\GifEncoder
* @covers \Intervention\Image\Drivers\Imagick\Encoders\GifEncoder
*/
class GifEncoderTest extends TestCase

View File

@ -15,6 +15,8 @@ use Intervention\Image\Tests\Traits\CanCreateImagickTestImage;
/**
* @requires extension imagick
* @covers \Intervention\Image\Encoders\HeicEncoder
* @covers \Intervention\Image\Drivers\Imagick\Encoders\HeicEncoder
*/
final class HeicEncoderTest extends TestCase
{

View File

@ -0,0 +1,38 @@
<?php
namespace Intervention\Image\Tests\Drivers\Imagick\Encoders;
use Imagick;
use ImagickPixel;
use Intervention\Image\Drivers\Imagick\Core;
use Intervention\Image\Drivers\Imagick\Driver;
use Intervention\Image\Encoders\Jpeg2000Encoder;
use Intervention\Image\Image;
use Intervention\Image\Tests\TestCase;
/**
* @requires extension imagick
* @covers \Intervention\Image\Encoders\Jpeg2000Encoder
* @covers \Intervention\Image\Drivers\Imagick\Encoders\Jpeg2000Encoder
*/
class Jpeg2000EncoderTest extends TestCase
{
protected function getTestImage(): Image
{
$imagick = new Imagick();
$imagick->newImage(3, 2, new ImagickPixel('red'), 'png');
return new Image(
new Driver(),
new Core($imagick)
);
}
public function testEncode(): void
{
$image = $this->getTestImage();
$encoder = new Jpeg2000Encoder(75);
$result = $encoder->encode($image);
$this->assertMediaType('image/jp2', (string) $result);
}
}

View File

@ -12,6 +12,7 @@ use Intervention\Image\Tests\TestCase;
/**
* @requires extension imagick
* @covers \Intervention\Image\Encoders\JpegEncoder
* @covers \Intervention\Image\Drivers\Imagick\Encoders\JpegEncoder
*/
class JpegEncoderTest extends TestCase

View File

@ -15,6 +15,8 @@ use Intervention\Image\Tests\Traits\CanCreateImagickTestImage;
/**
* @requires extension imagick
* @covers \Intervention\Image\Encoders\PngEncoder
* @covers \Intervention\Image\Drivers\Imagick\Encoders\PngEncoder
*/
final class PngEncoderTest extends TestCase
{

View File

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Tests\Drivers\Imagick\Encoders;
use Imagick;
use ImagickPixel;
use Intervention\Image\Drivers\Imagick\Core;
use Intervention\Image\Drivers\Imagick\Driver;
use Intervention\Image\Encoders\TiffEncoder;
use Intervention\Image\Image;
use Intervention\Image\Tests\TestCase;
/**
* @requires extension imagick
* @covers \Intervention\Image\Encoders\TiffEncoder
* @covers \Intervention\Image\Drivers\Imagick\Encoders\TiffEncoder
*/
final class TiffEncoderTest extends TestCase
{
protected function getTestImage(): Image
{
$imagick = new Imagick();
$imagick->newImage(3, 2, new ImagickPixel('red'), 'png');
return new Image(
new Driver(),
new Core($imagick)
);
}
public function testEncode(): void
{
$image = $this->getTestImage();
$encoder = new TiffEncoder();
$result = $encoder->encode($image);
$this->assertMediaType('image/tiff', (string) $result);
}
}

View File

@ -14,6 +14,8 @@ use Intervention\Image\Tests\TestCase;
/**
* @requires extension imagick
* @covers \Intervention\Image\Encoders\WebpEncoder
* @covers \Intervention\Image\Drivers\Imagick\Encoders\WebpEncoder
*/
final class WebpEncoderTest extends TestCase
{