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;
|
|
|
|
|
2024-01-10 21:37:24 +00:00
|
|
|
use App\Events\MultipleSongsLiked;
|
|
|
|
use App\Events\MultipleSongsUnliked;
|
2018-08-18 12:27:26 +00:00
|
|
|
use App\Events\SongLikeToggled;
|
|
|
|
use App\Models\Interaction;
|
|
|
|
use App\Models\Song;
|
|
|
|
use App\Services\InteractionService;
|
|
|
|
use Illuminate\Support\Collection;
|
2024-04-18 11:27:07 +00:00
|
|
|
use Illuminate\Support\Facades\Event;
|
2024-10-24 10:45:45 +00:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2018-08-18 12:27:26 +00:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
use function Tests\create_user;
|
|
|
|
|
2018-08-18 12:27:26 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function increasePlayCount(): void
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
|
|
|
/** @var Interaction $interaction */
|
2020-11-14 16:57:25 +00:00
|
|
|
$interaction = Interaction::factory()->create();
|
2022-08-09 18:45:11 +00:00
|
|
|
$currentCount = $interaction->play_count;
|
2018-08-18 12:27:26 +00:00
|
|
|
$this->interactionService->increasePlayCount($interaction->song, $interaction->user);
|
|
|
|
|
2022-10-07 14:25:44 +00:00
|
|
|
self::assertSame($currentCount + 1, $interaction->refresh()->play_count);
|
2018-08-18 12:27:26 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function toggleLike(): void
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
2024-04-18 11:27:07 +00:00
|
|
|
Event::fake(SongLikeToggled::class);
|
2018-08-18 12:27:26 +00:00
|
|
|
|
2021-07-26 21:21:36 +00:00
|
|
|
/** @var Interaction $interaction */
|
2020-11-14 16:57:25 +00:00
|
|
|
$interaction = Interaction::factory()->create();
|
2022-08-09 18:45:11 +00:00
|
|
|
$currentLiked = $interaction->liked;
|
2018-08-18 12:27:26 +00:00
|
|
|
|
|
|
|
$this->interactionService->toggleLike($interaction->song, $interaction->user);
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
self::assertNotSame($currentLiked, $interaction->refresh()->liked);
|
2024-04-18 11:27:07 +00:00
|
|
|
Event::assertDispatched(SongLikeToggled::class);
|
2018-08-18 12:27:26 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function likeMultipleSongs(): void
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
2024-04-18 11:27:07 +00:00
|
|
|
Event::fake(MultipleSongsLiked::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();
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2018-08-18 12:27:26 +00:00
|
|
|
|
2024-01-10 21:37:24 +00:00
|
|
|
$this->interactionService->likeMany($songs, $user);
|
2018-08-18 12:27:26 +00:00
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
$songs->each(static function (Song $song) use ($user): void {
|
2022-08-09 18:45:11 +00:00
|
|
|
/** @var Interaction $interaction */
|
|
|
|
$interaction = Interaction::query()
|
2024-11-09 14:56:48 +00:00
|
|
|
->whereBelongsTo($song)
|
|
|
|
->whereBelongsTo($user)
|
2022-08-09 18:45:11 +00:00
|
|
|
->first();
|
|
|
|
|
|
|
|
self::assertTrue($interaction->liked);
|
2018-08-18 12:27:26 +00:00
|
|
|
});
|
2024-04-18 11:27:07 +00:00
|
|
|
|
|
|
|
Event::assertDispatched(MultipleSongsLiked::class);
|
2018-08-18 12:27:26 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function unlikeMultipleSongs(): void
|
2018-08-18 12:27:26 +00:00
|
|
|
{
|
2024-04-18 11:27:07 +00:00
|
|
|
Event::fake(MultipleSongsUnliked::class);
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2020-11-14 16:57:25 +00:00
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
$interactions = Interaction::factory(3)->for($user)->create(['liked' => true]);
|
2018-08-18 12:27:26 +00:00
|
|
|
|
2024-11-09 14:56:48 +00:00
|
|
|
$this->interactionService->unlikeMany($interactions->map(static fn (Interaction $i) => $i->song), $user); // @phpstan-ignore-line
|
2018-08-18 12:27:26 +00:00
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
$interactions->each(static function (Interaction $interaction): void {
|
2022-08-09 18:45:11 +00:00
|
|
|
self::assertFalse($interaction->refresh()->liked);
|
2018-08-18 12:27:26 +00:00
|
|
|
});
|
2024-04-18 11:27:07 +00:00
|
|
|
|
|
|
|
Event::assertDispatched(MultipleSongsUnliked::class);
|
2018-08-18 12:27:26 +00:00
|
|
|
}
|
|
|
|
}
|