koel/tests/Feature/SongTest.php

209 lines
6.8 KiB
PHP
Raw Normal View History

2016-03-05 09:01:12 +00:00
<?php
2017-02-14 06:53:02 +00:00
namespace Tests\Feature;
2016-03-05 09:01:12 +00:00
use App\Models\Album;
use App\Models\Artist;
use App\Models\Song;
use App\Models\User;
2021-06-05 10:47:56 +00:00
use Illuminate\Support\Collection;
2016-03-05 09:01:12 +00:00
2017-08-05 16:56:11 +00:00
class SongTest extends TestCase
2016-03-05 09:01:12 +00:00
{
2019-07-22 07:03:23 +00:00
public function setUp(): void
2016-05-30 16:19:52 +00:00
{
parent::setUp();
2018-08-19 21:17:05 +00:00
static::createSampleMediaSet();
2016-05-30 16:19:52 +00:00
}
2019-07-22 07:03:23 +00:00
public function testSingleUpdateAllInfoNoCompilation(): void
2016-03-05 09:01:12 +00:00
{
2022-07-27 15:32:36 +00:00
/** @var User $user */
$user = User::factory()->admin()->create();
/** @var Song $song */
$song = Song::query()->first();
2021-06-05 10:47:56 +00:00
2022-07-27 08:49:33 +00:00
$this->putAs('/api/songs', [
'songs' => [$song->id],
'data' => [
'title' => 'Foo Bar',
2022-07-06 16:08:55 +00:00
'artist_name' => 'John Cena',
'album_name' => 'One by One',
'lyrics' => 'Lorem ipsum dolor sic amet.',
'track' => 1,
2022-07-06 16:08:55 +00:00
'disc' => 2,
],
2022-07-27 15:32:36 +00:00
], $user)
->assertOk();
2016-03-05 09:01:12 +00:00
2021-06-05 10:47:56 +00:00
/** @var Artist $artist */
$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 */
$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.',
'track' => 1,
2022-07-06 16:08:55 +00:00
'disc' => 2,
2016-03-05 09:01:12 +00:00
]);
}
2019-07-22 07:03:23 +00:00
public function testSingleUpdateSomeInfoNoCompilation(): void
2016-03-05 09:01:12 +00:00
{
2022-07-27 15:32:36 +00:00
/** @var User $user */
$user = User::factory()->admin()->create();
/** @var Song $song */
$song = Song::query()->first();
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', [
'songs' => [$song->id],
'data' => [
'title' => '',
2022-07-06 16:08:55 +00:00
'artist_name' => '',
'album_name' => 'One by One',
'lyrics' => 'Lorem ipsum dolor sic amet.',
'track' => 1,
],
2022-07-27 15:32:36 +00:00
], $user)
->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
}
2022-07-06 16:08:55 +00:00
public function testMultipleUpdateNoCompilation(): void
2016-03-05 09:01:12 +00:00
{
2022-07-27 15:32:36 +00:00
/** @var User $user */
$user = User::factory()->admin()->create();
$songIds = Song::query()->latest()->take(3)->pluck('id')->toArray();
2016-03-05 09:01:12 +00:00
2022-07-27 08:49:33 +00:00
$this->putAs('/api/songs', [
'songs' => $songIds,
'data' => [
'title' => 'foo',
2022-07-06 16:08:55 +00:00
'artist_name' => 'John Cena',
'album_name' => 'One by One',
'lyrics' => 'bar',
'track' => 9999,
],
2022-07-27 15:32:36 +00:00
], $user)
->assertOk();
2016-03-05 09:01:12 +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);
2016-03-05 09:01:12 +00:00
}
2022-07-06 16:08:55 +00:00
public function testMultipleUpdateCreatingNewAlbumsAndArtists(): void
2016-03-05 09:01:12 +00:00
{
2022-07-27 15:32:36 +00:00
/** @var User $user */
$user = User::factory()->admin()->create();
2022-07-06 16:08:55 +00:00
/** @var array<array-key, Song>|Collection $originalSongs */
$originalSongs = Song::query()->latest()->take(3)->get();
2016-03-05 09:01:12 +00:00
2022-07-27 08:49:33 +00:00
$this->putAs('/api/songs', [
2022-10-12 09:27:35 +00:00
'songs' => $originalSongs->pluck('id')->toArray(),
'data' => [
'title' => 'Foo Bar',
2022-07-06 16:08:55 +00:00
'artist_name' => 'John Cena',
'album_name' => '',
'lyrics' => 'Lorem ipsum dolor sic amet.',
'track' => 1,
],
2022-07-27 15:32:36 +00:00
], $user)
->assertOk();
2016-03-05 09:01:12 +00:00
2022-10-12 09:27:35 +00:00
/** @var array<array-key, Song>|Collection $songs */
$songs = Song::query()->latest()->take(3)->get();
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.
2022-10-07 14:25:44 +00:00
self::assertSame($songs[0]->album->name, $originalSongs[0]->album->name);
self::assertNotSame($songs[0]->album->id, $originalSongs[0]->album->id);
self::assertSame($songs[1]->album->name, $originalSongs[1]->album->name);
self::assertNotSame($songs[1]->album->id, $originalSongs[1]->album->id);
self::assertSame($songs[2]->album->name, $originalSongs[2]->album->name);
self::assertNotSame($songs[2]->album->id, $originalSongs[2]->album->id);
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
2022-07-06 16:08:55 +00:00
public function testSingleUpdateAllInfoWithCompilation(): void
2016-04-24 04:37:04 +00:00
{
2022-07-27 15:32:36 +00:00
/** @var User $user */
$user = User::factory()->admin()->create();
/** @var Song $song */
$song = Song::query()->first();
2021-06-05 10:47:56 +00:00
2022-07-27 08:49:33 +00:00
$this->putAs('/api/songs', [
'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',
'lyrics' => 'Lorem ipsum dolor sic amet.',
'track' => 1,
2022-07-06 16:08:55 +00:00
'disc' => 2,
],
2022-07-27 15:32:36 +00:00
], $user)
->assertOk();
2016-04-24 04:37:04 +00:00
2022-07-06 16:08:55 +00:00
/** @var Album $album */
$album = Album::query()->where('name', 'One by One')->first();
2022-07-06 16:08:55 +00:00
/** @var Artist $albumArtist */
$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 */
$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,
'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
2019-07-22 07:03:23 +00:00
public function testDeletingByChunk(): void
2016-09-26 07:32:16 +00:00
{
2022-10-07 14:25:44 +00:00
self::assertNotSame(0, Song::query()->count());
$ids = Song::query()->select('id')->get()->pluck('id')->all();
2022-07-27 15:32:36 +00:00
2016-09-26 07:32:16 +00:00
Song::deleteByChunk($ids, 'id', 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
}