You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
3.8 KiB
122 lines
3.8 KiB
2 years ago
|
<?php
|
||
|
|
||
|
namespace Tests\Feature;
|
||
|
|
||
|
use Illuminate\Http\UploadedFile;
|
||
|
use Illuminate\Support\Facades\Config;
|
||
|
use Illuminate\Support\Facades\Storage;
|
||
|
use Illuminate\Testing\Fluent\AssertableJson;
|
||
|
use Symfony\Component\HttpFoundation\Response as ResponseAlias;
|
||
|
use Tests\TestCase;
|
||
|
|
||
|
/**
|
||
|
* Тестируем контроллер ADMIN
|
||
|
*/
|
||
|
class AdminTest extends TestCase
|
||
|
{
|
||
|
/**
|
||
|
* Префикс галерей на диске
|
||
|
* @var string
|
||
|
*/
|
||
|
private $prefixGallery = null;
|
||
|
|
||
|
/**
|
||
|
* Используем локальное хранилище, вместо вызова удаленного s3
|
||
|
* @return void
|
||
|
*/
|
||
|
protected function setUp(): void
|
||
|
{
|
||
|
parent::setUp();
|
||
|
Config::set('filesystems.default', 'local');
|
||
|
$this->prefixGallery = env("GALLERY_PREFIX", "/gallery/");
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Тестируем создание галереи
|
||
|
* @test
|
||
|
* @return void
|
||
|
*/
|
||
|
public function createGallery(): string
|
||
|
{
|
||
|
$response = $this->post(route("admin:gallery:create"));
|
||
|
$response->assertStatus(ResponseAlias::HTTP_OK);
|
||
|
$response->assertJson(function (AssertableJson $json) {
|
||
|
$json
|
||
|
->where('name', function (string $name) {
|
||
|
return strlen($name) === 32;
|
||
|
})
|
||
|
->where('files', null);
|
||
|
});
|
||
|
self::assertTrue(Storage::exists($this->prefixGallery . $response->json('name')));
|
||
|
return $response->json('name');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Тестируем список галерей, проверяем наличие созданной
|
||
|
* @test
|
||
|
* @depends createGallery
|
||
|
* @param string $galleryId
|
||
|
* @return void
|
||
|
*/
|
||
|
public function listGallery(string $galleryId): void
|
||
|
{
|
||
|
$response = $this->get(route("admin:gallery:list"));
|
||
|
$response->assertStatus(ResponseAlias::HTTP_OK);
|
||
|
$response->assertJsonFragment(['name' => $galleryId, 'files' => null]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Тестируем загрузку файла
|
||
|
* @test
|
||
|
* @depends createGallery
|
||
|
* @param string $galleryId
|
||
|
* @return array
|
||
|
*/
|
||
|
public function uploadFileInGallery(string $galleryId): array
|
||
|
{
|
||
|
$response = $this->post(
|
||
|
route("admin:file:upload", ['galleryId' => $galleryId]),
|
||
|
['file' => UploadedFile::fake()->image('avatar.jpg')]
|
||
|
);
|
||
|
$response->assertStatus(ResponseAlias::HTTP_OK);
|
||
|
$response->assertJson(function (AssertableJson $json) use ($galleryId) {
|
||
|
$json
|
||
|
->where('fileId', function (string $name) {
|
||
|
return strlen($name) === 32 + 4;
|
||
|
})
|
||
|
->where('galleryId', $galleryId);
|
||
|
});
|
||
|
self::assertTrue(Storage::exists($this->prefixGallery . $galleryId . "/" . $response->json('fileId')));
|
||
|
return ['fileId' => $response->json('fileId'), 'galleryId' => $galleryId];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Тестируем удаление файла
|
||
|
* @test
|
||
|
* @depends uploadFileInGallery
|
||
|
* @param array $data
|
||
|
* @return void
|
||
|
*/
|
||
|
public function removeFileInGallery(array $data): void
|
||
|
{
|
||
|
$response =$this->delete(route("admin:file:remove",$data));
|
||
|
$response->assertStatus(ResponseAlias::HTTP_OK);
|
||
|
self::assertNotTrue(Storage::exists($this->prefixGallery . $data['galleryId'] . "/" . $data['fileId']));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Тестируем удаление галереи
|
||
|
* @test
|
||
|
* @depends createGallery
|
||
|
* @param string $galleryId
|
||
|
* @return void
|
||
|
*/
|
||
|
public function removeGallery(string $galleryId): void
|
||
|
{
|
||
|
$response =$this->delete(route("admin:gallery:remove",['id'=>$galleryId]));
|
||
|
$response->assertStatus(ResponseAlias::HTTP_OK);
|
||
|
self::assertNotTrue(Storage::exists($this->prefixGallery . $galleryId));
|
||
|
}
|
||
|
}
|