Skip to content

Commit 477118c

Browse files
committed
Added Controller Unit tests
1 parent a8d47a4 commit 477118c

File tree

2 files changed

+66
-30
lines changed

2 files changed

+66
-30
lines changed

src/Http/Controllers/ImageController.php

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
namespace le0daniel\Laravel\ImageEngine\Http\Controllers;
1010

11+
use Illuminate\Http\JsonResponse;
1112
use Illuminate\Routing\Controller as BaseController;
12-
use Illuminate\Support\Facades\Log;
1313
use le0daniel\Laravel\ImageEngine\Image\ImageException;
1414
use le0daniel\Laravel\ImageEngine\Image\ImageEngine;
1515
use le0daniel\Laravel\ImageEngine\Utility\SignatureException;
1616
use le0daniel\Laravel\ImageEngine\Utility\Signatures;
1717
use Symfony\Component\HttpFoundation\BinaryFileResponse;
1818

19-
class ImageController extends BaseController
19+
final class ImageController extends BaseController
2020
{
2121
private ImageEngine $imageEngine;
2222

@@ -25,20 +25,22 @@ public function __construct(ImageEngine $imageEngine)
2525
$this->imageEngine = $imageEngine;
2626
}
2727

28-
private function expired()
28+
private function jsonResponse(array $data, int $statusCode = 200): JsonResponse
2929
{
30-
return response()->json(
31-
[
32-
'error' => 'Expired'
33-
],
34-
410
35-
);
30+
return new JsonResponse($data, $statusCode);
31+
}
32+
33+
private function expired(): JsonResponse
34+
{
35+
return $this->jsonResponse(['error' => 'Expired',], 410);
3636
}
3737

3838
public function image(string $folder, string $path, string $extension)
3939
{
4040
try {
41-
$imageRepresentation = $this->imageEngine->getImageFromSignedString($folder . Signatures::SIGNATURE_STRING_SEPARATOR . $path);
41+
$imageRepresentation = $this->imageEngine->getImageFromSignedString(
42+
$folder . Signatures::SIGNATURE_STRING_SEPARATOR . $path
43+
);
4244
if ($imageRepresentation->isExpired) {
4345
return $this->expired();
4446
}
@@ -55,21 +57,9 @@ public function image(string $folder, string $path, string $extension)
5557
$imageRepresentation->cacheControlHeaders()
5658
);
5759
} catch (SignatureException $signatureException) {
58-
Log::error('Image Rendering: ' . $signatureException->getMessage());
59-
return response()->json(
60-
[
61-
'Error' => 'Invalid signature provided',
62-
],
63-
422
64-
);
60+
return $this->jsonResponse(['error' => 'Invalid signature provided'], 422);
6561
} catch (ImageException $error) {
66-
Log::error('Image Rendering: ' . $error->getMessage() . ' => ' . $error->getHint());
67-
return response()->json(
68-
[
69-
'Error' => 'Internal rendering error',
70-
],
71-
500
72-
);
62+
return $this->jsonResponse(['error' => 'Internal rendering error'], 500);
7363
}
7464
}
7565

tests/Http/Controllers/ImageControllerTest.php

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
namespace le0daniel\Tests\Laravel\ImageEngine\Http\Controllers;
44

5+
use Carbon\Carbon;
6+
use Illuminate\Http\JsonResponse;
57
use Intervention\Image\ImageManager;
68
use le0daniel\Laravel\ImageEngine\Http\Controllers\ImageController;
79
use le0daniel\Laravel\ImageEngine\Image\ImageEngine;
10+
use le0daniel\Laravel\ImageEngine\Image\ImageException;
811
use le0daniel\Laravel\ImageEngine\Image\ImageRepresentation;
12+
use le0daniel\Laravel\ImageEngine\Utility\SignatureException;
913
use PHPUnit\Framework\TestCase;
1014
use Prophecy\Argument;
1115
use Prophecy\PhpUnit\ProphecyTrait;
@@ -25,6 +29,12 @@ protected function setUp(): void
2529
$this->imageController = new ImageController(
2630
$this->imageEngine->reveal()
2731
);
32+
33+
$this
34+
->imageEngine
35+
->render(Argument::type(ImageRepresentation::class), 'jpg', false)
36+
->willReturn(test_files('image.jpg'))
37+
;
2838
}
2939

3040
public function testImage()
@@ -33,17 +43,53 @@ public function testImage()
3343
ImageRepresentation::from('path', 'medium')
3444
);
3545

36-
$this
37-
->imageEngine
38-
->render(Argument::type(ImageRepresentation::class), 'jpg', false)
39-
->willReturn(test_files('image.jpg'))
40-
;
41-
4246
/** @var BinaryFileResponse $binaryFileResponse */
4347
$binaryFileResponse = $this->imageController->image('folder', 'string', 'jpg');
4448
$this->assertSame(
4549
test_files('image.jpg'),
4650
$binaryFileResponse->getFile()->getRealPath()
4751
);
4852
}
53+
54+
public function testExpired()
55+
{
56+
$this->imageEngine->getImageFromSignedString('folder::string')->willReturn(
57+
ImageRepresentation::from('path', 'medium', Carbon::now()->subHour())
58+
);
59+
60+
/** @var JsonResponse $jsonResponse */
61+
$jsonResponse = $this->imageController->image('folder', 'string', 'jpg');
62+
$this->assertSame(
63+
410,
64+
$jsonResponse->getStatusCode()
65+
);
66+
}
67+
68+
public function testInvalidSignature()
69+
{
70+
$this->imageEngine->getImageFromSignedString('folder::string')->willThrow(
71+
new SignatureException('invalid sig')
72+
);
73+
74+
/** @var JsonResponse $jsonResponse */
75+
$jsonResponse = $this->imageController->image('folder', 'string', 'jpg');
76+
$this->assertSame(
77+
422,
78+
$jsonResponse->getStatusCode()
79+
);
80+
}
81+
82+
public function testImageExceptionHandling()
83+
{
84+
$this->imageEngine->getImageFromSignedString('folder::string')->willThrow(
85+
ImageException::withHint('test', 'value')
86+
);
87+
88+
/** @var JsonResponse $jsonResponse */
89+
$jsonResponse = $this->imageController->image('folder', 'string', 'jpg');
90+
$this->assertSame(
91+
500,
92+
$jsonResponse->getStatusCode()
93+
);
94+
}
4995
}

0 commit comments

Comments
 (0)