mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-09-03 11:15:04 +02:00
211 lines
5.5 KiB
PHP
211 lines
5.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Models\List;
|
|
|
|
use App\Enums\Models\List\PlaylistVisibility;
|
|
use App\Models\Auth\User;
|
|
use App\Models\List\Playlist;
|
|
use App\Models\List\Playlist\PlaylistTrack;
|
|
use App\Models\Wiki\Image;
|
|
use App\Pivots\List\PlaylistImage;
|
|
use CyrildeWit\EloquentViewable\View;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
|
use Illuminate\Foundation\Testing\WithFaker;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* Class TestCase.
|
|
*/
|
|
class PlaylistTest extends TestCase
|
|
{
|
|
use WithFaker;
|
|
|
|
/**
|
|
* The visibility attribute of a playlist shall be cast to a PlaylistVisibility enum instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testCastsSeasonToEnum(): void
|
|
{
|
|
$playlist = Playlist::factory()->createOne();
|
|
|
|
$visibility = $playlist->visibility;
|
|
|
|
static::assertInstanceOf(PlaylistVisibility::class, $visibility);
|
|
}
|
|
|
|
/**
|
|
* Anime shall be nameable.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testNameable(): void
|
|
{
|
|
$playlist = Playlist::factory()->createOne();
|
|
|
|
static::assertIsString($playlist->getName());
|
|
}
|
|
|
|
/**
|
|
* Public playlists shall be searchable.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testSearchableIfPublic(): void
|
|
{
|
|
$playlist = Playlist::factory()
|
|
->createOne([
|
|
Playlist::ATTRIBUTE_VISIBILITY => PlaylistVisibility::PUBLIC,
|
|
]);
|
|
|
|
static::assertTrue($playlist->shouldBeSearchable());
|
|
}
|
|
|
|
/**
|
|
* Playlists shall not be searchable if not public.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testNotSearchableIfNotPublic(): void
|
|
{
|
|
$visibility = null;
|
|
|
|
while ($visibility == null) {
|
|
$candidate = PlaylistVisibility::getRandomInstance();
|
|
if (PlaylistVisibility::PUBLIC()->isNot($candidate)) {
|
|
$visibility = $candidate;
|
|
}
|
|
}
|
|
|
|
$playlist = Playlist::factory()
|
|
->createOne([
|
|
Playlist::ATTRIBUTE_VISIBILITY => $visibility->value,
|
|
]);
|
|
|
|
static::assertFalse($playlist->shouldBeSearchable());
|
|
}
|
|
|
|
/**
|
|
* Playlists shall have a one-to-many polymorphic relationship to View.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testViews(): void
|
|
{
|
|
$playlist = Playlist::factory()->createOne();
|
|
|
|
views($playlist)->record();
|
|
|
|
static::assertInstanceOf(MorphMany::class, $playlist->views());
|
|
static::assertEquals(1, $playlist->views()->count());
|
|
static::assertInstanceOf(View::class, $playlist->views()->first());
|
|
}
|
|
|
|
/**
|
|
* Playlists shall belong to a User.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testUser(): void
|
|
{
|
|
$playlist = Playlist::factory()
|
|
->for(User::factory())
|
|
->createOne();
|
|
|
|
static::assertInstanceOf(BelongsTo::class, $playlist->user());
|
|
static::assertInstanceOf(User::class, $playlist->user()->first());
|
|
}
|
|
|
|
/**
|
|
* Playlists shall link to the first Track.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testFirst(): void
|
|
{
|
|
$playlist = Playlist::factory()
|
|
->createOne();
|
|
|
|
$first = PlaylistTrack::factory()
|
|
->for($playlist)
|
|
->createOne();
|
|
|
|
$playlist->fill([
|
|
Playlist::ATTRIBUTE_FIRST => $first->getKey(),
|
|
]);
|
|
|
|
$playlist->save();
|
|
|
|
static::assertInstanceOf(BelongsTo::class, $playlist->first());
|
|
static::assertInstanceOf(PlaylistTrack::class, $playlist->first()->first());
|
|
}
|
|
|
|
/**
|
|
* Playlists shall link to the last Track.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testLast(): void
|
|
{
|
|
$playlist = Playlist::factory()->createOne();
|
|
|
|
$last = PlaylistTrack::factory()
|
|
->for($playlist)
|
|
->createOne();
|
|
|
|
$playlist->fill([
|
|
Playlist::ATTRIBUTE_LAST => $last->getKey(),
|
|
]);
|
|
|
|
$playlist->save();
|
|
|
|
static::assertInstanceOf(BelongsTo::class, $playlist->last());
|
|
static::assertInstanceOf(PlaylistTrack::class, $playlist->last()->first());
|
|
}
|
|
|
|
/**
|
|
* Playlists shall have a many-to-many relationship with the type Image.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testImages(): void
|
|
{
|
|
$imageCount = $this->faker->randomDigitNotNull();
|
|
|
|
$playlist = Playlist::factory()
|
|
->has(Image::factory()->count($imageCount))
|
|
->createOne();
|
|
|
|
static::assertInstanceOf(BelongsToMany::class, $playlist->images());
|
|
static::assertEquals($imageCount, $playlist->images()->count());
|
|
static::assertInstanceOf(Image::class, $playlist->images()->first());
|
|
static::assertEquals(PlaylistImage::class, $playlist->images()->getPivotClass());
|
|
}
|
|
|
|
/**
|
|
* Playlists shall have a one-to-many relationship with the type PlaylistTrack.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testTracks(): void
|
|
{
|
|
$trackCount = $this->faker->randomDigitNotNull();
|
|
|
|
$playlist = Playlist::factory()->createOne();
|
|
|
|
PlaylistTrack::factory()
|
|
->for($playlist)
|
|
->count($trackCount)
|
|
->create();
|
|
|
|
static::assertInstanceOf(HasMany::class, $playlist->tracks());
|
|
static::assertEquals($trackCount, $playlist->tracks()->count());
|
|
static::assertInstanceOf(PlaylistTrack::class, $playlist->tracks()->first());
|
|
}
|
|
}
|