2021-10-11 11:30:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
|
|
use App\Models\Playlist;
|
|
|
|
use App\Models\Song;
|
|
|
|
use App\Models\User;
|
|
|
|
|
|
|
|
class PlaylistSongTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testUpdatePlaylistSongs(): void
|
|
|
|
{
|
|
|
|
$this->doTestUpdatePlaylistSongs();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @deprecated */
|
|
|
|
public function testSyncPlaylist(): void
|
|
|
|
{
|
|
|
|
$this->doTestUpdatePlaylistSongs(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function doTestUpdatePlaylistSongs(bool $useDeprecatedRoute = false): void
|
|
|
|
{
|
|
|
|
/** @var User $user */
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
/** @var Playlist $playlist */
|
2022-07-27 10:44:25 +00:00
|
|
|
$playlist = Playlist::factory()->for($user)->create();
|
2021-10-11 11:30:27 +00:00
|
|
|
|
2022-07-27 10:44:25 +00:00
|
|
|
$toRemainSongs = Song::factory(3)->create();
|
|
|
|
$toBeRemovedSongs = Song::factory(2)->create();
|
|
|
|
$playlist->songs()->attach($toRemainSongs->merge($toBeRemovedSongs));
|
2021-10-11 11:30:27 +00:00
|
|
|
|
|
|
|
$path = $useDeprecatedRoute ? "api/playlist/$playlist->id/sync" : "api/playlist/$playlist->id/songs";
|
|
|
|
|
2022-07-29 06:47:10 +00:00
|
|
|
$this->putAs($path, ['songs' => $toRemainSongs->pluck('id')->all()], $user)->assertNoContent();
|
2021-10-11 11:30:27 +00:00
|
|
|
|
2022-07-27 10:44:25 +00:00
|
|
|
self::assertEqualsCanonicalizing(
|
|
|
|
$toRemainSongs->pluck('id')->all(),
|
|
|
|
$playlist->refresh()->songs->pluck('id')->all()
|
|
|
|
);
|
2021-10-11 11:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPlaylistSongs(): void
|
|
|
|
{
|
|
|
|
/** @var Playlist $playlist */
|
2022-07-27 10:44:25 +00:00
|
|
|
$playlist = Playlist::factory()->create();
|
2021-10-11 11:30:27 +00:00
|
|
|
|
|
|
|
$songs = Song::factory(2)->create();
|
|
|
|
$playlist->songs()->saveMany($songs);
|
|
|
|
|
2022-07-27 10:44:25 +00:00
|
|
|
$this->getAs("api/playlist/$playlist->id/songs", $playlist->user)
|
2021-10-11 11:30:27 +00:00
|
|
|
->assertJson($songs->pluck('id')->all());
|
|
|
|
}
|
|
|
|
}
|