From e4ca67bc69b979cdc1aa3f92638a2c799e0950bf Mon Sep 17 00:00:00 2001 From: Phan An Date: Wed, 27 Jul 2022 12:44:25 +0200 Subject: [PATCH] fix(test): broken tests --- app/Values/AlbumInformation.php | 3 +- tests/Feature/PlaylistSongTest.php | 41 +++++-------------- .../Services/LastfmServiceTest.php | 9 ++-- tests/blobs/lastfm/album.json | 4 +- 4 files changed, 18 insertions(+), 39 deletions(-) diff --git a/app/Values/AlbumInformation.php b/app/Values/AlbumInformation.php index d9233a6b..3d9e9849 100644 --- a/app/Values/AlbumInformation.php +++ b/app/Values/AlbumInformation.php @@ -3,6 +3,7 @@ namespace App\Values; use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Support\Arr; final class AlbumInformation implements Arrayable { @@ -25,7 +26,7 @@ final class AlbumInformation implements Arrayable { return self::make( url: $data->url, - cover: $data->cover, + cover: Arr::get($data->image, '0.#text'), wiki: [ 'summary' => isset($data->wiki) ? self::formatLastFmText($data->wiki->summary) : '', 'full' => isset($data->wiki) ? self::formatLastFmText($data->wiki->content) : '', diff --git a/tests/Feature/PlaylistSongTest.php b/tests/Feature/PlaylistSongTest.php index 503b2f6d..f2cfa6e5 100644 --- a/tests/Feature/PlaylistSongTest.php +++ b/tests/Feature/PlaylistSongTest.php @@ -5,7 +5,6 @@ namespace Tests\Feature; use App\Models\Playlist; use App\Models\Song; use App\Models\User; -use Illuminate\Support\Collection; class PlaylistSongTest extends TestCase { @@ -33,49 +32,31 @@ class PlaylistSongTest extends TestCase $user = User::factory()->create(); /** @var Playlist $playlist */ - $playlist = Playlist::factory()->create([], $user); + $playlist = Playlist::factory()->for($user)->create(); - /** @var array|Collection $songs */ - $songs = Song::orderBy('id')->take(4)->get(); - $playlist->songs()->attach($songs->pluck('id')->all()); - - /** @var Song $removedSong */ - $removedSong = $songs->pop(); + $toRemainSongs = Song::factory(3)->create(); + $toBeRemovedSongs = Song::factory(2)->create(); + $playlist->songs()->attach($toRemainSongs->merge($toBeRemovedSongs)); $path = $useDeprecatedRoute ? "api/playlist/$playlist->id/sync" : "api/playlist/$playlist->id/songs"; - $this->putAs($path, [ - 'songs' => $songs->pluck('id')->all(), - ], $user)->assertOk(); + $this->putAs($path, ['songs' => $toRemainSongs->pluck('id')->all()], $user)->assertOk(); - // We should still see the first 3 songs, but not the removed one - foreach ($songs as $song) { - self::assertDatabaseHas('playlist_song', [ - 'playlist_id' => $playlist->id, - 'song_id' => $song->id, - ]); - } - - self::assertDatabaseMissing('playlist_song', [ - 'playlist_id' => $playlist->id, - 'song_id' => $removedSong->id, - ]); + self::assertEqualsCanonicalizing( + $toRemainSongs->pluck('id')->all(), + $playlist->refresh()->songs->pluck('id')->all() + ); } public function testGetPlaylistSongs(): void { - /** @var User $user */ - $user = User::factory()->create(); - /** @var Playlist $playlist */ - $playlist = Playlist::factory()->create([ - 'user_id' => $user->id, - ]); + $playlist = Playlist::factory()->create(); $songs = Song::factory(2)->create(); $playlist->songs()->saveMany($songs); - $this->getAs("api/playlist/$playlist->id/songs", $user) + $this->getAs("api/playlist/$playlist->id/songs", $playlist->user) ->assertJson($songs->pluck('id')->all()); } } diff --git a/tests/Integration/Services/LastfmServiceTest.php b/tests/Integration/Services/LastfmServiceTest.php index 6bcb573f..ce9a437f 100644 --- a/tests/Integration/Services/LastfmServiceTest.php +++ b/tests/Integration/Services/LastfmServiceTest.php @@ -60,10 +60,7 @@ class LastfmServiceTest extends TestCase $artist = Artist::factory()->create(['name' => 'bar']); /** @var Album $album */ - $album = Album::factory()->create([ - 'artist_id' => $artist->id, - 'name' => 'foo', - ]); + $album = Album::factory()->for($artist)->create(['name' => 'foo']); /** @var Client $client */ $client = Mockery::mock(Client::class, [ @@ -79,12 +76,12 @@ class LastfmServiceTest extends TestCase 'tracks' => [ [ 'title' => 'Track 1', - 'url' => 'http://foo/track1', + 'url' => 'https://foo/track1', 'length' => 100, ], [ 'title' => 'Track 2', - 'url' => 'http://foo/track2', + 'url' => 'https://foo/track2', 'length' => 150, ], ], diff --git a/tests/blobs/lastfm/album.json b/tests/blobs/lastfm/album.json index ee0c5237..3aa222d7 100644 --- a/tests/blobs/lastfm/album.json +++ b/tests/blobs/lastfm/album.json @@ -36,7 +36,7 @@ "track": [ { "name": "Track 1", - "url": "http://foo/track1", + "url": "https://foo/track1", "duration": "100", "@attr": { "rank": "1" @@ -53,7 +53,7 @@ }, { "name": "Track 2", - "url": "http://foo/track2", + "url": "https://foo/track2", "duration": "150", "@attr": { "rank": "2"