Skip to content

Commit eb3e7a8

Browse files
committed
Added new Unit tests
1 parent 9a26209 commit eb3e7a8

File tree

12 files changed

+122
-21
lines changed

12 files changed

+122
-21
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"ext-json": "*"
3434
},
3535
"require-dev": {
36-
"phpunit/phpunit": "^9.2"
36+
"phpunit/phpunit": "^9.2",
37+
"phpspec/prophecy-phpunit": "^2.0"
3738
}
3839
}

src/Commands/ImageUrl.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public function handle()
4242
$disk
4343
);
4444

45-
if (!$image->disk()->exists($path)) {
45+
$disk = Storage::disk($image->diskName);
46+
if (!$disk->exists($path)) {
4647
$this->line('Image not found. Path ' . $path . ' on disk ' . $disk, 'error');
4748
return;
4849
}

src/Http/Controllers/ImageController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use le0daniel\Laravel\ImageEngine\Image\ImageException;
1414
use le0daniel\Laravel\ImageEngine\Image\ImageEngine;
1515
use le0daniel\Laravel\ImageEngine\Utility\SignatureException;
16+
use le0daniel\Laravel\ImageEngine\Utility\Signatures;
1617

1718
class ImageController extends BaseController
1819
{
@@ -36,7 +37,7 @@ private function expired()
3637
public function image(string $folder, string $path, string $extension)
3738
{
3839
try {
39-
$imageRepresentation = $this->imageEngine->getImageSignedString($folder . '::' . $path);
40+
$imageRepresentation = $this->imageEngine->getImageFromSignedString($folder . Signatures::SIGNATURE_STRING_SEPARATOR . $path);
4041
if ($imageRepresentation->isExpired) {
4142
return $this->expired();
4243
}

src/Image/ImageEngine.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ final class ImageEngine
2727
private string $tmpPath;
2828
private ImageManager $imageManager;
2929

30-
public function __construct(ImageManager $imageManager)
30+
public function __construct(ImageManager $imageManager, string $secret, string $tmpPath)
3131
{
3232
$this->imageManager = $imageManager;
33-
$this->secret = config('image-engine.key');
34-
$this->tmpPath = rtrim(config('image-engine.tmp_directory'), '/');
33+
$this->secret = $secret;
34+
$this->tmpPath = rtrim($tmpPath, '/');
3535
}
3636

3737
private function getBasePath(ImageRepresentation $imageRepresentation, bool $forceConfidential): string
@@ -64,7 +64,7 @@ private function getAbsoluteRenderPath(
6464
bool $forceConfidential
6565
): string {
6666
$basePath = $this->getBasePath($imageRepresentation, $forceConfidential);
67-
[$folder, $path] = $this->serializeImage($imageRepresentation);
67+
[$folder, $path] = $this->serializeAndSignImage($imageRepresentation);
6868
return "{$basePath}/{$folder}/{$path}.{$extension}";
6969
}
7070

@@ -74,13 +74,13 @@ private function getTmpPath(string $extension)
7474
return "{$this->tmpPath}/{$randomName}.{$extension}";
7575
}
7676

77-
public function serializeImage(ImageRepresentation $imageRepresentation): array
77+
public function serializeAndSignImage(ImageRepresentation $imageRepresentation): array
7878
{
7979
$signature = Signatures::sign($this->secret, $imageRepresentation->serialize());
80-
return explode('::', $signature, 2);
80+
return explode(Signatures::SIGNATURE_STRING_SEPARATOR, $signature, 2);
8181
}
8282

83-
public function getImageSignedString(string $signedString): ImageRepresentation
83+
public function getImageFromSignedString(string $signedString): ImageRepresentation
8484
{
8585
$payload = Signatures::verifyAndReturnPayloadString($this->secret, $signedString);
8686
return ImageRepresentation::fromSerialized($payload);

src/Image/ImageRepresentation.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626
class ImageRepresentation
2727
{
28-
private const DEFAULT_DISK_NAME = 'local';
28+
public const DEFAULT_DISK_NAME = 'local';
2929
private const SERIALIZE_KEY_MAP = [
3030
'filePath' => 'p',
3131
'size' => 's',
@@ -131,11 +131,6 @@ public function __get(string $name)
131131
: $this->getValue($name, null);
132132
}
133133

134-
public function disk(): Filesystem
135-
{
136-
return Storage::disk($this->diskName);
137-
}
138-
139134
public function toArray()
140135
{
141136
$array = Arrays::removeNullValues(

src/ImageEngineProvider.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace le0daniel\Laravel\ImageEngine;
1010

1111
use Illuminate\Support\ServiceProvider;
12+
use Intervention\Image\ImageManager;
1213
use le0daniel\Laravel\ImageEngine\Commands\FilesystemClear;
1314
use le0daniel\Laravel\ImageEngine\Commands\ImageUrl;
1415
use le0daniel\Laravel\ImageEngine\Image\ImageEngine;
@@ -22,6 +23,17 @@ public function register()
2223
__DIR__ . '/config/image-engine.php',
2324
'image-engine'
2425
);
26+
27+
$this->app->singleton(
28+
ImageEngine::class,
29+
static function ($container) {
30+
return new ImageEngine(
31+
$container->make(ImageManager::class),
32+
config('image-engine.key'),
33+
config('image-engine.tmp_directory')
34+
);
35+
}
36+
);
2537
}
2638

2739
public function boot()

src/Utility/Images.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private static function isLocalDisk(Filesystem $disk): bool
4040

4141
public static function downloadForLocalProcessing(ImageRepresentation $imageRepresentation): string
4242
{
43-
$disk = $imageRepresentation->disk();
43+
$disk = Storage::disk($imageRepresentation->diskName);
4444

4545
// Return path from local disk
4646
if (self::isLocalDisk($disk)) {

src/Utility/Signatures.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44

55
final class Signatures
66
{
7-
private const SIGNATURE_REGEX = '/^[^:]*::[a-zA-Z0-9\-\_]+$/';
7+
public const SIGNATURE_STRING_SEPARATOR = '::';
8+
private const SIGNATURE_REGEX = '/^[^:]*' . self::SIGNATURE_STRING_SEPARATOR . '[a-zA-Z0-9\-\_]+$/';
89

910
public static function sign(string $secret, string $stringToSign): string
1011
{
1112
$signature = self::calculateSignature($secret, $stringToSign);
12-
return $stringToSign . '::' . Base64::urlEncode($signature);
13+
return $stringToSign . self::SIGNATURE_STRING_SEPARATOR . Base64::urlEncode($signature);
1314
}
1415

1516
public static function verifyAndReturnPayloadString(string $secret, string $signedString): string
1617
{
1718
self::verifyStructure($signedString);
18-
[$payload, $userProvidedSignature] = explode('::', $signedString, 2);
19+
[$payload, $userProvidedSignature] = explode(self::SIGNATURE_STRING_SEPARATOR, $signedString, 2);
1920
self::verifySignature($secret, $payload, $userProvidedSignature);
2021
return $payload;
2122
}

src/helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
if (!function_exists('image_url')) {
1414
function image_url(ImageRepresentation $image, string $extension): string
1515
{
16-
[$folder, $path] = app()->make(ImageEngine::class)->serializeImage($image);
16+
[$folder, $path] = app()->make(ImageEngine::class)->serializeAndSignImage($image);
1717
return URL::route(
1818
'image-engine.image',
1919
[

tests/Image/ImageEngineTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace le0daniel\Tests\Laravel\ImageEngine\Image;
4+
5+
use Intervention\Image\ImageManager;
6+
use le0daniel\Laravel\ImageEngine\Image\ImageEngine;
7+
use le0daniel\Laravel\ImageEngine\Image\ImageRepresentation;
8+
use PHPUnit\Framework\TestCase;
9+
use Prophecy\PhpUnit\ProphecyTrait;
10+
11+
final class ImageEngineTest extends TestCase
12+
{
13+
use ProphecyTrait;
14+
private const SECRET = 'asdfasdf!?';
15+
private $imageManager;
16+
private ImageEngine $imageEngine;
17+
18+
protected function setUp(): void
19+
{
20+
$this->imageManager = $this->prophesize(ImageManager::class);
21+
$this->imageEngine = new ImageEngine(
22+
$this->imageManager->reveal(),
23+
self::SECRET,
24+
test_files('')
25+
);
26+
}
27+
28+
public function testSerializeAndSignImage()
29+
{
30+
[$serialized, $signature] = $this->imageEngine->serializeAndSignImage(ImageRepresentation::from(
31+
'test',
32+
'medium'
33+
));
34+
$this->assertEquals('eyJzIjoibWVkaXVtIiwicCI6InRlc3QifQ', $serialized);
35+
$this->assertEquals('P6ZVsi5HxRKd412m87AITMlde88iOjc76YIAOBpvP4o', $signature);
36+
}
37+
38+
public function testGetImageFromSignedString()
39+
{
40+
$image = $this->imageEngine->getImageFromSignedString(
41+
'eyJzIjoibWVkaXVtIiwicCI6InRlc3QifQ::P6ZVsi5HxRKd412m87AITMlde88iOjc76YIAOBpvP4o'
42+
);
43+
$this->assertEquals('test', $image->filePath);
44+
$this->assertEquals('medium', $image->size);
45+
$this->assertEquals(ImageRepresentation::DEFAULT_DISK_NAME, $image->diskName);
46+
$this->assertFalse($image->isConfidential);
47+
}
48+
}

0 commit comments

Comments
 (0)