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
|
|
|
|
2024-01-02 13:10:06 +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 App\Models\User;
|
2023-04-17 19:45:43 +00:00
|
|
|
use Illuminate\Support\Facades\Event;
|
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
|
|
|
{
|
2024-01-02 13:10:06 +00:00
|
|
|
Event::fake(PlaybackStarted::class);
|
2023-04-17 19:45:43 +00:00
|
|
|
|
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);
|
2024-01-02 13:10:06 +00:00
|
|
|
Event::assertDispatched(PlaybackStarted::class);
|
2022-07-26 21:05:43 +00:00
|
|
|
}
|
2022-07-27 09:30:04 +00:00
|
|
|
|
|
|
|
public function testStoreNewEntry(): void
|
|
|
|
{
|
2024-01-02 13:10:06 +00:00
|
|
|
Event::fake(PlaybackStarted::class);
|
2023-04-17 19:45:43 +00:00
|
|
|
|
2022-07-27 09:30:04 +00:00
|
|
|
/** @var Song $song */
|
|
|
|
$song = Song::factory()->create();
|
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
$this->postAs('/api/interaction/play', ['song' => $song->id], $user)
|
|
|
|
->assertJsonStructure([
|
|
|
|
'type',
|
|
|
|
'id',
|
|
|
|
'song_id',
|
|
|
|
'liked',
|
|
|
|
'play_count',
|
|
|
|
]);
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
/** @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);
|
2024-01-02 13:10:06 +00:00
|
|
|
Event::assertDispatched(PlaybackStarted::class);
|
2022-07-27 09:30:04 +00:00
|
|
|
}
|
2022-07-26 21:05:43 +00:00
|
|
|
}
|