2016-03-05 09:01:12 +00:00
|
|
|
<?php
|
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2024-01-25 16:21:26 +00:00
|
|
|
use App\Http\Resources\SongResource;
|
2016-03-05 09:01:12 +00:00
|
|
|
use App\Models\Album;
|
|
|
|
use App\Models\Artist;
|
|
|
|
use App\Models\Song;
|
2021-06-05 10:47:56 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2024-10-24 10:45:45 +00:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2024-01-09 18:34:40 +00:00
|
|
|
use Tests\TestCase;
|
2016-03-05 09:01:12 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
use function Tests\create_admin;
|
|
|
|
|
2017-08-05 16:56:11 +00:00
|
|
|
class SongTest extends TestCase
|
2016-03-05 09:01:12 +00:00
|
|
|
{
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function index(): void
|
2016-05-30 16:19:52 +00:00
|
|
|
{
|
2023-06-04 21:51:53 +00:00
|
|
|
Song::factory(10)->create();
|
2018-08-19 21:17:05 +00:00
|
|
|
|
2024-01-25 16:21:26 +00:00
|
|
|
$this->getAs('api/songs')->assertJsonStructure(SongResource::PAGINATION_JSON_STRUCTURE);
|
|
|
|
$this->getAs('api/songs?sort=title&order=desc')->assertJsonStructure(SongResource::PAGINATION_JSON_STRUCTURE);
|
2023-06-04 21:51:53 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function show(): void
|
2023-06-04 21:51:53 +00:00
|
|
|
{
|
|
|
|
/** @var Song $song */
|
|
|
|
$song = Song::factory()->create();
|
|
|
|
|
2024-01-25 16:21:26 +00:00
|
|
|
$this->getAs('api/songs/' . $song->id)->assertJsonStructure(SongResource::JSON_STRUCTURE);
|
2023-06-04 21:51:53 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function destroy(): void
|
2023-06-04 21:51:53 +00:00
|
|
|
{
|
|
|
|
$songs = Song::factory(3)->create();
|
|
|
|
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->deleteAs('api/songs', ['songs' => $songs->modelKeys()], create_admin())
|
2023-06-04 21:51:53 +00:00
|
|
|
->assertNoContent();
|
|
|
|
|
|
|
|
$songs->each(fn (Song $song) => $this->assertModelMissing($song));
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function unauthorizedDelete(): void
|
2023-06-04 21:51:53 +00:00
|
|
|
{
|
|
|
|
$songs = Song::factory(3)->create();
|
|
|
|
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->deleteAs('api/songs', ['songs' => $songs->modelKeys()])
|
2023-06-04 21:51:53 +00:00
|
|
|
->assertForbidden();
|
|
|
|
|
|
|
|
$songs->each(fn (Song $song) => $this->assertModelExists($song));
|
2016-05-30 16:19:52 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function singleUpdateAllInfoNoCompilation(): void
|
2016-03-05 09:01:12 +00:00
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
/** @var Song $song */
|
2024-01-10 23:11:45 +00:00
|
|
|
$song = Song::factory()->create();
|
2021-06-05 10:47:56 +00:00
|
|
|
|
2022-07-27 08:49:33 +00:00
|
|
|
$this->putAs('/api/songs', [
|
2020-01-08 14:21:29 +00:00
|
|
|
'songs' => [$song->id],
|
|
|
|
'data' => [
|
|
|
|
'title' => 'Foo Bar',
|
2022-07-06 16:08:55 +00:00
|
|
|
'artist_name' => 'John Cena',
|
|
|
|
'album_name' => 'One by One',
|
2020-01-08 14:21:29 +00:00
|
|
|
'lyrics' => 'Lorem ipsum dolor sic amet.',
|
|
|
|
'track' => 1,
|
2022-07-06 16:08:55 +00:00
|
|
|
'disc' => 2,
|
2020-01-08 14:21:29 +00:00
|
|
|
],
|
2024-01-11 12:41:33 +00:00
|
|
|
], create_admin())
|
2022-07-27 15:32:36 +00:00
|
|
|
->assertOk();
|
2016-03-05 09:01:12 +00:00
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
/** @var Artist $artist */
|
2022-08-09 18:45:11 +00:00
|
|
|
$artist = Artist::query()->where('name', 'John Cena')->first();
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertNotNull($artist);
|
2016-03-05 09:01:12 +00:00
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
/** @var Album $album */
|
2022-08-09 18:45:11 +00:00
|
|
|
$album = Album::query()->where('name', 'One by One')->first();
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertNotNull($album);
|
2016-03-05 09:01:12 +00:00
|
|
|
|
2022-07-06 16:08:55 +00:00
|
|
|
self::assertDatabaseHas(Song::class, [
|
2016-03-05 09:01:12 +00:00
|
|
|
'id' => $song->id,
|
|
|
|
'album_id' => $album->id,
|
|
|
|
'lyrics' => 'Lorem ipsum dolor sic amet.',
|
2016-03-20 13:18:34 +00:00
|
|
|
'track' => 1,
|
2022-07-06 16:08:55 +00:00
|
|
|
'disc' => 2,
|
2016-03-05 09:01:12 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function singleUpdateSomeInfoNoCompilation(): void
|
2016-03-05 09:01:12 +00:00
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
/** @var Song $song */
|
2024-01-10 23:11:45 +00:00
|
|
|
$song = Song::factory()->create();
|
2022-08-09 18:45:11 +00:00
|
|
|
|
2022-07-06 16:08:55 +00:00
|
|
|
$originalArtistId = $song->artist->id;
|
2021-06-05 10:47:56 +00:00
|
|
|
|
2022-07-27 08:49:33 +00:00
|
|
|
$this->putAs('/api/songs', [
|
2020-01-08 14:21:29 +00:00
|
|
|
'songs' => [$song->id],
|
|
|
|
'data' => [
|
|
|
|
'title' => '',
|
2022-07-06 16:08:55 +00:00
|
|
|
'artist_name' => '',
|
|
|
|
'album_name' => 'One by One',
|
2020-01-08 14:21:29 +00:00
|
|
|
'lyrics' => 'Lorem ipsum dolor sic amet.',
|
|
|
|
'track' => 1,
|
|
|
|
],
|
2024-01-11 12:41:33 +00:00
|
|
|
], create_admin())
|
2022-07-27 15:32:36 +00:00
|
|
|
->assertOk();
|
2016-03-05 09:01:12 +00:00
|
|
|
|
|
|
|
// We don't expect the song's artist to change
|
2022-10-07 14:25:44 +00:00
|
|
|
self::assertSame($originalArtistId, $song->refresh()->artist->id);
|
2016-03-05 09:01:12 +00:00
|
|
|
|
|
|
|
// But we expect a new album to be created for this artist and contain this song
|
2022-10-07 14:25:44 +00:00
|
|
|
self::assertSame('One by One', $song->album->name);
|
2016-03-05 09:01:12 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function multipleUpdateNoCompilation(): void
|
2016-03-05 09:01:12 +00:00
|
|
|
{
|
2024-11-09 14:56:48 +00:00
|
|
|
$songIds = Song::factory(3)->create()->modelKeys();
|
2016-03-05 09:01:12 +00:00
|
|
|
|
2022-07-27 08:49:33 +00:00
|
|
|
$this->putAs('/api/songs', [
|
2020-01-08 14:21:29 +00:00
|
|
|
'songs' => $songIds,
|
|
|
|
'data' => [
|
2022-11-29 12:16:43 +00:00
|
|
|
'title' => null,
|
2022-07-06 16:08:55 +00:00
|
|
|
'artist_name' => 'John Cena',
|
|
|
|
'album_name' => 'One by One',
|
2022-11-29 12:16:43 +00:00
|
|
|
'lyrics' => null,
|
2020-01-08 14:21:29 +00:00
|
|
|
'track' => 9999,
|
|
|
|
],
|
2024-01-11 12:41:33 +00:00
|
|
|
], create_admin())
|
2022-07-27 15:32:36 +00:00
|
|
|
->assertOk();
|
2016-03-05 09:01:12 +00:00
|
|
|
|
2024-04-18 11:27:07 +00:00
|
|
|
/** @var Collection<array-key, Song> $songs */
|
2022-08-09 18:45:11 +00:00
|
|
|
$songs = Song::query()->whereIn('id', $songIds)->get();
|
2016-03-05 09:01:12 +00:00
|
|
|
|
2022-07-06 16:08:55 +00:00
|
|
|
// All of these songs must now belong to a new album and artist set
|
2022-10-07 14:25:44 +00:00
|
|
|
self::assertSame('One by One', $songs[0]->album->name);
|
2022-07-06 16:08:55 +00:00
|
|
|
self::assertSame($songs[0]->album_id, $songs[1]->album_id);
|
|
|
|
self::assertSame($songs[0]->album_id, $songs[2]->album_id);
|
2016-03-05 09:01:12 +00:00
|
|
|
|
2022-10-07 14:25:44 +00:00
|
|
|
self::assertSame('John Cena', $songs[0]->artist->name);
|
2022-07-06 16:08:55 +00:00
|
|
|
self::assertSame($songs[0]->artist_id, $songs[1]->artist_id);
|
|
|
|
self::assertSame($songs[0]->artist_id, $songs[2]->artist_id);
|
2022-11-29 12:16:43 +00:00
|
|
|
|
|
|
|
self::assertNotSame($songs[0]->title, $songs[1]->title);
|
|
|
|
self::assertNotSame($songs[0]->lyrics, $songs[1]->lyrics);
|
|
|
|
|
|
|
|
self::assertSame(9999, $songs[0]->track);
|
|
|
|
self::assertSame(9999, $songs[1]->track);
|
|
|
|
self::assertSame(9999, $songs[2]->track);
|
2016-03-05 09:01:12 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function multipleUpdateCreatingNewAlbumsAndArtists(): void
|
2016-03-05 09:01:12 +00:00
|
|
|
{
|
2024-01-10 23:11:45 +00:00
|
|
|
$originalSongs = Song::factory(3)->create();
|
2024-11-09 14:56:48 +00:00
|
|
|
$originalSongIds = $originalSongs->modelKeys();
|
2024-01-10 23:11:45 +00:00
|
|
|
$originalAlbumNames = $originalSongs->pluck('album.name')->all();
|
|
|
|
$originalAlbumIds = $originalSongs->pluck('album_id')->all();
|
2016-03-05 09:01:12 +00:00
|
|
|
|
2022-07-27 08:49:33 +00:00
|
|
|
$this->putAs('/api/songs', [
|
2024-01-01 11:40:21 +00:00
|
|
|
'songs' => $originalSongIds,
|
2020-01-08 14:21:29 +00:00
|
|
|
'data' => [
|
|
|
|
'title' => 'Foo Bar',
|
2022-07-06 16:08:55 +00:00
|
|
|
'artist_name' => 'John Cena',
|
|
|
|
'album_name' => '',
|
2020-01-08 14:21:29 +00:00
|
|
|
'lyrics' => 'Lorem ipsum dolor sic amet.',
|
|
|
|
'track' => 1,
|
|
|
|
],
|
2024-01-11 12:41:33 +00:00
|
|
|
], create_admin())
|
2022-07-27 15:32:36 +00:00
|
|
|
->assertOk();
|
2016-03-05 09:01:12 +00:00
|
|
|
|
2024-01-01 11:40:21 +00:00
|
|
|
$songs = Song::query()->whereIn('id', $originalSongIds)->get()->orderByArray($originalSongIds);
|
2016-03-05 09:01:12 +00:00
|
|
|
|
|
|
|
// Even though the album name doesn't change, a new artist should have been created
|
|
|
|
// and thus, a new album with the same name was created as well.
|
2024-01-10 23:11:45 +00:00
|
|
|
collect([0, 1, 2])->each(static function (int $i) use ($songs, $originalAlbumNames, $originalAlbumIds): void {
|
|
|
|
self::assertSame($songs[$i]->album->name, $originalAlbumNames[$i]);
|
|
|
|
self::assertNotSame($songs[$i]->album_id, $originalAlbumIds[$i]);
|
|
|
|
});
|
2016-03-05 09:01:12 +00:00
|
|
|
|
|
|
|
// And of course, the new artist is...
|
2022-10-07 14:25:44 +00:00
|
|
|
self::assertSame('John Cena', $songs[0]->artist->name); // JOHN CENA!!!
|
|
|
|
self::assertSame('John Cena', $songs[1]->artist->name); // JOHN CENA!!!
|
|
|
|
self::assertSame('John Cena', $songs[2]->artist->name); // And... JOHN CENAAAAAAAAAAA!!!
|
2016-03-05 09:01:12 +00:00
|
|
|
}
|
2016-04-24 04:37:04 +00:00
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function singleUpdateAllInfoWithCompilation(): void
|
2016-04-24 04:37:04 +00:00
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
/** @var Song $song */
|
2024-01-10 23:11:45 +00:00
|
|
|
$song = Song::factory()->create();
|
2022-08-09 18:45:11 +00:00
|
|
|
|
2022-07-27 08:49:33 +00:00
|
|
|
$this->putAs('/api/songs', [
|
2020-01-08 14:21:29 +00:00
|
|
|
'songs' => [$song->id],
|
|
|
|
'data' => [
|
|
|
|
'title' => 'Foo Bar',
|
2022-07-06 16:08:55 +00:00
|
|
|
'artist_name' => 'John Cena',
|
|
|
|
'album_name' => 'One by One',
|
|
|
|
'album_artist_name' => 'John Lennon',
|
2020-01-08 14:21:29 +00:00
|
|
|
'lyrics' => 'Lorem ipsum dolor sic amet.',
|
|
|
|
'track' => 1,
|
2022-07-06 16:08:55 +00:00
|
|
|
'disc' => 2,
|
2020-01-08 14:21:29 +00:00
|
|
|
],
|
2024-01-11 12:41:33 +00:00
|
|
|
], create_admin())
|
2022-07-27 15:32:36 +00:00
|
|
|
->assertOk();
|
2016-04-24 04:37:04 +00:00
|
|
|
|
2022-07-06 16:08:55 +00:00
|
|
|
/** @var Album $album */
|
2022-08-09 18:45:11 +00:00
|
|
|
$album = Album::query()->where('name', 'One by One')->first();
|
2022-07-06 16:08:55 +00:00
|
|
|
|
|
|
|
/** @var Artist $albumArtist */
|
2022-08-09 18:45:11 +00:00
|
|
|
$albumArtist = Artist::query()->where('name', 'John Lennon')->first();
|
2016-04-24 04:37:04 +00:00
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
/** @var Artist $artist */
|
2022-08-09 18:45:11 +00:00
|
|
|
$artist = Artist::query()->where('name', 'John Cena')->first();
|
2016-04-24 04:37:04 +00:00
|
|
|
|
2022-07-06 16:08:55 +00:00
|
|
|
self::assertDatabaseHas(Song::class, [
|
2016-04-24 04:37:04 +00:00
|
|
|
'id' => $song->id,
|
2017-04-29 03:49:14 +00:00
|
|
|
'artist_id' => $artist->id,
|
2022-07-06 16:08:55 +00:00
|
|
|
'album_id' => $album->id,
|
2016-04-24 04:37:04 +00:00
|
|
|
'lyrics' => 'Lorem ipsum dolor sic amet.',
|
|
|
|
'track' => 1,
|
2022-07-06 16:08:55 +00:00
|
|
|
'disc' => 2,
|
2016-04-24 04:37:04 +00:00
|
|
|
]);
|
|
|
|
|
2022-07-06 16:08:55 +00:00
|
|
|
self::assertTrue($album->artist->is($albumArtist));
|
2016-04-24 04:37:04 +00:00
|
|
|
}
|
2016-05-27 08:34:38 +00:00
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function updateSingleSongWithEmptyTrackAndDisc(): void
|
2022-11-29 12:16:43 +00:00
|
|
|
{
|
|
|
|
/** @var Song $song */
|
|
|
|
$song = Song::factory()->create([
|
|
|
|
'track' => 12,
|
|
|
|
'disc' => 2,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->putAs('/api/songs', [
|
|
|
|
'songs' => [$song->id],
|
|
|
|
'data' => [
|
|
|
|
'track' => null,
|
|
|
|
'disc' => null,
|
|
|
|
],
|
2024-01-11 12:41:33 +00:00
|
|
|
], create_admin())
|
2022-11-29 12:16:43 +00:00
|
|
|
->assertOk();
|
|
|
|
|
|
|
|
$song->refresh();
|
|
|
|
|
|
|
|
self::assertSame(0, $song->track);
|
|
|
|
self::assertSame(1, $song->disc);
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function deletingByChunk(): void
|
2016-09-26 07:32:16 +00:00
|
|
|
{
|
2023-06-04 21:51:53 +00:00
|
|
|
Song::factory(5)->create();
|
|
|
|
|
2024-11-09 14:56:48 +00:00
|
|
|
Song::deleteByChunk(Song::query()->get()->modelKeys(), 1);
|
2022-07-27 15:32:36 +00:00
|
|
|
|
2022-10-07 14:25:44 +00:00
|
|
|
self::assertSame(0, Song::query()->count());
|
2016-09-26 07:32:16 +00:00
|
|
|
}
|
2016-03-05 09:01:12 +00:00
|
|
|
}
|