koel/tests/Feature/InteractionTest.php

111 lines
2.8 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;
2015-12-21 13:49:00 +00:00
use App\Events\SongLikeToggled;
2015-12-14 13:22:39 +00:00
use App\Models\Song;
use App\Models\User;
2018-08-24 15:27:19 +00:00
use Exception;
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
{
2018-08-24 15:27:19 +00:00
/**
* @throws Exception
*/
2019-07-22 07:03:23 +00:00
public function setUp(): void
2015-12-13 04:42:28 +00:00
{
parent::setUp();
static::createSampleMediaSet();
2015-12-13 04:42:28 +00:00
}
2017-08-05 18:55:02 +00:00
/** @test */
2019-07-22 07:03:23 +00:00
public function play_count_is_increased(): void
2015-12-13 04:42:28 +00:00
{
2015-12-23 06:26:16 +00:00
$this->withoutEvents();
2015-12-13 04:42:28 +00:00
$user = factory(User::class)->create();
$song = Song::orderBy('id')->first();
2017-02-14 06:53:02 +00:00
$this->postAsUser('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
2017-02-14 06:53:02 +00:00
$this->postAsUser('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,
]);
}
/**
* @test
2017-12-09 20:10:55 +00:00
*
2018-08-24 15:27:19 +00:00
* @throws Exception
*/
2019-07-22 07:03:23 +00:00
public function user_can_like_and_unlike_a_song(): void
2015-12-13 04:42:28 +00:00
{
2015-12-21 13:49:00 +00:00
$this->expectsEvents(SongLikeToggled::class);
2015-12-13 04:42:28 +00:00
$user = factory(User::class)->create();
$song = Song::orderBy('id')->first();
2017-02-14 06:53:02 +00:00
$this->postAsUser('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
2017-02-14 06:53:02 +00:00
$this->postAsUser('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,
]);
}
/**
* @test
2017-12-09 20:10:55 +00:00
*
2018-08-24 15:27:19 +00:00
* @throws Exception
*/
2019-07-22 07:03:23 +00:00
public function user_can_like_and_unlike_songs_in_batch(): void
2015-12-13 04:42:28 +00:00
{
2015-12-21 13:49:00 +00:00
$this->expectsEvents(SongLikeToggled::class);
2015-12-21 13:50:26 +00:00
2015-12-13 04:42:28 +00:00
$user = factory(User::class)->create();
$songs = Song::orderBy('id')->take(2)->get();
$songIds = array_pluck($songs->toArray(), 'id');
2017-02-14 06:53:02 +00:00
$this->postAsUser('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,
]);
}
2017-02-14 06:53:02 +00:00
$this->postAsUser('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,
]);
}
}
}