Files
animethemes-server/tests/Feature/Events/List/PlaylistTest.php
T
paranarimasu 2e930b17ff feat: initial migration to Laravel 10 (#572)
* feat: initial migration to Laravel 10

* style: fix StyleCI findings
2023-04-18 16:22:36 -05:00

145 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Events\List;
use App\Contracts\Models\HasHashids;
use App\Events\List\Playlist\PlaylistCreated;
use App\Events\List\Playlist\PlaylistDeleted;
use App\Events\List\Playlist\PlaylistRestored;
use App\Events\List\Playlist\PlaylistUpdated;
use App\Models\Auth\User;
use App\Models\List\Playlist;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Event;
use Tests\TestCase;
/**
* Class PlaylistTest.
*/
class PlaylistTest extends TestCase
{
/**
* When a Playlist is created, a PlaylistCreated event shall be dispatched.
*
* @return void
*/
public function testPlaylistCreatedEventDispatched(): void
{
Playlist::factory()->createOne();
Event::assertDispatched(PlaylistCreated::class);
}
/**
* When a Playlist is deleted, a PlaylistDeleted event shall be dispatched.
*
* @return void
*/
public function testPlaylistDeletedEventDispatched(): void
{
$playlist = Playlist::factory()->createOne();
$playlist->delete();
Event::assertDispatched(PlaylistDeleted::class);
}
/**
* When a Playlist is restored, a PlaylistRestored event shall be dispatched.
*
* @return void
*/
public function testPlaylistRestoredEventDispatched(): void
{
$playlist = Playlist::factory()->createOne();
$playlist->restore();
Event::assertDispatched(PlaylistRestored::class);
}
/**
* When a Playlist is restored, a PlaylistUpdated event shall not be dispatched.
* Note: This is a customization that overrides default framework behavior.
* An updated event is fired on restore.
*
* @return void
*/
public function testPlaylistRestoresQuietly(): void
{
$playlist = Playlist::factory()->createOne();
$playlist->restore();
Event::assertNotDispatched(PlaylistUpdated::class);
}
/**
* When a Playlist is updated, a PlaylistUpdated event shall be dispatched.
*
* @return void
*/
public function testPlaylistUpdatedEventDispatched(): void
{
$playlist = Playlist::factory()->createOne();
$changes = Playlist::factory()->makeOne();
$playlist->fill($changes->getAttributes());
$playlist->save();
Event::assertDispatched(PlaylistUpdated::class);
}
/**
* The PlaylistUpdated event shall contain embed fields.
*
* @return void
*/
public function testPlaylistUpdatedEventEmbedFields(): void
{
$playlist = Playlist::factory()->createOne();
$changes = Playlist::factory()->makeOne();
$playlist->fill($changes->getAttributes());
$playlist->save();
Event::assertDispatched(PlaylistUpdated::class, function (PlaylistUpdated $event) {
$message = $event->getDiscordMessage();
return ! empty(Arr::get($message->embed, 'fields'));
});
}
/**
* The Playlist Created event shall assign hashids to the playlist without an owner.
*
* @return void
*/
public function testPlaylistCreatedAssignsNullableUserHashids(): void
{
Event::fakeExcept(PlaylistCreated::class);
Playlist::factory()->createOne();
static::assertDatabaseMissing(Playlist::class, [HasHashids::ATTRIBUTE_HASHID => null]);
}
/**
* The Playlist Created event shall assign hashids to the playlist with an owner.
*
* @return void
*/
public function testPlaylistCreatedAssignsNonNullUserHashids(): void
{
Event::fakeExcept(PlaylistCreated::class);
Playlist::factory()
->for(User::factory())
->createOne();
static::assertDatabaseMissing(Playlist::class, [HasHashids::ATTRIBUTE_HASHID => null]);
}
}