1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-14 01:44:03 +02:00

Add & update abstract class tests

This commit is contained in:
Gwendolen Lynch
2021-12-23 07:04:00 +01:00
parent 5dcd6e820f
commit f031c026ef
7 changed files with 590 additions and 1 deletions

View File

@@ -1,6 +1,6 @@
<?php
namespace Intervention\Image\Tests;
namespace Intervention\Image\Tests\Drivers\Abstract;
use Intervention\Image\Collection;
use Intervention\Image\Drivers\Abstract\AbstractImage;
@@ -11,6 +11,7 @@ use Intervention\Image\Interfaces\EncoderInterface;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Tests\TestCase;
use Mockery;
/**
@@ -89,6 +90,14 @@ class AbstractImageTest extends TestCase
$this->assertEquals(200, $img->getSize()->getHeight());
}
public function testSizeAlias(): void
{
$img = $this->abstractImageMock();
$this->assertInstanceOf(Size::class, $img->getSize());
$this->assertEquals(300, $img->size()->getWidth());
$this->assertEquals(200, $img->size()->getHeight());
}
public function testIsAnimated(): void
{
$img = Mockery::mock(AbstractImage::class, [new Collection()])->makePartial();
@@ -186,6 +195,51 @@ class AbstractImageTest extends TestCase
$this->assertInstanceOf(ImageInterface::class, $result);
}
public function testInvert(): void
{
$img = $this->abstractImageMock();
$modifier = Mockery::mock(ModifierInterface::class);
$modifier->shouldReceive('apply')->with($img)->andReturn($img);
$img->shouldReceive('resolveDriverClass')
->with('Modifiers\InvertModifier')
->andReturn($modifier);
$result = $img->invert();
$this->assertInstanceOf(ImageInterface::class, $result);
}
public function testBrightness(): void
{
$img = $this->abstractImageMock();
$modifier = Mockery::mock(ModifierInterface::class);
$modifier->shouldReceive('apply')->with($img)->andReturn($img);
$img->shouldReceive('resolveDriverClass')
->with('Modifiers\BrightnessModifier', 5)
->andReturn($modifier);
$result = $img->brightness(5);
$this->assertInstanceOf(ImageInterface::class, $result);
}
public function testContrast(): void
{
$img = $this->abstractImageMock();
$modifier = Mockery::mock(ModifierInterface::class);
$modifier->shouldReceive('apply')->with($img)->andReturn($img);
$img->shouldReceive('resolveDriverClass')
->with('Modifiers\ContrastModifier', 5)
->andReturn($modifier);
$result = $img->contrast(5);
$this->assertInstanceOf(ImageInterface::class, $result);
}
public function testBlur(): void
{
$img = $this->abstractImageMock();
@@ -231,6 +285,56 @@ class AbstractImageTest extends TestCase
$this->assertInstanceOf(ImageInterface::class, $result);
}
public function testFill(): void
{
$img = $this->abstractImageMock();
$modifier = Mockery::mock(ModifierInterface::class);
$modifier->shouldReceive('apply')->with($img)->andReturn($img);
$color = Mockery::mock(ColorInterface::class);
$img->shouldReceive('handleInput')
->with('abcdef')
->andReturn($color);
$img->shouldReceive('resolveDriverClass')
->with('Modifiers\FillModifier', $color, null)
->andReturn($modifier);
$result = $img->fill('abcdef');
$this->assertInstanceOf(ImageInterface::class, $result);
}
public function testPixelate(): void
{
$img = $this->abstractImageMock();
$modifier = Mockery::mock(ModifierInterface::class);
$modifier->shouldReceive('apply')->with($img)->andReturn($img);
$img->shouldReceive('resolveDriverClass')
->with('Modifiers\PixelateModifier', 42)
->andReturn($modifier);
$result = $img->pixelate(42);
$this->assertInstanceOf(ImageInterface::class, $result);
}
public function testSharpen(): void
{
$img = $this->abstractImageMock();
$modifier = Mockery::mock(ModifierInterface::class);
$modifier->shouldReceive('apply')->with($img)->andReturn($img);
$img->shouldReceive('resolveDriverClass')
->with('Modifiers\SharpenModifier', 7)
->andReturn($modifier);
$result = $img->sharpen(7);
$this->assertInstanceOf(ImageInterface::class, $result);
}
public function testPickColors(): void
{
$color = Mockery::mock(ColorInterface::class);
@@ -239,4 +343,138 @@ class AbstractImageTest extends TestCase
$result = $img->pickColors(1, 2);
$this->assertInstanceOf(Collection::class, $result);
}
public function testResize(): void
{
$img = $this->abstractImageMock();
$modifier = Mockery::mock(ModifierInterface::class);
$modifier->shouldReceive('apply')->with($img)->andReturn($img);
$img->shouldReceive('resolveDriverClass')
->with('Modifiers\ResizeModifier', 200, 100)
->andReturn($modifier);
$result = $img->resize(200, 100);
$this->assertInstanceOf(ImageInterface::class, $result);
}
public function testResizeDown(): void
{
$img = $this->abstractImageMock();
$modifier = Mockery::mock(ModifierInterface::class);
$modifier->shouldReceive('apply')->with($img)->andReturn($img);
$img->shouldReceive('resolveDriverClass')
->with('Modifiers\ResizeDownModifier', 200, 100)
->andReturn($modifier);
$result = $img->resizeDown(200, 100);
$this->assertInstanceOf(ImageInterface::class, $result);
}
public function testScale(): void
{
$img = $this->abstractImageMock();
$modifier = Mockery::mock(ModifierInterface::class);
$modifier->shouldReceive('apply')->with($img)->andReturn($img);
$img->shouldReceive('resolveDriverClass')
->with('Modifiers\ScaleModifier', 200, 100)
->andReturn($modifier);
$result = $img->scale(200, 100);
$this->assertInstanceOf(ImageInterface::class, $result);
}
public function testScaleDown(): void
{
$img = $this->abstractImageMock();
$modifier = Mockery::mock(ModifierInterface::class);
$modifier->shouldReceive('apply')->with($img)->andReturn($img);
$img->shouldReceive('resolveDriverClass')
->with('Modifiers\ScaleDownModifier', 200, 100)
->andReturn($modifier);
$result = $img->scaleDown(200, 100);
$this->assertInstanceOf(ImageInterface::class, $result);
}
public function testFit(): void
{
$img = $this->abstractImageMock();
$modifier = Mockery::mock(ModifierInterface::class);
$modifier->shouldReceive('apply')->with($img)->andReturn($img);
$img->shouldReceive('resolveDriverClass')
->with('Modifiers\FitModifier', 200, 100, 'center')
->andReturn($modifier);
$result = $img->fit(200, 100, 'center');
$this->assertInstanceOf(ImageInterface::class, $result);
}
public function testFitDown(): void
{
$img = $this->abstractImageMock();
$modifier = Mockery::mock(ModifierInterface::class);
$modifier->shouldReceive('apply')->with($img)->andReturn($img);
$img->shouldReceive('resolveDriverClass')
->with('Modifiers\FitDownModifier', 200, 100, 'center')
->andReturn($modifier);
$result = $img->fitDown(200, 100, 'center');
$this->assertInstanceOf(ImageInterface::class, $result);
}
public function testPad(): void
{
$img = $this->abstractImageMock();
$modifier = Mockery::mock(ModifierInterface::class);
$modifier->shouldReceive('apply')->with($img)->andReturn($img);
$img->shouldReceive('resolveDriverClass')
->with('Modifiers\PadModifier', 200, 100, 'ffffff', 'center')
->andReturn($modifier);
$result = $img->pad(200, 100, 'ffffff', 'center');
$this->assertInstanceOf(ImageInterface::class, $result);
}
public function testPadDown(): void
{
$img = $this->abstractImageMock();
$modifier = Mockery::mock(ModifierInterface::class);
$modifier->shouldReceive('apply')->with($img)->andReturn($img);
$img->shouldReceive('resolveDriverClass')
->with('Modifiers\PadDownModifier', 200, 100, 'ffffff', 'center')
->andReturn($modifier);
$result = $img->padDown(200, 100, 'ffffff', 'center');
$this->assertInstanceOf(ImageInterface::class, $result);
}
public function testDestroy(): void
{
$img = $this->abstractImageMock();
$modifier = Mockery::mock(ModifierInterface::class);
$modifier->shouldReceive('apply')->with($img)->andReturn($img);
$img->shouldReceive('resolveDriverClass')
->with('Modifiers\DestroyModifier')
->andReturn($modifier);
$img->destroy();
}
}

View File

@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Tests\Drivers\Abstract;
use Intervention\Image\Drivers\Abstract\AbstractInputHandler;
use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Tests\TestCase;
use Mockery;
/**
* @covers \Intervention\Image\Drivers\Abstract\AbstractInputHandler
*/
final class AbstractInputHandlerTest extends TestCase
{
public function testHandle(): void
{
$image = Mockery::mock(ImageInterface::class);
$chain = Mockery::mock(AbstractDecoder::class);
$chain->shouldReceive('handle')->with('test image')->andReturn($image);
$chain->shouldReceive('decode')->with('test image')->andReturn(Mockery::mock(ImageInterface::class));
$modifier = $this->getModifier($chain);
$modifier->handle('test image');
}
private function getModifier(AbstractDecoder $chain): AbstractInputHandler
{
return new class ($chain) extends AbstractInputHandler {
public function __construct(private AbstractDecoder $chain)
{
//
}
protected function chain(): AbstractDecoder
{
return $this->chain;
}
};
}
}

