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

Change return type of AbstractDecoder::handle()

This commit is contained in:
Oliver Vogel
2022-05-22 10:53:42 +02:00
parent 1cfbdf8d24
commit 4847f2c5fc
2 changed files with 18 additions and 12 deletions

View File

@@ -15,16 +15,16 @@ abstract class AbstractDecoder
// //
} }
final public function handle($input): null|ImageInterface|ColorInterface final public function handle($input): ImageInterface|ColorInterface
{ {
try { try {
$decoded = $this->decode($input); $decoded = $this->decode($input);
} catch (DecoderException $e) { } catch (DecoderException $e) {
if ($this->hasSuccessor()) { if (!$this->hasSuccessor()) {
return $this->successor->handle($input); $this->fail();
} }
$this->fail(); return $this->successor->handle($input);
} }
return $decoded; return $decoded;

View File

@@ -6,6 +6,7 @@ namespace Intervention\Image\Tests\Drivers\Abstract\Decoders;
use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder; use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder;
use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Tests\TestCase; use Intervention\Image\Tests\TestCase;
use Mockery; use Mockery;
@@ -16,31 +17,36 @@ class AbstractDecoderTest extends TestCase
{ {
public function testHandle(): void public function testHandle(): void
{ {
$result = Mockery::mock(ColorInterface::class);
$decoder = Mockery::mock(AbstractDecoder::class)->makePartial(); $decoder = Mockery::mock(AbstractDecoder::class)->makePartial();
$decoder->shouldReceive('decode')->with('input string')->andReturn(null); $decoder->shouldReceive('decode')->with('test input')->andReturn($result);
$decoder->handle('input string'); $decoder->handle('test input');
} }
public function testHandleFail(): void public function testHandleFail(): void
{ {
$decoder = Mockery::mock(AbstractDecoder::class, [])->makePartial()->shouldAllowMockingProtectedMethods(); $decoder = Mockery::mock(AbstractDecoder::class, [])->makePartial()->shouldAllowMockingProtectedMethods();
$decoder->shouldReceive('decode')->with('input string')->andThrow(DecoderException::class); $decoder->shouldReceive('decode')->with('test input')->andThrow(DecoderException::class);
$this->expectException(DecoderException::class); $this->expectException(DecoderException::class);
$this->expectExceptionMessage('Unable to decode given input.'); $this->expectExceptionMessage('Unable to decode given input.');
$decoder->handle('input string'); $decoder->handle('test input');
} }
public function testHandleFailWithSuccessor(): void public function testHandleFailWithSuccessor(): void
{ {
$result = Mockery::mock(ColorInterface::class);
$successor = Mockery::mock(AbstractDecoder::class)->makePartial(); $successor = Mockery::mock(AbstractDecoder::class)->makePartial();
$successor->shouldReceive('decode')->with('input string')->andReturn(null); $successor->shouldReceive('decode')->with('test input')->andReturn($result);
$decoder = Mockery::mock(AbstractDecoder::class, [$successor])->makePartial()->shouldAllowMockingProtectedMethods(); $decoder = Mockery::mock(
$decoder->shouldReceive('decode')->with('input string')->andThrow(DecoderException::class); AbstractDecoder::class,
[$successor]
)->makePartial()->shouldAllowMockingProtectedMethods();
$decoder->shouldReceive('decode')->with('test input')->andThrow(DecoderException::class);
$decoder->handle('input string'); $decoder->handle('test input');
} }
} }