2018-08-18 12:27:26 +00:00
|
|
|
<?php
|
2018-08-18 13:20:02 +00:00
|
|
|
|
2018-08-18 12:27:26 +00:00
|
|
|
namespace Tests\Integration\Services;
|
|
|
|
|
|
|
|
use App\Events\SongLikeToggled;
|
2021-06-04 15:19:33 +00:00
|
|
|
use App\Events\SongsBatchLiked;
|
|
|
|
use App\Events\SongsBatchUnliked;
|
2018-08-18 12:27:26 +00:00
|
|
|
use App\Models\Interaction;
|
|
|
|
use App\Models\Song;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Services\InteractionService;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class InteractionServiceTest extends TestCase
|
|
|
|
{
|
2021-06-05 10:47:56 +00:00
|
|
|
private InteractionService $interactionService;
|
2018-08-18 12:27:26 +00:00
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function setUp(): void
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2021-06-04 15:19:33 +00:00
|
|
|
$this->interactionService = new InteractionService();
|
2018-08-18 12:27:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
public function testIncreasePlayCount(): void
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
|
|
|
/** @var Interaction $interaction */
|
2020-11-14 16:57:25 +00:00
|
|
|
$interaction = Interaction::factory()->create();
|
2018-08-18 12:27:26 +00:00
|
|
|
|
|
|
|
$this->interactionService->increasePlayCount($interaction->song, $interaction->user);
|
|
|
|
|
|
|
|
$updatedInteraction = Interaction::find($interaction->id);
|
|
|
|
self::assertEquals($interaction->play_count + 1, $updatedInteraction->play_count);
|
|
|
|
}
|
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
public function testToggleLike(): void
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
|
|
|
$this->expectsEvents(SongLikeToggled::class);
|
|
|
|
|
2021-07-26 21:21:36 +00:00
|
|
|
/** @var Interaction $interaction */
|
2020-11-14 16:57:25 +00:00
|
|
|
$interaction = Interaction::factory()->create();
|
2018-08-18 12:27:26 +00:00
|
|
|
|
|
|
|
$this->interactionService->toggleLike($interaction->song, $interaction->user);
|
|
|
|
|
|
|
|
$updatedInteraction = Interaction::find($interaction->id);
|
|
|
|
self::assertNotSame($interaction->liked, $updatedInteraction->liked);
|
|
|
|
}
|
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
public function testLikeMultipleSongs(): void
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
2021-06-04 15:19:33 +00:00
|
|
|
$this->expectsEvents(SongsBatchLiked::class);
|
2018-08-18 12:27:26 +00:00
|
|
|
|
|
|
|
/** @var Collection $songs */
|
2020-11-14 16:57:25 +00:00
|
|
|
$songs = Song::factory(2)->create();
|
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
$user = User::factory()->create();
|
2018-08-18 12:27:26 +00:00
|
|
|
|
|
|
|
$this->interactionService->batchLike($songs->pluck('id')->all(), $user);
|
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
$songs->each(static function (Song $song) use ($user): void {
|
2018-08-18 12:27:26 +00:00
|
|
|
self::assertTrue(Interaction::whereSongIdAndUserId($song->id, $user->id)->first()->liked);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
public function testUnlikeMultipleSongs(): void
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
2021-06-04 15:19:33 +00:00
|
|
|
$this->expectsEvents(SongsBatchUnliked::class);
|
2018-08-18 12:27: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();
|
|
|
|
|
2018-08-18 12:27:26 +00:00
|
|
|
/** @var Collection $interactions */
|
2020-11-14 16:57:25 +00:00
|
|
|
$interactions = Interaction::factory(3)->create([
|
2018-08-18 12:27:26 +00:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'liked' => true,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->interactionService->batchUnlike($interactions->pluck('song.id')->all(), $user);
|
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
$interactions->each(static function (Interaction $interaction): void {
|
2018-08-18 12:27:26 +00:00
|
|
|
self::assertFalse(Interaction::find($interaction->id)->liked);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|