View File

@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Tests\Drivers\Abstract\Decoders;
use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Tests\TestCase;
use Mockery;
/**
* @covers \Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder
*/
class AbstractDecoderTest extends TestCase
{
public function testHandle(): void
{
$decoder = Mockery::mock(AbstractDecoder::class)->makePartial();
$decoder->shouldReceive('decode')->with('input string')->andReturn(null);
$decoder->handle('input string');
}
public function testHandleFail(): void
{
$decoder = Mockery::mock(AbstractDecoder::class, [])->makePartial()->shouldAllowMockingProtectedMethods();
$decoder->shouldReceive('decode')->with('input string')->andThrow(DecoderException::class);
$this->expectException(DecoderException::class);
$this->expectExceptionMessage('Unable to decode given input.');
$decoder->handle('input string');
}
public function testHandleFailWithSuccessor(): void
{
$successor = Mockery::mock(AbstractDecoder::class)->makePartial();
$successor->shouldReceive('decode')->with('input string')->andReturn(null);
$decoder = Mockery::mock(AbstractDecoder::class, [$successor])->makePartial()->shouldAllowMockingProtectedMethods();
$decoder->shouldReceive('decode')->with('input string')->andThrow(DecoderException::class);
$decoder->handle('input string');
}
}

View File

@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Tests\Drivers\Abstract\Encoders;
use Intervention\Image\Drivers\Abstract\Encoders\AbstractEncoder;
use Intervention\Image\EncodedImage;
use Intervention\Image\Interfaces\EncoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Tests\TestCase;
/**
* @covers \Intervention\Image\Drivers\Abstract\Encoders\AbstractEncoder
*
* @internal
*/
class AbstractEncoderTest extends TestCase
{
public function testGetBuffered(): void
{
$callback = function () { echo 'hello'; };
static::assertSame('hello', $this->getAbstractEncoder()->getBuffered($callback));
}
public function testSetGetQuality(): void
{
$encoder = $this->getAbstractEncoder();
$encoder->setQuality(55);
static::assertSame(55, $encoder->getQuality());
}
private function getAbstractEncoder(): AbstractEncoder
{
return new class () extends AbstractEncoder implements EncoderInterface {
public function getBuffered(callable $callback): string
{
return parent::getBuffered($callback);
}
public function encode(ImageInterface $image): EncodedImage
{
}
};
}
}

