Browse Source

add test for show

pull/2/head
Mohammad Khazaee 2 years ago
parent
commit
91ad1c6f4d
  1. 41
      tests/Feature/FileShowTest.php
  2. 22
      tests/Feature/Traits/FileImageTrait.php
  3. 52
      tests/Feature/Traits/FileShowTrait.php

41
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']);
}
}

22
tests/Feature/Traits/FileImageTrait.php

@ -0,0 +1,22 @@
<?php
namespace Tests\Feature\Traits;
use App\Image\ImageProcessor;
use Illuminate\Support\Facades\Storage;
trait FileImageTrait
{
public function createImageForModel(\App\Models\Collection $collection, \App\Models\File $file): void
{
$imageProcessor = new ImageProcessor;
$imageProcessor->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());
}
}

52
tests/Feature/Traits/FileShowTrait.php

@ -0,0 +1,52 @@
<?php
namespace Tests\Feature\Traits;
use App\Models\Collection;
use App\Models\File;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
trait FileShowTrait
{
use FileImageTrait;
public function dimensions(\Illuminate\Http\File $image, $minWidth, $minHeight, $maxWidth, $maxHeight, $maxSize, $minSize) : bool
{
$data = [
'image' => $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];
}
}
Loading…
Cancel
Save