feat(test|api): add Album tests

This commit is contained in:
Phan An 2022-07-26 22:08:31 +02:00
parent 818d4b0fac
commit 9e641c9ba9
No known key found for this signature in database
GPG key ID: A81E4477F0BB6FDC
7 changed files with 78 additions and 7 deletions

View file

@ -4,5 +4,5 @@ trim_trailing_whitespace = true
indent_style = space
indent_size = 2
[*.php]
[{*.php, *.xml, *.xml.dist}]
indent_size = 4

View file

@ -23,7 +23,7 @@ class AlbumResource extends JsonResource
'artist_name' => $this->album->artist->name,
'cover' => $this->album->cover,
'created_at' => $this->album->created_at,
'length' => $this->album->length,
'length' => (float) $this->album->length,
'play_count' => (int) $this->album->play_count,
'song_count' => (int) $this->album->song_count,
];

View file

@ -22,11 +22,25 @@ class RouteServiceProvider extends ServiceProvider
Route::group([], base_path(sprintf('routes/%s.base.php', $type)));
$apiVersion = request()->header('X-Api-Version');
$apiVersion = self::getApiVersion();
$routeFile = $apiVersion ? base_path(sprintf('routes/%s.%s.php', $type, $apiVersion)) : null;
if ($routeFile && file_exists($routeFile)) {
Route::group([], $routeFile);
}
}
private static function getApiVersion(): ?string
{
// In the test environment, the route service provider is loaded _before_ the request is made,
// so we can't rely on the header.
// Instead, we manually set the API version as an env variable in applicable test cases.
$version = app()->runningUnitTests() ? env('X_API_VERSION') : request()->header('X-Api-Version');
if ($version) {
Assert::oneOf($version, ['v6']);
}
return $version;
}
}

View file

@ -16,8 +16,7 @@ use Mockery\MockInterface;
class DownloadTest extends TestCase
{
/** @var MockInterface|DownloadService */
private $downloadService;
private MockInterface|DownloadService $downloadService;
public function setUp(): void
{

View file

@ -12,8 +12,7 @@ abstract class TestCase extends BaseTestCase
{
/** @var User $user */
$user = $user ?: User::factory()->create();
$headers['X-Requested-With'] = 'XMLHttpRequest';
$headers['Authorization'] = 'Bearer ' . $user->createToken('koel')->plainTextToken;
$this->withToken($user->createToken('koel')->plainTextToken);
return parent::json($method, $uri, $data, $headers);
}

View file

@ -0,0 +1,44 @@
<?php
namespace Tests\Feature\V6;
use App\Models\Album;
class AlbumTest extends TestCase
{
private const JSON_STRUCTURE = [
'type',
'id',
'name',
'artist_id',
'artist_name',
'cover',
'created_at',
'length',
'play_count',
'song_count',
];
private const JSON_COLLECTION_STRUCTURE = [
'data' => [
'*' => self::JSON_STRUCTURE,
],
];
public function testIndex(): void
{
Album::factory(10)->create();
$this->getAsUser('api/albums')
->assertJsonStructure(self::JSON_COLLECTION_STRUCTURE);
}
public function testShow(): void
{
/** @var Album $album */
$album = Album::factory()->create();
$this->getAsUser('api/albums/' . $album->id)
->assertJsonStructure(self::JSON_STRUCTURE);
}
}

View file

@ -0,0 +1,15 @@
<?php
namespace Tests\Feature\V6;
use Tests\Feature\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
public function setUp(): void
{
putenv('X_API_VERSION=v6');
parent::setUp();
}
}