koel/tests/Feature/InteractionTest.php

100 lines
2.6 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
2017-02-14 06:53:02 +00:00
namespace Tests\Feature;
2024-01-10 21:37:24 +00:00
use App\Events\MultipleSongsLiked;
use App\Events\PlaybackStarted;
2015-12-21 13:49:00 +00:00
use App\Events\SongLikeToggled;
2024-01-10 21:37:24 +00:00
use App\Models\Interaction;
2015-12-14 13:22:39 +00:00
use App\Models\Song;
use Illuminate\Support\Facades\Event;
2024-01-09 18:34:40 +00:00
use Tests\TestCase;
2015-12-13 04:42:28 +00:00
2024-01-11 12:41:33 +00:00
use function Tests\create_user;
2017-08-05 16:56:11 +00:00
class InteractionTest extends TestCase
2015-12-13 04:42:28 +00:00
{
2020-12-22 20:11:22 +00:00
public function testIncreasePlayCount(): void
2015-12-13 04:42:28 +00:00
{
Event::fake(PlaybackStarted::class);
2021-07-26 21:21:36 +00:00
2024-01-11 12:41:33 +00:00
$user = create_user();
2024-01-10 21:37:24 +00:00
$song = Song::factory()->create();
2024-04-24 21:58:19 +00:00
2022-07-27 08:49:33 +00:00
$this->postAs('api/interaction/play', ['song' => $song->id], $user);
2015-12-13 04:42:28 +00:00
2024-01-10 21:37:24 +00:00
self::assertDatabaseHas(Interaction::class, [
2015-12-13 04:42:28 +00:00
'user_id' => $user->id,
'song_id' => $song->id,
'play_count' => 1,
]);
// Try again
2022-07-27 08:49:33 +00:00
$this->postAs('api/interaction/play', ['song' => $song->id], $user);
2015-12-13 04:42:28 +00:00
2024-01-10 21:37:24 +00:00
self::assertDatabaseHas(Interaction::class, [
2015-12-13 04:42:28 +00:00
'user_id' => $user->id,
'song_id' => $song->id,
'play_count' => 2,
]);
}
2024-01-10 20:04:37 +00:00
public function testToggleLike(): void
2015-12-13 04:42:28 +00:00
{
Event::fake(SongLikeToggled::class);
2015-12-21 13:49:00 +00:00
2024-01-11 12:41:33 +00:00
$user = create_user();
2024-01-10 21:37:24 +00:00
$song = Song::factory()->create();
2024-04-24 21:58:19 +00:00
2022-07-27 08:49:33 +00:00
$this->postAs('api/interaction/like', ['song' => $song->id], $user);
2015-12-13 04:42:28 +00:00
2024-01-10 21:37:24 +00:00
self::assertDatabaseHas(Interaction::class, [
2015-12-13 04:42:28 +00:00
'user_id' => $user->id,
'song_id' => $song->id,
'liked' => 1,
]);
// Try again
2022-07-27 08:49:33 +00:00
$this->postAs('api/interaction/like', ['song' => $song->id], $user);
2015-12-13 04:42:28 +00:00
2024-01-10 21:37:24 +00:00
self::assertDatabaseHas(Interaction::class, [
2015-12-13 04:42:28 +00:00
'user_id' => $user->id,
'song_id' => $song->id,
'liked' => 0,
]);
Event::assertDispatched(SongLikeToggled::class);
2015-12-13 04:42:28 +00:00
}
2024-01-10 20:04:37 +00:00
public function testToggleLikeBatch(): void
2015-12-13 04:42:28 +00:00
{
Event::fake(MultipleSongsLiked::class);
2015-12-21 13:50:26 +00:00
2024-01-11 12:41:33 +00:00
$user = create_user();
2024-01-10 21:37:24 +00:00
$songs = Song::factory(2)->create();
2023-06-05 21:46:41 +00:00
$songIds = $songs->pluck('id')->all();
2015-12-13 04:42:28 +00:00
2022-07-27 08:49:33 +00:00
$this->postAs('api/interaction/batch/like', ['songs' => $songIds], $user);
2015-12-13 04:42:28 +00:00
foreach ($songs as $song) {
2024-01-10 21:37:24 +00:00
self::assertDatabaseHas(Interaction::class, [
2015-12-13 04:42:28 +00:00
'user_id' => $user->id,
'song_id' => $song->id,
'liked' => 1,
]);
}
2022-07-27 08:49:33 +00:00
$this->postAs('api/interaction/batch/unlike', ['songs' => $songIds], $user);
2015-12-13 04:42:28 +00:00
foreach ($songs as $song) {
2024-01-10 21:37:24 +00:00
self::assertDatabaseHas(Interaction::class, [
2015-12-13 04:42:28 +00:00
'user_id' => $user->id,
'song_id' => $song->id,
'liked' => 0,
]);
}
Event::assertDispatched(MultipleSongsLiked::class);
2015-12-13 04:42:28 +00:00
}
}