View File

@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Tests\Drivers\Abstract\Modifiers;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractFitModifier;
use Intervention\Image\Drivers\Imagick\ImageFactory;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SizeInterface;
use Intervention\Image\Tests\TestCase;
/**
* @covers \Intervention\Image\Drivers\Abstract\Modifiers\AbstractFitModifier
*/
class AbstractFitModifierTest extends TestCase
{
public function providerCropSize(): iterable
{
yield '150x100' => [150, 100, 50, 100, 50, 0];
yield '100x150' => [100, 150, 75, 150, 13, 0];
}
/** @dataProvider providerCropSize */
public function testGetCropSize(int $width, int $height, int $expectedWidth, int $expectedHeight, int $expectedX, int $expectedY): void
{
$modifier = $this->getModifier(100, 200, 'center');
$image = (new ImageFactory())->newImage($width, $height);
$size = $modifier->getCropSize($image);
static::assertSame($expectedWidth, $size->getWidth());
static::assertSame($expectedHeight, $size->getHeight());
static::assertSame($expectedX, $size->getPivot()->getX());
static::assertSame($expectedY, $size->getPivot()->getY());
}
public function testGetResizeSize(): void
{
$modifier = $this->getModifier(200, 100, 'center');
$image = (new ImageFactory())->newImage(300, 200);
$size = $modifier->getCropSize($image);
$resize = $modifier->getResizeSize($size);
static::assertSame(200, $resize->getWidth());
static::assertSame(100, $resize->getHeight());
static::assertSame(0, $resize->getPivot()->getX());
static::assertSame(0, $resize->getPivot()->getY());
}
private function getModifier(int $width, int $height, string $position): AbstractFitModifier
{
return new class ($width, $height, $position) extends AbstractFitModifier {
public function getCropSize(ImageInterface $image): SizeInterface
{
return parent::getCropSize($image);
}
public function getResizeSize(SizeInterface $size): SizeInterface
{
return parent::getResizeSize($size);
}
};
}
}

