koel/tests/Feature/InteractionTest.php

106 lines
2.8 KiB
PHP
Raw Normal View History

2015-12-13 12:42:28 +08:00
<?php
2017-02-14 14:53:02 +08:00
namespace Tests\Feature;
2015-12-21 21:49:00 +08:00
use App\Events\SongLikeToggled;
use App\Events\SongsBatchLiked;
2015-12-14 08:22:39 -05:00
use App\Models\Song;
use App\Models\User;
use Illuminate\Support\Collection;
2015-12-13 12:42:28 +08:00
2017-08-05 17:56:11 +01:00
class InteractionTest extends TestCase
2015-12-13 12:42:28 +08:00
{
2019-07-22 09:03:23 +02:00
public function setUp(): void
2015-12-13 12:42:28 +08:00
{
parent::setUp();
2020-12-22 21:11:22 +01:00
static::createSampleMediaSet();
2015-12-13 12:42:28 +08:00
}
2020-12-22 21:11:22 +01:00
public function testIncreasePlayCount(): void
2015-12-13 12:42:28 +08:00
{
2015-12-23 14:26:16 +08:00
$this->withoutEvents();
2021-07-26 23:21:36 +02:00
/** @var User $user */
$user = User::factory()->create();
2015-12-13 12:42:28 +08:00
2021-07-26 23:21:36 +02:00
/** @var Song $song */
$song = Song::query()->orderBy('id')->first();
2022-07-27 10:49:33 +02:00
$this->postAs('api/interaction/play', ['song' => $song->id], $user);
2015-12-13 12:42:28 +08:00
2020-09-06 20:21:39 +02:00
self::assertDatabaseHas('interactions', [
2015-12-13 12:42:28 +08:00
'user_id' => $user->id,
'song_id' => $song->id,
'play_count' => 1,
]);
// Try again
2022-07-27 10:49:33 +02:00
$this->postAs('api/interaction/play', ['song' => $song->id], $user);
2015-12-13 12:42:28 +08:00
2020-09-06 20:21:39 +02:00
self::assertDatabaseHas('interactions', [
2015-12-13 12:42:28 +08:00
'user_id' => $user->id,
'song_id' => $song->id,
'play_count' => 2,
]);
}
2020-12-22 21:11:22 +01:00
public function testToggle(): void
2015-12-13 12:42:28 +08:00
{
2015-12-21 21:49:00 +08:00
$this->expectsEvents(SongLikeToggled::class);
2021-07-26 23:21:36 +02:00
/** @var User $user */
$user = User::factory()->create();
2015-12-13 12:42:28 +08:00
2021-07-26 23:21:36 +02:00
/** @var Song $song */
$song = Song::query()->orderBy('id')->first();
2022-07-27 10:49:33 +02:00
$this->postAs('api/interaction/like', ['song' => $song->id], $user);
2015-12-13 12:42:28 +08:00
2020-09-06 20:21:39 +02:00
self::assertDatabaseHas('interactions', [
2015-12-13 12:42:28 +08:00
'user_id' => $user->id,
'song_id' => $song->id,
'liked' => 1,
]);
// Try again
2022-07-27 10:49:33 +02:00
$this->postAs('api/interaction/like', ['song' => $song->id], $user);
2015-12-13 12:42:28 +08:00
2020-09-06 20:21:39 +02:00
self::assertDatabaseHas('interactions', [
2015-12-13 12:42:28 +08:00
'user_id' => $user->id,
'song_id' => $song->id,
'liked' => 0,
]);
}
2020-12-22 21:11:22 +01:00
public function testToggleBatch(): void
2015-12-13 12:42:28 +08:00
{
$this->expectsEvents(SongsBatchLiked::class);
2015-12-21 08:50:26 -05:00
/** @var User $user */
$user = User::factory()->create();
2015-12-13 12:42:28 +08:00
/** @var Collection|array<Song> $songs */
$songs = Song::query()->orderBy('id')->take(2)->get();
2023-06-05 23:46:41 +02:00
$songIds = $songs->pluck('id')->all();
2015-12-13 12:42:28 +08:00
2022-07-27 10:49:33 +02:00
$this->postAs('api/interaction/batch/like', ['songs' => $songIds], $user);
2015-12-13 12:42:28 +08:00
foreach ($songs as $song) {
2020-09-06 20:21:39 +02:00
self::assertDatabaseHas('interactions', [
2015-12-13 12:42:28 +08:00
'user_id' => $user->id,
'song_id' => $song->id,
'liked' => 1,
]);
}
2022-07-27 10:49:33 +02:00
$this->postAs('api/interaction/batch/unlike', ['songs' => $songIds], $user);
2015-12-13 12:42:28 +08:00
foreach ($songs as $song) {
2020-09-06 20:21:39 +02:00
self::assertDatabaseHas('interactions', [
2015-12-13 12:42:28 +08:00
'user_id' => $user->id,
'song_id' => $song->id,
'liked' => 0,
]);
}
}
}