2022-08-10 14:56:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services;
|
|
|
|
|
2022-09-08 05:06:49 +00:00
|
|
|
use App\Models\Playlist;
|
|
|
|
use App\Models\PlaylistFolder;
|
2022-08-10 14:56:01 +00:00
|
|
|
use App\Services\PlaylistFolderService;
|
2022-09-08 05:06:49 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2022-08-10 14:56:01 +00:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
use function Tests\create_user;
|
|
|
|
|
2022-08-10 14:56:01 +00:00
|
|
|
class PlaylistFolderServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
private PlaylistFolderService $service;
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->service = new PlaylistFolderService();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreate(): void
|
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2022-08-10 14:56:01 +00:00
|
|
|
|
|
|
|
self::assertCount(0, $user->playlist_folders);
|
|
|
|
|
|
|
|
$this->service->createFolder($user, 'Classical');
|
|
|
|
|
|
|
|
self::assertCount(1, $user->refresh()->playlist_folders);
|
|
|
|
self::assertSame('Classical', $user->playlist_folders[0]->name);
|
|
|
|
}
|
2022-09-08 05:06:49 +00:00
|
|
|
|
|
|
|
public function testUpdate(): void
|
|
|
|
{
|
|
|
|
/** @var PlaylistFolder $folder */
|
|
|
|
$folder = PlaylistFolder::factory()->create(['name' => 'Metal']);
|
|
|
|
|
|
|
|
$this->service->renameFolder($folder, 'Classical');
|
|
|
|
|
|
|
|
self::assertSame('Classical', $folder->fresh()->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddPlaylistsToFolder(): void
|
|
|
|
{
|
2024-03-27 09:53:05 +00:00
|
|
|
$user = create_user();
|
|
|
|
|
2022-09-08 05:06:49 +00:00
|
|
|
/** @var Collection|array<array-key, Playlist> $playlists */
|
2024-03-27 09:53:05 +00:00
|
|
|
$playlists = Playlist::factory()->for($user)->count(3)->create();
|
2022-09-08 05:06:49 +00:00
|
|
|
|
|
|
|
/** @var PlaylistFolder $folder */
|
2024-03-27 09:53:05 +00:00
|
|
|
$folder = PlaylistFolder::factory()->for($user)->create();
|
2022-09-08 05:06:49 +00:00
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
$this->service->addPlaylistsToFolder($folder, $playlists->pluck('id')->all());
|
2022-09-08 05:06:49 +00:00
|
|
|
|
|
|
|
self::assertCount(3, $folder->playlists);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMovePlaylistsToRootLevel(): void
|
|
|
|
{
|
|
|
|
/** @var PlaylistFolder $folder */
|
|
|
|
$folder = PlaylistFolder::factory()->create();
|
|
|
|
|
|
|
|
/** @var Collection|array<array-key, Playlist> $playlists */
|
2024-03-27 09:53:05 +00:00
|
|
|
$playlists = Playlist::factory()->count(3)->create();
|
|
|
|
$folder->playlists()->attach($playlists->pluck('id')->all());
|
2022-09-08 05:06:49 +00:00
|
|
|
|
2024-03-27 09:53:05 +00:00
|
|
|
$this->service->movePlaylistsToRootLevel($folder, $playlists->pluck('id')->all());
|
2022-09-08 05:06:49 +00:00
|
|
|
|
|
|
|
self::assertCount(0, $folder->playlists);
|
|
|
|
|
2024-03-27 09:53:05 +00:00
|
|
|
$playlists->each(static fn (Playlist $playlist) => self::assertNull($playlist->refresh()->getFolder()));
|
2022-09-08 05:06:49 +00:00
|
|
|
}
|
2022-08-10 14:56:01 +00:00
|
|
|
}
|