View File

@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Tests\Drivers\Abstract\Modifiers;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractPadModifier;
use Intervention\Image\Drivers\Imagick\ImageFactory;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SizeInterface;
use Intervention\Image\Tests\TestCase;
/**
* @covers \Intervention\Image\Drivers\Abstract\Modifiers\AbstractPadModifier
*
* @internal
*/
final class AbstractPadModifierTest extends TestCase
{
public function providerCropSize(): iterable
{
yield '150x100' => [150, 100, 100, 67, 0, 67];
yield '100x150' => [100, 150, 100, 150, 0, 25];
}
/** @dataProvider providerCropSize */
public function testGetCropSize(int $width, int $height, int $expectedWidth, int $expectedHeight, int $expectedX, int $expectedY): void
{
$modifier = $this->getModifier(100, 200, 'ffffff', 'center');
$image = (new ImageFactory())->newImage($width, $height);
$size = $modifier->getCropSize($image);
static::assertSame($expectedWidth, $size->getWidth());
static::assertSame($expectedHeight, $size->getHeight());
static::assertSame($expectedX, $size->getPivot()->getX());
static::assertSame($expectedY, $size->getPivot()->getY());
}
public function testGetResizeSize(): void
{
$modifier = $this->getModifier(200, 100, 'ffffff', 'center');
$image = (new ImageFactory())->newImage(300, 200);
$resize = $modifier->getResizeSize($image);
static::assertSame(200, $resize->getWidth());
static::assertSame(100, $resize->getHeight());
static::assertSame(0, $resize->getPivot()->getX());
static::assertSame(0, $resize->getPivot()->getY());
}
private function getModifier(int $width, int $height, $background, string $position): AbstractPadModifier
{
return new class($width, $height, $background, $position) extends AbstractPadModifier {
public function getCropSize(ImageInterface $image): SizeInterface
{
return parent::getCropSize($image);
}
public function getResizeSize(ImageInterface $image): SizeInterface
{
return parent::getResizeSize($image);
}
};
}
}

View File

@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Tests\Drivers\Abstract\Modifiers;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractRotateModifier;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Exceptions\TypeException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Tests\TestCase;
use Mockery;
/**
* @covers \Intervention\Image\Drivers\Abstract\Modifiers\AbstractRotateModifier
*/
final class AbstractRotateModifierTest extends TestCase
{
public function providerRotationAngle(): iterable
{
yield '0 degrees' => [0.0, 0];
yield '90 degrees' => [90.0, 90];
yield '180 degrees' => [180.0, 180];
yield '270 degrees' => [270.0, 270];
yield '360 degrees' => [0.0, 360];
}
/** @dataProvider providerRotationAngle */
public function testRotationAngle(float $expected, int $angle): void
{
$modifier = $this->getModifier($angle, 'abcdef');
static::assertSame($expected, $modifier->rotationAngle());
}
public function testBackgroundColor(): void
{
$modifier = $this->getModifier(90, 'abcdef');
$color = $modifier->backgroundColor();
static::assertSame(255, $color->red());
}
public function testBackgroundColorInvalidValueThrowsException(): void
{
$this->expectException(TypeException::class);
$this->expectExceptionMessage('Argument #2 must be a color value');
$modifier = $this->getModifier(90, 'bad value');
$modifier->backgroundColor();
}
private function getModifier(float $angle, $background): AbstractRotateModifier
{
return new class ($angle, $background) extends AbstractRotateModifier {
public function rotationAngle(): float
{
return parent::rotationAngle();
}
public function backgroundColor(): ColorInterface
{
return parent::backgroundColor();
}
public function handleInput($input): ImageInterface|ColorInterface
{
if ($this->background === 'bad value') {
throw new DecoderException();
}
$color = Mockery::mock(ColorInterface::class);
$color->shouldReceive('red')->andReturn(255);
return $color;
}
};
}
}