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
|
|
|
|
2020-04-27 18:55:12 +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-06 16:08:55 +00:00
|
|
|
$song = Song::first();
|
2021-06-05 10:47:56 +00:00
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
$this->putAsUser('/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
|
|
|
],
|
2022-07-06 16:08:55 +00:00
|
|
|
], User::factory()->admin()->create())
|
2020-09-06 18:21:39 +00:00
|
|
|
->assertStatus(200);
|
2016-03-05 09:01:12 +00:00
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
/** @var Artist $artist */
|
2020-09-06 18:21:39 +00:00
|
|
|
$artist = Artist::where('name', 'John Cena')->first();
|
|
|
|
self::assertNotNull($artist);
|
2016-03-05 09:01:12 +00:00
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
/** @var Album $album */
|
2020-09-06 18:21:39 +00:00
|
|
|
$album = Album::where('name', 'One by One')->first();
|
|
|
|
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
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function testSingleUpdateSomeInfoNoCompilation(): void
|
2016-03-05 09:01:12 +00:00
|
|
|
{
|
2022-07-06 16:08:55 +00:00
|
|
|
$song = Song::first();
|
|
|
|
$originalArtistId = $song->artist->id;
|
2021-06-05 10:47:56 +00:00
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
$this->putAsUser('/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,
|
|
|
|
],
|
2022-07-06 16:08:55 +00:00
|
|
|
], User::factory()->admin()->create())
|
2020-09-06 18:21:39 +00:00
|
|
|
->assertStatus(200);
|
2016-03-05 09:01:12 +00:00
|
|
|
|
|
|
|
// We don't expect the song's artist to change
|
2022-07-06 16:08:55 +00:00
|
|
|
self::assertEquals($originalArtistId, Song::find($song->id)->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
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertEquals('One by One', Song::find($song->id)->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-06 16:08:55 +00:00
|
|
|
$songIds = Song::latest()->take(3)->pluck('id')->toArray();
|
2016-03-05 09:01:12 +00:00
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
$this->putAsUser('/api/songs', [
|
2020-01-08 14:21:29 +00:00
|
|
|
'songs' => $songIds,
|
|
|
|
'data' => [
|
|
|
|
'title' => 'foo',
|
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' => 'bar',
|
|
|
|
'track' => 9999,
|
|
|
|
],
|
2022-07-06 16:08:55 +00:00
|
|
|
], User::factory()->admin()->create())
|
2020-09-06 18:21:39 +00:00
|
|
|
->assertStatus(200);
|
2016-03-05 09:01:12 +00:00
|
|
|
|
2022-07-06 16:08:55 +00:00
|
|
|
$songs = Song::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
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertEquals('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-07-06 16:08:55 +00:00
|
|
|
self::assertEquals('John Cena', $songs[0]->artist->name);
|
|
|
|
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-06 16:08:55 +00:00
|
|
|
/** @var array<array-key, Song>|Collection $originalSongs */
|
|
|
|
$originalSongs = Song::latest()->take(3)->get();
|
2016-03-05 09:01:12 +00:00
|
|
|
$songIds = $originalSongs->pluck('id')->toArray();
|
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
$this->putAsUser('/api/songs', [
|
2020-01-08 14:21:29 +00:00
|
|
|
'songs' => $songIds,
|
|
|
|
'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,
|
|
|
|
],
|
2022-07-06 16:08:55 +00:00
|
|
|
], User::factory()->admin()->create())
|
2020-09-06 18:21:39 +00:00
|
|
|
->assertStatus(200);
|
2016-03-05 09:01:12 +00:00
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
/** @var array<Song>|Collection $songs */
|
2022-07-06 16:08:55 +00:00
|
|
|
$songs = Song::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.
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertEquals($songs[0]->album->name, $originalSongs[0]->album->name);
|
|
|
|
self::assertNotEquals($songs[0]->album->id, $originalSongs[0]->album->id);
|
|
|
|
self::assertEquals($songs[1]->album->name, $originalSongs[1]->album->name);
|
|
|
|
self::assertNotEquals($songs[1]->album->id, $originalSongs[1]->album->id);
|
|
|
|
self::assertEquals($songs[2]->album->name, $originalSongs[2]->album->name);
|
|
|
|
self::assertNotEquals($songs[2]->album->id, $originalSongs[2]->album->id);
|
2016-03-05 09:01:12 +00:00
|
|
|
|
|
|
|
// And of course, the new artist is...
|
2022-07-06 16:08:55 +00:00
|
|
|
self::assertEquals('John Cena', $songs[0]->artist->name); // JOHN CENA!!!
|
|
|
|
self::assertEquals('John Cena', $songs[1]->artist->name); // JOHN CENA!!!
|
|
|
|
self::assertEquals('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-06 16:08:55 +00:00
|
|
|
$song = Song::first();
|
2021-06-05 10:47:56 +00:00
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
$this->putAsUser('/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
|
|
|
],
|
2022-07-06 16:08:55 +00:00
|
|
|
], User::factory()->admin()->create())
|
2020-09-06 18:21:39 +00:00
|
|
|
->assertStatus(200);
|
2016-04-24 04:37:04 +00:00
|
|
|
|
2022-07-06 16:08:55 +00:00
|
|
|
/** @var Album $album */
|
2022-07-07 10:45:57 +00:00
|
|
|
$album = Album::where('name', 'One by One')->first();
|
2022-07-06 16:08:55 +00:00
|
|
|
|
|
|
|
/** @var Artist $albumArtist */
|
|
|
|
$albumArtist = Artist::whereName('John Lennon')->first();
|
2016-04-24 04:37:04 +00:00
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
/** @var Artist $artist */
|
2017-04-23 16:01:02 +00:00
|
|
|
$artist = Artist::whereName('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
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function testDeletingByChunk(): void
|
2016-09-26 07:32:16 +00:00
|
|
|
{
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertNotEquals(0, Song::count());
|
2016-09-26 07:32:16 +00:00
|
|
|
$ids = Song::select('id')->get()->pluck('id')->all();
|
|
|
|
Song::deleteByChunk($ids, 'id', 1);
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertEquals(0, Song::count());
|
2016-09-26 07:32:16 +00:00
|
|
|
}
|
2016-03-05 09:01:12 +00:00
|
|
|
}
|