mirror of
https://github.com/koel/koel
synced 2024-09-20 06:11:53 +00:00
feat(test|api): add Album tests
This commit is contained in:
parent
818d4b0fac
commit
9e641c9ba9
7 changed files with 78 additions and 7 deletions
|
@ -4,5 +4,5 @@ trim_trailing_whitespace = true
|
|||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
[*.php]
|
||||
[{*.php, *.xml, *.xml.dist}]
|
||||
indent_size = 4
|
||||
|
|
|
@ -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,
|
||||
];
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,8 +16,7 @@ use Mockery\MockInterface;
|
|||
|
||||
class DownloadTest extends TestCase
|
||||
{
|
||||
/** @var MockInterface|DownloadService */
|
||||
private $downloadService;
|
||||
private MockInterface|DownloadService $downloadService;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
44
tests/Feature/V6/AlbumTest.php
Normal file
44
tests/Feature/V6/AlbumTest.php
Normal 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);
|
||||
}
|
||||
}
|
15
tests/Feature/V6/TestCase.php
Normal file
15
tests/Feature/V6/TestCase.php
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue