2022-07-26 21:05:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\V6;
|
|
|
|
|
2023-04-17 19:45:43 +00:00
|
|
|
use App\Events\SongStartedPlaying;
|
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
|
|
|
{
|
2023-04-17 19:45:43 +00:00
|
|
|
Event::fake(SongStartedPlaying::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);
|
2023-04-17 19:45:43 +00:00
|
|
|
Event::assertDispatched(SongStartedPlaying::class);
|
2022-07-26 21:05:43 +00:00
|
|
|
}
|
2022-07-27 09:30:04 +00:00
|
|
|
|
|
|
|
public function testStoreNewEntry(): void
|
|
|
|
{
|
2023-04-17 19:45:43 +00:00
|
|
|
Event::fake(SongStartedPlaying::class);
|
|
|
|
|
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);
|
2023-04-17 19:45:43 +00:00
|
|
|
Event::assertDispatched(SongStartedPlaying::class);
|
2022-07-27 09:30:04 +00:00
|
|
|
}
|
2022-07-26 21:05:43 +00:00
|
|
|
}
|