2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
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;
|
2015-12-13 04:42:28 +00:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
|
|
|
|
|
|
|
class InteractionTest extends TestCase
|
|
|
|
{
|
2016-05-30 05:57:51 +00:00
|
|
|
use DatabaseTransactions, WithoutMiddleware;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->createSampleMediaSet();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPlayCountRegister()
|
|
|
|
{
|
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();
|
|
|
|
$this->actingAs($user)
|
2016-01-26 06:32:29 +00:00
|
|
|
->post('api/interaction/play', ['song' => $song->id]);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
$this->seeInDatabase('interactions', [
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'song_id' => $song->id,
|
|
|
|
'play_count' => 1,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Try again
|
|
|
|
$this->actingAs($user)
|
2016-01-26 06:32:29 +00:00
|
|
|
->post('api/interaction/play', ['song' => $song->id]);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
$this->seeInDatabase('interactions', [
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'song_id' => $song->id,
|
|
|
|
'play_count' => 2,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLikeRegister()
|
|
|
|
{
|
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();
|
|
|
|
$this->actingAs($user)
|
2016-01-26 06:32:29 +00:00
|
|
|
->post('api/interaction/like', ['song' => $song->id]);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
$this->seeInDatabase('interactions', [
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'song_id' => $song->id,
|
|
|
|
'liked' => 1,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Try again
|
|
|
|
$this->actingAs($user)
|
2016-01-26 06:32:29 +00:00
|
|
|
->post('api/interaction/like', ['song' => $song->id]);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
$this->seeInDatabase('interactions', [
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'song_id' => $song->id,
|
|
|
|
'liked' => 0,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBatchLikeAndUnlike()
|
|
|
|
{
|
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');
|
|
|
|
|
|
|
|
$this->actingAs($user)
|
2016-01-26 06:32:29 +00:00
|
|
|
->post('api/interaction/batch/like', ['songs' => $songIds]);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
foreach ($songs as $song) {
|
|
|
|
$this->seeInDatabase('interactions', [
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'song_id' => $song->id,
|
|
|
|
'liked' => 1,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->actingAs($user)
|
2016-01-26 06:32:29 +00:00
|
|
|
->post('api/interaction/batch/unlike', ['songs' => $songIds]);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
foreach ($songs as $song) {
|
|
|
|
$this->seeInDatabase('interactions', [
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'song_id' => $song->id,
|
|
|
|
'liked' => 0,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|