259 lines
7.6 KiB
PHP
259 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Api;
|
|
|
|
use App\Enums\Legacy\ApiError;
|
|
use App\Enums\Legacy\ApiStatus;
|
|
use App\Enums\MediaExtension;
|
|
use App\Enums\MediaStatut;
|
|
use App\Models\Article;
|
|
use App\Models\Media;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Testing\TestResponse;
|
|
use Symfony\Component\Mime\MimeTypes;
|
|
use Tests\CreatesApplication;
|
|
|
|
class ApiMediaTest extends ApiBase
|
|
{
|
|
use CreatesApplication;
|
|
use DatabaseTransactions;
|
|
|
|
/**
|
|
* Test get media by Id
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testMediaShowById()
|
|
{
|
|
\mt_srand();
|
|
$rand = \mt_rand(15, 60);
|
|
for ($i = 0; $i < $rand; $i++) {
|
|
\mt_srand();
|
|
$media = Media::query()->inRandomOrder(\mt_rand())
|
|
->where('statut', '!=', MediaStatut::brouillon)
|
|
->where('isDefault', false)
|
|
->first();
|
|
|
|
$response = $this->callApiWithToken('get', route('api.media.show', [
|
|
'apiMedia' => $media->id
|
|
]));
|
|
|
|
|
|
$this->assertResponseIsMediaResource($response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Could throw api error exception not found
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testMediaShowByArcadiaId()
|
|
{
|
|
\mt_srand();
|
|
$rand = \mt_rand(15, 60);
|
|
for ($i = 0; $i < $rand; $i++) {
|
|
\mt_srand();
|
|
$media = Media::query()->whereNotNull('arcadia_id')
|
|
->where('statut', '!=', MediaStatut::brouillon)
|
|
->where('isDefault', false)
|
|
->inRandomOrder(\mt_rand())->first();
|
|
|
|
$response = $this->callApiWithToken('get', route('api.media.showByArcadiaId', [
|
|
'apiMediaArcadiaId' => $media->arcadia_id
|
|
]));
|
|
|
|
$this->assertResponseIsMediaResource($response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Test get media by Id not found
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testMediaShowNotFound()
|
|
{
|
|
$response = $this->callApiWithToken('get', route('api.media.show', [
|
|
'apiMedia' => '94984919191616161'
|
|
]));
|
|
|
|
$this->assertResponseIsNotFound($response);
|
|
}
|
|
|
|
/**
|
|
* Test get media picture
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testMediaPicture()
|
|
{
|
|
\mt_srand();
|
|
$rand = \mt_rand(15, 60);
|
|
for ($i = 0; $i < $rand; $i++) {
|
|
\mt_srand();
|
|
$media = Media::query()->whereNotNull('arcadia_id')
|
|
->where('statut', '!=', MediaStatut::brouillon)
|
|
->where('isDefault', false)
|
|
->inRandomOrder(\mt_rand())->first();
|
|
|
|
$response = $this->callApiWithToken('get', route('api.media.picture', [
|
|
'apiMedia' => $media->id
|
|
]));
|
|
|
|
$this->assertResponseIsMediaPictureResource($response);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Test get media picture
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testMediaPictureNotFound()
|
|
{
|
|
\mt_srand();
|
|
$response = $this->callApiWithToken('get', route('api.media.picture', [
|
|
'apiMedia' => \mt_rand(9999999, 99999999999)
|
|
]));
|
|
|
|
$this->assertResponseIsNotFound($response);
|
|
}
|
|
|
|
/**
|
|
* Test get media picture
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testGetMediaListByArticle()
|
|
{
|
|
\mt_srand();
|
|
$rand = \mt_rand(15, 60);
|
|
for ($i = 0; $i < $rand; $i++) {
|
|
\mt_srand();
|
|
$article = Article::query()
|
|
->inRandomOrder(\mt_rand())->first();
|
|
|
|
$response = $this->callApiWithToken('get', route('api.articles.media.index', [
|
|
'apiNoArticle' => $article->noArt
|
|
]));
|
|
|
|
$this->assertResponseIsMediaListResource($response);
|
|
}
|
|
}
|
|
|
|
// * PRIVATE METHODS
|
|
|
|
/**
|
|
* Assert the response contains a not found message.
|
|
*
|
|
* @param TestResponse $response
|
|
* @return void
|
|
*/
|
|
private function assertResponseIsNotFound(TestResponse $response)
|
|
{
|
|
$json = $response->decodeResponseJson();
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::KO());
|
|
$json->assertStructure(['data' => [
|
|
'code', 'message'
|
|
]], $json->json());
|
|
|
|
$this->assertEquals(ApiError::modelNotFound()->value, $json->json('data.code'));
|
|
}
|
|
|
|
/**
|
|
* Assert the response contains a media list resource.
|
|
*
|
|
* @param TestResponse $response
|
|
* @return void
|
|
*/
|
|
private function assertResponseIsMediaListResource(TestResponse $response)
|
|
{
|
|
$json = $response->decodeResponseJson();
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
|
|
$json->assertStructure([
|
|
'data' => [
|
|
'*' => [
|
|
'id', 'arcadia_id', 'libelle', 'uid',
|
|
'version', 'created_at', 'updated_at'
|
|
]
|
|
]
|
|
], $json->json());
|
|
}
|
|
|
|
/**
|
|
* Assert the response contains a media resource.
|
|
*
|
|
* @param TestResponse $response
|
|
* @return void
|
|
*/
|
|
private function assertResponseIsMediaResource(TestResponse $response)
|
|
{
|
|
$json = $response->decodeResponseJson();
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
|
|
$json->assertStructure(['data' => [
|
|
'id', 'arcadia_id', 'libelle', 'uid',
|
|
'version', 'created_at', 'updated_at'
|
|
]], $json->json());
|
|
|
|
$this->assertInstanceOf(Carbon::class, Carbon::parse($json->json('data.created_at')));
|
|
$this->assertInstanceOf(Carbon::class, Carbon::parse($json->json('data.updated_at')));
|
|
|
|
$this->assertDatabaseHas((new Media())->getTable(), [
|
|
'id' => $json->json('data.id'),
|
|
'arcadia_id' => $json->json('data.arcadia_id'),
|
|
'libelle' => $json->json('data.libelle'),
|
|
'uid' => $json->json('data.uid'),
|
|
'version' => $json->json('data.version'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Assert the response contains a media picture resource.
|
|
*
|
|
* @param TestResponse $response
|
|
* @return void
|
|
*/
|
|
private function assertResponseIsMediaPictureResource(TestResponse $response)
|
|
{
|
|
$json = $response->decodeResponseJson();
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
|
|
$json->assertStructure(['data' => [
|
|
'base64_bin', 'uid', 'extension'
|
|
]], $json->json());
|
|
|
|
$this->assertTrue(
|
|
base64_encode(base64_decode($json->json('data.base64_bin'), true)) === $json->json('data.base64_bin'),
|
|
'Should be base64 encoded'
|
|
);
|
|
$this->assertTrue(
|
|
\in_array($json->json('data.extension'), collect(MediaExtension::cases())->pluck('value')->all()),
|
|
'Extension should exist and be a MediaExtension enum'
|
|
);
|
|
$this->assertDatabaseHas((new Media())->getTable(), [
|
|
'uid' => $json->json('data.uid'),
|
|
'extension' => $json->json('data.extension'),
|
|
]);
|
|
$tmpFilePath = 'tmp/' . $json->json('data.uid') . '.' . $json->json('data.extension');
|
|
|
|
Storage::put(
|
|
$tmpFilePath,
|
|
\base64_decode($json->json('data.base64_bin'))
|
|
);
|
|
$ext = $json->json('data.extension');
|
|
|
|
$mt = new MimeTypes();
|
|
$guessedExts = $mt->getExtensions($mt->guessMimeType(Storage::path($tmpFilePath)));
|
|
|
|
$strGuessedExts = \implode(',', $guessedExts);
|
|
|
|
Storage::delete($tmpFilePath);
|
|
|
|
$this->assertTrue(
|
|
\in_array($ext, $guessedExts),
|
|
"File guessed extensions '{$strGuessedExts}' must contain name extension '{$ext}'"
|
|
);
|
|
}
|
|
}
|