Skip to content

Commit 08a589d

Browse files
committed
add tests
1 parent 0e82da5 commit 08a589d

File tree

12 files changed

+195
-1
lines changed

12 files changed

+195
-1
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,16 @@ php artisan vendor:publish --provider="Bst27\ImageProxy\ImageProxyServiceProvide
110110
This will create `config/image-proxy.php`.
111111

112112
---
113+
114+
## Tests
115+
116+
To run tests, you can execute the following command:
117+
118+
```bash
119+
docker run --rm -it \
120+
-u "$(id -u):$(id -g)" \
121+
-v "$PWD":/var/www/html \
122+
-w /var/www/html \
123+
laravelsail/php84-composer:latest \
124+
php vendor/bin/phpunit
125+
```

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"psr-4": {
3232
"Workbench\\App\\": "workbench/app/",
3333
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
34-
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
34+
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/",
35+
"Bst27\\ImageProxy\\Tests\\": "tests/"
3536
}
3637
},
3738
"scripts": {

phpunit.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
colors="true"
4+
stopOnFailure="false">
5+
<testsuites>
6+
<testsuite name="Unit">
7+
<directory>./tests/Unit</directory>
8+
</testsuite>
9+
<testsuite name="Feature">
10+
<directory>./tests/Feature</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Bst27\ImageProxy\Tests\Feature;
4+
5+
use Bst27\ImageProxy\Tests\TestCase;
6+
7+
class ImageProxyControllerTest extends TestCase
8+
{
9+
public function testServeFilenameRejectsWrongFilename()
10+
{
11+
$good = proxy_image('images/60x40.png', 'default', 'bar.png', []);
12+
$bad = preg_replace('/bar\.png$/', 'baz.png', $good);
13+
14+
$this->get($bad)->assertStatus(404);
15+
$this->get($good)->assertStatus(200);
16+
}
17+
18+
public function testServeShortRejectsWrongExtension()
19+
{
20+
$good = proxy_image('images/60x40.png');
21+
$bad = preg_replace('/\.png$/', '.dat', $good);
22+
23+
$this->get($bad)->assertStatus(404);
24+
$this->get($good)->assertStatus(200);
25+
}
26+
27+
public function testInvalidTokenReturns404()
28+
{
29+
$this->get('/img/invalid-token-123.png')->assertStatus(404);
30+
}
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Bst27\ImageProxy\Tests\Feature;
4+
5+
use Bst27\ImageProxy\Tests\TestCase;
6+
7+
class ProxyImageHelperTest extends TestCase
8+
{
9+
public function testHelperAndDefaultManipulatorIntegration()
10+
{
11+
$url = proxy_image('images/60x40.jpg', 'default', false, []);
12+
13+
$response = $this->get($url);
14+
15+
$response->assertStatus(200);
16+
$response->assertHeader('Content-Type', 'image/jpeg');
17+
$this->assertNotEmpty($response->streamedContent());
18+
}
19+
}

tests/Fixtures/images/60x40.jpg

429 Bytes
Loading

tests/Fixtures/images/60x40.png

581 Bytes
Loading

tests/TestCase.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Bst27\ImageProxy\Tests;
4+
5+
use Bst27\ImageProxy\ImageProxyServiceProvider;
6+
use Orchestra\Testbench\TestCase as OrchestraTestCase;
7+
8+
abstract class TestCase extends OrchestraTestCase
9+
{
10+
protected function getPackageProviders($app): array
11+
{
12+
return [
13+
ImageProxyServiceProvider::class,
14+
];
15+
}
16+
17+
protected function getEnvironmentSetUp($app): void
18+
{
19+
// Use local disk for source images, pointing at tests/Fixtures
20+
$app['config']->set('image-proxy.disks.source', 'fixtures');
21+
$app['config']->set('filesystems.disks.fixtures', [
22+
'driver' => 'local',
23+
'root' => __DIR__ . '/Fixtures',
24+
]);
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Bst27\ImageProxy\Tests\Unit;
4+
5+
use Bst27\ImageProxy\Services\Base64UrlTokenEncoder;
6+
use Bst27\ImageProxy\Tests\TestCase;
7+
8+
class Base64UrlTokenEncoderTest extends TestCase
9+
{
10+
public function testEncodeDecodeRoundtrip()
11+
{
12+
$encoder = new Base64UrlTokenEncoder();
13+
$plain = 'hello-world-123';
14+
$encoded = $encoder->encode($plain);
15+
16+
$this->assertNotEmpty($encoded);
17+
$this->assertNotSame($plain, $encoded);
18+
19+
$decoded = $encoder->decode($encoded);
20+
$this->assertSame($plain, $decoded);
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Bst27\ImageProxy\Tests\Unit;
4+
5+
use Bst27\ImageProxy\Services\ImageManipulator\DefaultManipulator;
6+
use Bst27\ImageProxy\Tests\TestCase;
7+
use Illuminate\Support\Facades\Storage;
8+
9+
class DefaultManipulatorTest extends TestCase
10+
{
11+
public function testManipulateReturnsCompressedPng()
12+
{
13+
$binary = Storage::disk('fixtures')->get('images/60x40.png');
14+
15+
$manipulator = new DefaultManipulator();
16+
$result = $manipulator->manipulate($binary, []);
17+
18+
$this->assertIsString($result);
19+
$this->assertNotEmpty($result);
20+
$this->assertStringStartsWith("\x89PNG", $result);
21+
}
22+
}

0 commit comments

Comments
 (0)