for($user)->count(3)->create(); $this->getAs('api/playlists', $user) ->assertJsonStructure(['*' => PlaylistResource::JSON_STRUCTURE]) ->assertJsonCount(3, '*'); } public function testCreatingPlaylist(): void { $user = create_user(); /** @var array|Collection $songs */ $songs = Song::factory(4)->create(); $this->postAs('api/playlists', [ 'name' => 'Foo Bar', 'songs' => $songs->pluck('id')->all(), 'rules' => [], ], $user) ->assertJsonStructure(PlaylistResource::JSON_STRUCTURE); /** @var Playlist $playlist */ $playlist = Playlist::query()->latest()->first(); self::assertSame('Foo Bar', $playlist->name); self::assertTrue($playlist->ownedBy($user)); self::assertNull($playlist->getFolder()); self::assertEqualsCanonicalizing($songs->pluck('id')->all(), $playlist->songs->pluck('id')->all()); } public function testCreatingSmartPlaylist(): void { $user = create_user(); $rule = SmartPlaylistRule::make([ 'model' => 'artist.name', 'operator' => SmartPlaylistRule::OPERATOR_IS, 'value' => ['Bob Dylan'], ]); $this->postAs('api/playlists', [ 'name' => 'Smart Foo Bar', 'rules' => [ [ 'id' => '2a4548cd-c67f-44d4-8fec-34ff75c8a026', 'rules' => [$rule->toArray()], ], ], ], $user)->assertJsonStructure(PlaylistResource::JSON_STRUCTURE); /** @var Playlist $playlist */ $playlist = Playlist::query()->latest()->first(); self::assertSame('Smart Foo Bar', $playlist->name); self::assertTrue($playlist->ownedBy($user)); self::assertTrue($playlist->is_smart); self::assertCount(1, $playlist->rule_groups); self::assertNull($playlist->getFolder()); self::assertTrue($rule->equals($playlist->rule_groups[0]->rules[0])); } public function testCreatingSmartPlaylistFailsIfSongsProvided(): void { $this->postAs('api/playlists', [ 'name' => 'Smart Foo Bar', 'rules' => [ [ 'id' => '2a4548cd-c67f-44d4-8fec-34ff75c8a026', 'rules' => [ SmartPlaylistRule::make([ 'model' => 'artist.name', 'operator' => SmartPlaylistRule::OPERATOR_IS, 'value' => ['Bob Dylan'], ])->toArray(), ], ], ], 'songs' => Song::factory(3)->create()->pluck('id')->all(), ])->assertUnprocessable(); } public function testCreatingPlaylistWithNonExistentSongsFails(): void { $this->postAs('api/playlists', [ 'name' => 'Foo Bar', 'rules' => [], 'songs' => ['foo'], ]) ->assertUnprocessable(); } public function testUpdatePlaylistName(): void { /** @var Playlist $playlist */ $playlist = Playlist::factory()->create(['name' => 'Foo']); $this->putAs("api/playlists/$playlist->id", ['name' => 'Bar'], $playlist->user) ->assertJsonStructure(PlaylistResource::JSON_STRUCTURE); self::assertSame('Bar', $playlist->refresh()->name); } public function testNonOwnerCannotUpdatePlaylist(): void { /** @var Playlist $playlist */ $playlist = Playlist::factory()->create(['name' => 'Foo']); $this->putAs("api/playlists/$playlist->id", ['name' => 'Qux'])->assertForbidden(); self::assertSame('Foo', $playlist->refresh()->name); } public function testDeletePlaylist(): void { /** @var Playlist $playlist */ $playlist = Playlist::factory()->create(); $this->deleteAs("api/playlists/$playlist->id", [], $playlist->user); self::assertModelMissing($playlist); } public function testNonOwnerCannotDeletePlaylist(): void { /** @var Playlist $playlist */ $playlist = Playlist::factory()->create(); $this->deleteAs("api/playlists/$playlist->id")->assertForbidden(); self::assertModelExists($playlist); } }