2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2015-12-21 13:49:00 +00:00
|
|
|
use App\Events\SongLikeToggled;
|
2021-06-04 15:19:33 +00:00
|
|
|
use App\Events\SongsBatchLiked;
|
2015-12-14 13:22:39 +00:00
|
|
|
use App\Models\Song;
|
|
|
|
use App\Models\User;
|
2021-06-04 15:19:33 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2024-01-09 18:34:40 +00:00
|
|
|
use Tests\TestCase;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2017-08-05 16:56:11 +00:00
|
|
|
class InteractionTest extends TestCase
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2019-07-22 07:03:23 +00:00
|
|
|
public function setUp(): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2020-12-22 20:11:22 +00:00
|
|
|
|
2020-04-27 18:55:12 +00:00
|
|
|
static::createSampleMediaSet();
|
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
|
|
|
{
|
2015-12-23 06:26:16 +00:00
|
|
|
$this->withoutEvents();
|
2021-07-26 21:21:36 +00:00
|
|
|
|
|
|
|
/** @var User $user */
|
2020-11-14 16:57:25 +00:00
|
|
|
$user = User::factory()->create();
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2021-07-26 21:21:36 +00:00
|
|
|
/** @var Song $song */
|
2022-08-09 18:45:11 +00:00
|
|
|
$song = Song::query()->orderBy('id')->first();
|
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
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertDatabaseHas('interactions', [
|
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
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertDatabaseHas('interactions', [
|
2015-12-13 04:42:28 +00:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'song_id' => $song->id,
|
|
|
|
'play_count' => 2,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
public function testToggle(): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2015-12-21 13:49:00 +00:00
|
|
|
$this->expectsEvents(SongLikeToggled::class);
|
|
|
|
|
2021-07-26 21:21:36 +00:00
|
|
|
/** @var User $user */
|
2020-11-14 16:57:25 +00:00
|
|
|
$user = User::factory()->create();
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2021-07-26 21:21:36 +00:00
|
|
|
/** @var Song $song */
|
2022-08-09 18:45:11 +00:00
|
|
|
$song = Song::query()->orderBy('id')->first();
|
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
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertDatabaseHas('interactions', [
|
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
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertDatabaseHas('interactions', [
|
2015-12-13 04:42:28 +00:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'song_id' => $song->id,
|
|
|
|
'liked' => 0,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
public function testToggleBatch(): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2021-06-04 15:19:33 +00:00
|
|
|
$this->expectsEvents(SongsBatchLiked::class);
|
2015-12-21 13:50:26 +00:00
|
|
|
|
2021-06-04 15:19:33 +00:00
|
|
|
/** @var User $user */
|
2020-11-14 16:57:25 +00:00
|
|
|
$user = User::factory()->create();
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2021-06-04 15:19:33 +00:00
|
|
|
/** @var Collection|array<Song> $songs */
|
2022-08-09 18:45:11 +00:00
|
|
|
$songs = Song::query()->orderBy('id')->take(2)->get();
|
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) {
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertDatabaseHas('interactions', [
|
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) {
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertDatabaseHas('interactions', [
|
2015-12-13 04:42:28 +00:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'song_id' => $song->id,
|
|
|
|
'liked' => 0,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|