mirror of
https://github.com/koel/koel
synced 2024-11-10 06:34:14 +00:00
Finish Interaction unit test
This commit is contained in:
parent
5b6067426d
commit
fcf6c8a700
4 changed files with 108 additions and 0 deletions
|
@ -11,6 +11,7 @@ use Illuminate\Database\Eloquent\Model;
|
|||
* @property int play_count
|
||||
* @property Song song
|
||||
* @property User user
|
||||
* @property int id
|
||||
*/
|
||||
class Interaction extends Model
|
||||
{
|
||||
|
|
|
@ -352,4 +352,9 @@ class Song extends Model
|
|||
|
||||
return compact('bucket', 'key');
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,3 +52,12 @@ $factory->define(App\Models\Playlist::class, function ($faker) {
|
|||
'name' => $faker->name,
|
||||
];
|
||||
});
|
||||
|
||||
$factory->define(\App\Models\Interaction::class, function ($faker) {
|
||||
return [
|
||||
'song_id' => factory(\App\Models\Song::class)->create()->id,
|
||||
'user_id' => factory(\App\Models\User::class)->create()->id,
|
||||
'liked' => $faker->boolean,
|
||||
'play_count' => $faker->randomNumber,
|
||||
];
|
||||
});
|
||||
|
|
93
tests/Unit/InteractionTest.php
Normal file
93
tests/Unit/InteractionTest.php
Normal file
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Events\SongLikeToggled;
|
||||
use App\Models\Interaction;
|
||||
use App\Models\Song;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Tests\TestCase;
|
||||
|
||||
class InteractionTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function it_can_be_instantiated()
|
||||
{
|
||||
$this->assertInstanceOf(Interaction::class, new Interaction());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_increases_a_songs_play_count()
|
||||
{
|
||||
// Given an interaction
|
||||
/** @var Interaction $interaction */
|
||||
$interaction = factory(Interaction::class)->create();
|
||||
|
||||
// When I call the method to increases the song's play count
|
||||
Interaction::increasePlayCount($interaction->song, $interaction->user);
|
||||
|
||||
// Then I see the play count is increased
|
||||
/** @var Interaction $interaction */
|
||||
$updatedInteraction = Interaction::find($interaction->id);
|
||||
$this->assertEquals($interaction->play_count + 1, $updatedInteraction->play_count);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_toggles_like_status()
|
||||
{
|
||||
$this->expectsEvents(SongLikeToggled::class);
|
||||
|
||||
// Given an interaction
|
||||
$interaction = factory(Interaction::class)->create();
|
||||
|
||||
// When I call the method to toggle the song's like status by user
|
||||
Interaction::toggleLike($interaction->song, $interaction->user);
|
||||
|
||||
// Then I see the interaction's like status is toggled
|
||||
/** @var Interaction $interaction */
|
||||
$updatedInteraction = Interaction::find($interaction->id);
|
||||
$this->assertNotSame($interaction->liked, $updatedInteraction->liked);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function user_can_like_multiple_songs_at_once()
|
||||
{
|
||||
$this->expectsEvents(SongLikeToggled::class);
|
||||
|
||||
// Given multiple song and a user
|
||||
/** @var Collection $songs */
|
||||
$songs = factory(Song::class, 2)->create();
|
||||
$user = factory(User::class)->create();
|
||||
|
||||
// When I call the method to like songs in batch
|
||||
Interaction::batchLike($songs->pluck('id')->all(), $user);
|
||||
|
||||
// Then I see the songs are all liked
|
||||
$songs->each(function (Song $song) use ($user) {
|
||||
$this->assertTrue(Interaction::whereSongIdAndUserId($song->id, $user->id)->first()->liked);
|
||||
});
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function user_can_unlike_multiple_songs_at_once()
|
||||
{
|
||||
$this->expectsEvents(SongLikeToggled::class);
|
||||
|
||||
// Given multiple interaction records
|
||||
$user = factory(User::class)->create();
|
||||
/** @var Collection $interactions */
|
||||
$interactions = factory(Interaction::class, 3)->create([
|
||||
'user_id' => $user->id,
|
||||
'liked' => true,
|
||||
]);
|
||||
|
||||
// When I call the method to like songs in batch
|
||||
Interaction::batchUnlike($interactions->pluck('song.id')->all(), $user);
|
||||
|
||||
// Then I see the songs are all liked
|
||||
$interactions->each(function (Interaction $interaction) {
|
||||
$this->assertFalse(Interaction::find($interaction->id)->liked);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue