feat(test): add missing tests for Playlist services

This commit is contained in:
Phan An 2024-01-26 12:06:57 +01:00
parent 3942fb5811
commit 69dc61b06b
4 changed files with 56 additions and 4 deletions

View file

@ -24,7 +24,7 @@ context('Song Editing', { scrollBehavior: false }, () => {
cy.get('[name=title]').clear().type('New Title')
cy.findByTestId('edit-song-lyrics-tab').click()
cy.get('textarea[name=lyrics]').should('be.visible').and('contain.value', 'Sample song lyrics')
.clear().type('New lyrics{enter}Supports multiline.')
.clear().type('New lyrics{enter}Fake multiline.')
cy.get('button[type=submit]').click()
})

View file

@ -1,9 +1,10 @@
<?php
namespace App\Services\License;
namespace Tests\Fakes;
use App\Exceptions\MethodNotImplementedException;
use App\Models\License;
use App\Services\License\LicenseServiceInterface;
use App\Values\LicenseStatus;
class FakePlusLicenseService implements LicenseServiceInterface

View file

@ -9,6 +9,7 @@ use App\Services\PlaylistService;
use App\Values\SmartPlaylistRuleGroupCollection;
use Illuminate\Support\Collection;
use InvalidArgumentException as BaseInvalidArgumentException;
use Tests\PlusTestCase;
use Tests\TestCase;
use Webmozart\Assert\InvalidArgumentException;
@ -187,4 +188,49 @@ class PlaylistServiceTest extends TestCase
ownSongsOnly: true
);
}
public function testAddSongsToPlaylist(): void
{
/** @var Playlist $playlist */
$playlist = Playlist::factory()->create();
$playlist->addSongs(Song::factory(3)->create());
$songs = Song::factory(2)->create();
$addedSongs = $this->service->addSongsToPlaylist($playlist, $songs, $playlist->user);
$playlist->refresh();
self::assertCount(2, $addedSongs);
self::assertCount(5, $playlist->songs);
self::assertEqualsCanonicalizing($addedSongs->pluck('id')->all(), $songs->pluck('id')->all());
$songs->each(static fn (Song $song) => self::assertTrue($playlist->songs->contains($song)));
}
public function testPrivateSongsAreMadePublicWhenAddedToCollaborativePlaylist(): void
{
PlusTestCase::enablePlusLicense();
/** @var Playlist $playlist */
$playlist = Playlist::factory()->create();
$user = create_user();
$playlist->collaborators()->attach($user);
$playlist->refresh();
self::assertTrue($playlist->is_collaborative);
$songs = Song::factory(2)->create(['is_public' => false]);
$this->service->addSongsToPlaylist($playlist, $songs, $user);
$songs->each(static fn (Song $song) => self::assertTrue($song->refresh()->is_public));
}
public function testMakePlaylistSongsPublic(): void
{
/** @var Playlist $playlist */
$playlist = Playlist::factory()->create();
$playlist->addSongs(Song::factory(2)->create(['is_public' => false]));
$this->service->makePlaylistSongsPublic($playlist);
$playlist->songs->each(static fn (Song $song) => self::assertTrue($song->is_public));
}
}

View file

@ -3,15 +3,20 @@
namespace Tests;
use App\Facades\License;
use App\Services\License\FakePlusLicenseService;
use Tests\Fakes\FakePlusLicenseService;
use Tests\TestCase as BaseTestCase;
class PlusTestCase extends BaseTestCase
{
public static function enablePlusLicense(): void
{
License::swap(app(FakePlusLicenseService::class));
}
public function setUp(): void
{
parent::setUp();
License::swap(app()->make(FakePlusLicenseService::class));
self::enablePlusLicense();
}
}