From 9e641c9ba98ee4896ec1cf319a93263110e34c0d Mon Sep 17 00:00:00 2001 From: Phan An Date: Tue, 26 Jul 2022 22:08:31 +0200 Subject: [PATCH] feat(test|api): add Album tests --- .editorconfig | 2 +- app/Http/Resources/AlbumResource.php | 2 +- app/Providers/RouteServiceProvider.php | 16 +++++++++- tests/Feature/DownloadTest.php | 3 +- tests/Feature/TestCase.php | 3 +- tests/Feature/V6/AlbumTest.php | 44 ++++++++++++++++++++++++++ tests/Feature/V6/TestCase.php | 15 +++++++++ 7 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 tests/Feature/V6/AlbumTest.php create mode 100644 tests/Feature/V6/TestCase.php diff --git a/.editorconfig b/.editorconfig index 3149f1c2..f1938bbd 100644 --- a/.editorconfig +++ b/.editorconfig @@ -4,5 +4,5 @@ trim_trailing_whitespace = true indent_style = space indent_size = 2 -[*.php] +[{*.php, *.xml, *.xml.dist}] indent_size = 4 diff --git a/app/Http/Resources/AlbumResource.php b/app/Http/Resources/AlbumResource.php index f10efc12..0e3860a2 100644 --- a/app/Http/Resources/AlbumResource.php +++ b/app/Http/Resources/AlbumResource.php @@ -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, ]; diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index a4f9c549..27b04974 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -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; + } } diff --git a/tests/Feature/DownloadTest.php b/tests/Feature/DownloadTest.php index d35215a9..9ca42ac4 100644 --- a/tests/Feature/DownloadTest.php +++ b/tests/Feature/DownloadTest.php @@ -16,8 +16,7 @@ use Mockery\MockInterface; class DownloadTest extends TestCase { - /** @var MockInterface|DownloadService */ - private $downloadService; + private MockInterface|DownloadService $downloadService; public function setUp(): void { diff --git a/tests/Feature/TestCase.php b/tests/Feature/TestCase.php index 4884853d..e1abf418 100644 --- a/tests/Feature/TestCase.php +++ b/tests/Feature/TestCase.php @@ -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); } diff --git a/tests/Feature/V6/AlbumTest.php b/tests/Feature/V6/AlbumTest.php new file mode 100644 index 00000000..54a0df95 --- /dev/null +++ b/tests/Feature/V6/AlbumTest.php @@ -0,0 +1,44 @@ + [ + '*' => 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); + } +} diff --git a/tests/Feature/V6/TestCase.php b/tests/Feature/V6/TestCase.php new file mode 100644 index 00000000..1c9a9e3e --- /dev/null +++ b/tests/Feature/V6/TestCase.php @@ -0,0 +1,15 @@ +