koel/tests/Feature/GenreTest.php

60 lines
1.7 KiB
PHP
Raw Normal View History

2022-10-21 20:06:43 +00:00
<?php
2023-06-05 21:46:41 +00:00
namespace Tests\Feature;
2022-10-21 20:06:43 +00:00
use App\Models\Song;
use App\Values\Genre;
class GenreTest extends TestCase
{
private const JSON_STRUCTURE = [
'type',
'name',
'song_count',
'length',
];
public function testGetAllGenres(): void
{
Song::factory()->count(5)->create(['genre' => 'Rock']);
Song::factory()->count(2)->create(['genre' => 'Pop']);
Song::factory()->count(10)->create(['genre' => '']);
$this->getAs('api/genres')
->assertJsonStructure(['*' => self::JSON_STRUCTURE])
->assertJsonFragment(['name' => 'Rock', 'song_count' => 5])
->assertJsonFragment(['name' => 'Pop', 'song_count' => 2])
->assertJsonFragment(['name' => Genre::NO_GENRE, 'song_count' => 10]);
}
public function testGetOneGenre(): void
{
Song::factory()->count(5)->create(['genre' => 'Rock']);
$this->getAs('api/genres/Rock')
->assertJsonStructure(self::JSON_STRUCTURE)
->assertJsonFragment(['name' => 'Rock', 'song_count' => 5]);
}
2023-06-05 21:46:41 +00:00
2022-10-21 20:06:43 +00:00
public function testGetNonExistingGenreThrowsNotFound(): void
{
$this->getAs('api/genres/NonExistingGenre')->assertNotFound();
}
public function testPaginateSongsInGenre(): void
{
Song::factory()->count(5)->create(['genre' => 'Rock']);
$this->getAs('api/genres/Rock/songs')
->assertJsonStructure(SongTest::JSON_COLLECTION_STRUCTURE);
}
public function testGetRandomSongsInGenre(): void
{
Song::factory()->count(5)->create(['genre' => 'Rock']);
$this->getAs('api/genres/Rock/songs/random?limit=500')
->assertJsonStructure(['*' => SongTest::JSON_STRUCTURE]);
}
}