fix(test): broken tests

This commit is contained in:
Phan An 2022-07-27 12:44:25 +02:00
parent 6e2f3764cc
commit e4ca67bc69
No known key found for this signature in database
GPG key ID: A81E4477F0BB6FDC
4 changed files with 18 additions and 39 deletions

View file

@ -3,6 +3,7 @@
namespace App\Values; namespace App\Values;
use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
final class AlbumInformation implements Arrayable final class AlbumInformation implements Arrayable
{ {
@ -25,7 +26,7 @@ final class AlbumInformation implements Arrayable
{ {
return self::make( return self::make(
url: $data->url, url: $data->url,
cover: $data->cover, cover: Arr::get($data->image, '0.#text'),
wiki: [ wiki: [
'summary' => isset($data->wiki) ? self::formatLastFmText($data->wiki->summary) : '', 'summary' => isset($data->wiki) ? self::formatLastFmText($data->wiki->summary) : '',
'full' => isset($data->wiki) ? self::formatLastFmText($data->wiki->content) : '', 'full' => isset($data->wiki) ? self::formatLastFmText($data->wiki->content) : '',

View file

@ -5,7 +5,6 @@ namespace Tests\Feature;
use App\Models\Playlist; use App\Models\Playlist;
use App\Models\Song; use App\Models\Song;
use App\Models\User; use App\Models\User;
use Illuminate\Support\Collection;
class PlaylistSongTest extends TestCase class PlaylistSongTest extends TestCase
{ {
@ -33,49 +32,31 @@ class PlaylistSongTest extends TestCase
$user = User::factory()->create(); $user = User::factory()->create();
/** @var Playlist $playlist */ /** @var Playlist $playlist */
$playlist = Playlist::factory()->create([], $user); $playlist = Playlist::factory()->for($user)->create();
/** @var array<Song>|Collection $songs */ $toRemainSongs = Song::factory(3)->create();
$songs = Song::orderBy('id')->take(4)->get(); $toBeRemovedSongs = Song::factory(2)->create();
$playlist->songs()->attach($songs->pluck('id')->all()); $playlist->songs()->attach($toRemainSongs->merge($toBeRemovedSongs));
/** @var Song $removedSong */
$removedSong = $songs->pop();
$path = $useDeprecatedRoute ? "api/playlist/$playlist->id/sync" : "api/playlist/$playlist->id/songs"; $path = $useDeprecatedRoute ? "api/playlist/$playlist->id/sync" : "api/playlist/$playlist->id/songs";
$this->putAs($path, [ $this->putAs($path, ['songs' => $toRemainSongs->pluck('id')->all()], $user)->assertOk();
'songs' => $songs->pluck('id')->all(),
], $user)->assertOk();
// We should still see the first 3 songs, but not the removed one self::assertEqualsCanonicalizing(
foreach ($songs as $song) { $toRemainSongs->pluck('id')->all(),
self::assertDatabaseHas('playlist_song', [ $playlist->refresh()->songs->pluck('id')->all()
'playlist_id' => $playlist->id, );
'song_id' => $song->id,
]);
}
self::assertDatabaseMissing('playlist_song', [
'playlist_id' => $playlist->id,
'song_id' => $removedSong->id,
]);
} }
public function testGetPlaylistSongs(): void public function testGetPlaylistSongs(): void
{ {
/** @var User $user */
$user = User::factory()->create();
/** @var Playlist $playlist */ /** @var Playlist $playlist */
$playlist = Playlist::factory()->create([ $playlist = Playlist::factory()->create();
'user_id' => $user->id,
]);
$songs = Song::factory(2)->create(); $songs = Song::factory(2)->create();
$playlist->songs()->saveMany($songs); $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()); ->assertJson($songs->pluck('id')->all());
} }
} }

View file

@ -60,10 +60,7 @@ class LastfmServiceTest extends TestCase
$artist = Artist::factory()->create(['name' => 'bar']); $artist = Artist::factory()->create(['name' => 'bar']);
/** @var Album $album */ /** @var Album $album */
$album = Album::factory()->create([ $album = Album::factory()->for($artist)->create(['name' => 'foo']);
'artist_id' => $artist->id,
'name' => 'foo',
]);
/** @var Client $client */ /** @var Client $client */
$client = Mockery::mock(Client::class, [ $client = Mockery::mock(Client::class, [
@ -79,12 +76,12 @@ class LastfmServiceTest extends TestCase
'tracks' => [ 'tracks' => [
[ [
'title' => 'Track 1', 'title' => 'Track 1',
'url' => 'http://foo/track1', 'url' => 'https://foo/track1',
'length' => 100, 'length' => 100,
], ],
[ [
'title' => 'Track 2', 'title' => 'Track 2',
'url' => 'http://foo/track2', 'url' => 'https://foo/track2',
'length' => 150, 'length' => 150,
], ],
], ],

View file

@ -36,7 +36,7 @@
"track": [ "track": [
{ {
"name": "Track 1", "name": "Track 1",
"url": "http://foo/track1", "url": "https://foo/track1",
"duration": "100", "duration": "100",
"@attr": { "@attr": {
"rank": "1" "rank": "1"
@ -53,7 +53,7 @@
}, },
{ {
"name": "Track 2", "name": "Track 2",
"url": "http://foo/track2", "url": "https://foo/track2",
"duration": "150", "duration": "150",
"@attr": { "@attr": {
"rank": "2" "rank": "2"