koel/tests/Integration/Services/InteractionServiceTest.php

86 lines
2.5 KiB
PHP
Raw Normal View History

2018-08-18 14:27:26 +02:00
<?php
2018-08-18 15:20:02 +02:00
2018-08-18 14:27:26 +02:00
namespace Tests\Integration\Services;
2024-01-10 22:37:24 +01:00
use App\Events\MultipleSongsLiked;
use App\Events\MultipleSongsUnliked;
2018-08-18 14:27:26 +02:00
use App\Events\SongLikeToggled;
use App\Models\Interaction;
use App\Models\Song;
use App\Services\InteractionService;
use Illuminate\Support\Collection;
use Tests\TestCase;
2024-01-11 13:41:33 +01:00
use function Tests\create_user;
2018-08-18 14:27:26 +02:00
class InteractionServiceTest extends TestCase
{
2021-06-05 12:47:56 +02:00
private InteractionService $interactionService;
2018-08-18 14:27:26 +02:00
2019-07-22 09:03:23 +02:00
public function setUp(): void
2018-08-18 14:27:26 +02:00
{
parent::setUp();
$this->interactionService = new InteractionService();
2018-08-18 14:27:26 +02:00
}
2020-12-22 21:11:22 +01:00
public function testIncreasePlayCount(): void
2018-08-18 14:27:26 +02:00
{
/** @var Interaction $interaction */
$interaction = Interaction::factory()->create();
$currentCount = $interaction->play_count;
2018-08-18 14:27:26 +02:00
$this->interactionService->increasePlayCount($interaction->song, $interaction->user);
2022-10-07 22:25:44 +08:00
self::assertSame($currentCount + 1, $interaction->refresh()->play_count);
2018-08-18 14:27:26 +02:00
}
public function testToggleLike(): void
2018-08-18 14:27:26 +02:00
{
$this->expectsEvents(SongLikeToggled::class);
2021-07-26 23:21:36 +02:00
/** @var Interaction $interaction */
$interaction = Interaction::factory()->create();
$currentLiked = $interaction->liked;
2018-08-18 14:27:26 +02:00
$this->interactionService->toggleLike($interaction->song, $interaction->user);
self::assertNotSame($currentLiked, $interaction->refresh()->liked);
2018-08-18 14:27:26 +02:00
}
public function testLikeMultipleSongs(): void
2018-08-18 14:27:26 +02:00
{
2024-01-10 22:37:24 +01:00
$this->expectsEvents(MultipleSongsLiked::class);
2018-08-18 14:27:26 +02:00
/** @var Collection $songs */
$songs = Song::factory(2)->create();
2024-01-11 13:41:33 +01:00
$user = create_user();
2018-08-18 14:27:26 +02:00
2024-01-10 22:37:24 +01:00
$this->interactionService->likeMany($songs, $user);
2018-08-18 14:27:26 +02:00
$songs->each(static function (Song $song) use ($user): void {
/** @var Interaction $interaction */
$interaction = Interaction::query()
->where('song_id', $song->id)
->where('user_id', $user->id)
->first();
self::assertTrue($interaction->liked);
2018-08-18 14:27:26 +02:00
});
}
public function testUnlikeMultipleSongs(): void
2018-08-18 14:27:26 +02:00
{
2024-01-10 22:37:24 +01:00
$this->expectsEvents(MultipleSongsUnliked::class);
2024-01-11 13:41:33 +01:00
$user = create_user();
2018-08-18 14:27:26 +02:00
/** @var Collection $interactions */
2023-06-05 23:46:41 +02:00
$interactions = Interaction::factory(3)->for($user)->create(['liked' => true]);
2018-08-18 14:27:26 +02:00
2024-01-10 22:37:24 +01:00
$this->interactionService->unlikeMany($interactions->map(static fn (Interaction $i) => $i->song), $user);
2018-08-18 14:27:26 +02:00
$interactions->each(static function (Interaction $interaction): void {
self::assertFalse($interaction->refresh()->liked);
2018-08-18 14:27:26 +02:00
});
}
}