koel/tests/Feature/PlayCountTest.php

65 lines
1.6 KiB
PHP
Raw Normal View History

2022-07-26 21:05:43 +00:00
<?php
2023-06-05 21:46:41 +00:00
namespace Tests\Feature;
2022-07-26 21:05:43 +00:00
use App\Events\PlaybackStarted;
2022-07-26 21:05:43 +00:00
use App\Models\Interaction;
2022-07-27 09:30:04 +00:00
use App\Models\Song;
use Illuminate\Support\Facades\Event;
2024-01-09 18:34:40 +00:00
use Tests\TestCase;
2022-07-26 21:05:43 +00:00
2024-01-11 12:41:33 +00:00
use function Tests\create_user;
2022-07-26 21:05:43 +00:00
class PlayCountTest extends TestCase
{
2022-07-27 09:30:04 +00:00
public function testStoreExistingEntry(): void
2022-07-26 21:05:43 +00:00
{
Event::fake(PlaybackStarted::class);
2022-07-26 21:05:43 +00:00
/** @var Interaction $interaction */
$interaction = Interaction::factory()->create([
'play_count' => 10,
]);
2022-07-27 09:30:04 +00:00
$this->postAs('/api/interaction/play', ['song' => $interaction->song->id], $interaction->user)
->assertJsonStructure([
'type',
'id',
'song_id',
'liked',
'play_count',
]);
2022-07-26 21:05:43 +00:00
2022-10-07 14:25:44 +00:00
self::assertSame(11, $interaction->refresh()->play_count);
Event::assertDispatched(PlaybackStarted::class);
2022-07-26 21:05:43 +00:00
}
2022-07-27 09:30:04 +00:00
public function testStoreNewEntry(): void
{
Event::fake(PlaybackStarted::class);
2022-07-27 09:30:04 +00:00
/** @var Song $song */
$song = Song::factory()->create();
2024-01-11 12:41:33 +00:00
$user = create_user();
2022-07-27 09:30:04 +00:00
$this->postAs('/api/interaction/play', ['song' => $song->id], $user)
->assertJsonStructure([
'type',
'id',
'song_id',
'liked',
'play_count',
]);
/** @var Interaction $interaction */
$interaction = Interaction::query()
->where('song_id', $song->id)
->where('user_id', $user->id)
->first();
2022-10-07 14:25:44 +00:00
self::assertSame(1, $interaction->play_count);
Event::assertDispatched(PlaybackStarted::class);
2022-07-27 09:30:04 +00:00
}
2022-07-26 21:05:43 +00:00
}