From 91ad1c6f4d90a8545e6d11a90a83ee78a8266ca4 Mon Sep 17 00:00:00 2001 From: Mohammad Khazaee Date: Wed, 3 Aug 2022 14:09:01 +0430 Subject: [PATCH] add test for show --- tests/Feature/FileShowTest.php | 41 ++++++++++++++++--- tests/Feature/Traits/FileImageTrait.php | 22 +++++++++++ tests/Feature/Traits/FileShowTrait.php | 52 +++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 tests/Feature/Traits/FileImageTrait.php create mode 100644 tests/Feature/Traits/FileShowTrait.php diff --git a/tests/Feature/FileShowTest.php b/tests/Feature/FileShowTest.php index 22adbb3..92081cc 100644 --- a/tests/Feature/FileShowTest.php +++ b/tests/Feature/FileShowTest.php @@ -2,19 +2,50 @@ namespace Tests\Feature; +use App\Image\ImageProcessor; +use App\Models\Collection; +use App\Models\File; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; +use Illuminate\Support\Facades\Storage; +use Tests\Feature\Traits\FileImageTrait; +use Tests\Feature\Traits\FileShowTrait; use Tests\TestCase; class FileShowTest extends TestCase { + use FileShowTrait, FileImageTrait; + public function test_user_can_resize_image_with_width_and_height_fields_ok() + { + $models = $this->createCollectionAndFileModelWithImage(); + + $w = rand(10, 2000); + $h = rand(10, 2000); + $canv = rand(0, 1); + + $response = $this->loginAs()->getJson(route('api.files.show', ['collection_name' => $models['collection']->name, 'uuid' => $models['file']->uuid, 'extention' => $models['collection']->ext, 'w' => $w, 'h' => $h, 'canv' => $canv])); + $response->assertOk()->assertHeader('content-type', $response->headers->get('content-type')); + $this->assertEquals($w, getimagesize($response->getFile()->getPathname())[0]); + $this->assertEquals($h, getimagesize($response->getFile()->getPathname())[1]); + + $this->deleteFakeImageForModel($models['file']); + } + /** - * A basic feature test example. - * - * @return void + * @testWith ["test", 4,'1:fgvjjf'] + * ["longer-string", 13,'1:fgvjjf'] */ - public function test_fake() + public function test_user_send_not_currect_value_to_file($w, $h, $r) { - $this->assertTrue(true); + $models = $this->createCollectionAndFileModelWithImage(); + + $canv = rand(0, 1); + + $response = $this->loginAs()->getJson(route('api.files.show', ['collection_name' => $models['collection']->name, 'uuid' => $models['file']->uuid, 'extention' => $models['collection']->ext, 'w' => $w, 'h' => $h, 'canv' => $canv])); + $response->assertOk()->assertHeader('content-type', $response->headers->get('content-type')); + $this->assertNotEquals($w, getimagesize($response->getFile()->getPathname())[0]); + $this->assertNotEquals($h, getimagesize($response->getFile()->getPathname())[1]); + + $this->deleteFakeImageForModel($models['file']); } } diff --git a/tests/Feature/Traits/FileImageTrait.php b/tests/Feature/Traits/FileImageTrait.php new file mode 100644 index 0000000..098ec2a --- /dev/null +++ b/tests/Feature/Traits/FileImageTrait.php @@ -0,0 +1,22 @@ +createFakeImage(storage_path('stub') . '/image.png', Storage::disk($collection->disk)->path($file->server_path . $file->uuid . '.' . $collection->ext)); + } + + public function deleteFakeImageForModel(\App\Models\File $file): void + { + unlink($file->getPath()); + unlink($file->getModifiedPath()); + } +} diff --git a/tests/Feature/Traits/FileShowTrait.php b/tests/Feature/Traits/FileShowTrait.php new file mode 100644 index 0000000..163dcf4 --- /dev/null +++ b/tests/Feature/Traits/FileShowTrait.php @@ -0,0 +1,52 @@ + $image + ]; + $validator = Validator::make($data, [ + 'avatar' => [ + Rule::dimensions()->minWidth($minWidth)->minHeight($minHeight)->maxWidth($maxWidth)->maxHeight($maxHeight), + 'max:' . $maxSize, + 'min:' . $minSize + ], + ]); + + return !$validator->fails(); + } + + public function createCollectionAndFileModelWithImage() + { + $collection = Collection::factory()->createQuietly([ + 'alt_required' => false, + 'description_required' => false, + 'tmp_support' => true, + 'max_width' => 2000, + 'max_height' => 2000, + 'min_width' => 1, + 'min_height' => 1, + 'min_file_size' => 0 + ]); + $uuid = app()->uuid; + $file = File::factory()->createQuietly([ + 'uuid' => $uuid, + 'server_path' => '/' . date('y') . '/' . date('m') . '/', + 'user_id' => auth()->id(), + 'collection_id' => $collection->id + ]); + $this->createImageForModel($collection,$file); + + return ['file' => $file ,'collection' => $collection]; + } +}