2016-06-04 16:56:38 +00:00
|
|
|
<?php
|
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2016-06-04 16:56:38 +00:00
|
|
|
use App\Models\Album;
|
|
|
|
use App\Models\Artist;
|
|
|
|
use App\Models\Playlist;
|
|
|
|
use App\Models\Song;
|
|
|
|
use App\Models\User;
|
2018-08-29 06:15:11 +00:00
|
|
|
use App\Repositories\InteractionRepository;
|
2018-08-18 12:27:17 +00:00
|
|
|
use App\Services\DownloadService;
|
2018-08-19 09:05:33 +00:00
|
|
|
use Exception;
|
2018-08-22 20:25:01 +00:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Mockery;
|
2018-08-18 11:51:52 +00:00
|
|
|
use Mockery\MockInterface;
|
2016-06-04 16:56:38 +00:00
|
|
|
|
2017-08-05 16:56:11 +00:00
|
|
|
class DownloadTest extends TestCase
|
2016-06-04 16:56:38 +00:00
|
|
|
{
|
2018-08-18 11:51:52 +00:00
|
|
|
/**
|
2018-08-18 12:27:17 +00:00
|
|
|
* @var MockInterface|DownloadService
|
2018-08-18 11:51:52 +00:00
|
|
|
*/
|
|
|
|
private $downloadService;
|
|
|
|
|
2018-08-19 09:05:33 +00:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2019-07-22 07:03:23 +00:00
|
|
|
public function setUp(): void
|
2016-06-04 16:56:38 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2020-04-27 18:55:12 +00:00
|
|
|
static::createSampleMediaSet();
|
|
|
|
$this->downloadService = static::mockIocDependency(DownloadService::class);
|
2016-06-04 16:56:38 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function testDownloadOneSong(): void
|
2016-06-04 16:56:38 +00:00
|
|
|
{
|
|
|
|
$song = Song::first();
|
2018-08-22 20:25:01 +00:00
|
|
|
|
2018-08-18 11:51:52 +00:00
|
|
|
$this->downloadService
|
|
|
|
->shouldReceive('from')
|
2016-06-04 16:56:38 +00:00
|
|
|
->once()
|
2018-08-22 20:25:01 +00:00
|
|
|
->with(Mockery::on(static function (Collection $retrievedSongs) use ($song) {
|
|
|
|
return $retrievedSongs->count() === 1 && $retrievedSongs->first()->id === $song->id;
|
|
|
|
}))
|
2016-06-04 16:56:38 +00:00
|
|
|
->andReturn($this->mediaPath.'/blank.mp3');
|
|
|
|
|
2016-09-26 06:30:00 +00:00
|
|
|
$this->getAsUser("api/download/songs?songs[]={$song->id}")
|
2020-09-06 18:21:39 +00:00
|
|
|
->assertOk();
|
2016-06-04 16:56:38 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function testDownloadMultipleSongs(): void
|
2016-06-04 16:56:38 +00:00
|
|
|
{
|
2018-08-22 20:25:01 +00:00
|
|
|
$songs = Song::take(2)->orderBy('id')->get();
|
2018-08-18 11:51:52 +00:00
|
|
|
|
|
|
|
$this->downloadService
|
|
|
|
->shouldReceive('from')
|
2016-06-04 16:56:38 +00:00
|
|
|
->once()
|
2018-08-22 20:25:01 +00:00
|
|
|
->with(Mockery::on(static function (Collection $retrievedSongs) use ($songs) {
|
|
|
|
$retrievedIds = $retrievedSongs->pluck('id')->toArray();
|
|
|
|
$requestedIds = $songs->pluck('id')->toArray();
|
|
|
|
|
|
|
|
return $requestedIds[0] === $retrievedIds[0] && $requestedIds[1] === $retrievedIds[1];
|
|
|
|
}))
|
2016-06-04 16:56:38 +00:00
|
|
|
->andReturn($this->mediaPath.'/blank.mp3'); // should be a zip file, but we're testing here…
|
|
|
|
|
2016-09-26 06:30:00 +00:00
|
|
|
$this->getAsUser("api/download/songs?songs[]={$songs[0]->id}&songs[]={$songs[1]->id}")
|
2020-09-06 18:21:39 +00:00
|
|
|
->assertOk();
|
2016-06-04 16:56:38 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function testDownloadAlbum(): void
|
2016-06-04 16:56:38 +00:00
|
|
|
{
|
|
|
|
$album = Album::first();
|
|
|
|
|
2018-08-18 11:51:52 +00:00
|
|
|
$this->downloadService
|
|
|
|
->shouldReceive('from')
|
2016-06-04 16:56:38 +00:00
|
|
|
->once()
|
2018-08-22 20:25:01 +00:00
|
|
|
->with(Mockery::on(static function (Album $retrievedAlbum) use ($album) {
|
|
|
|
return $retrievedAlbum->id === $album->id;
|
|
|
|
}))
|
2016-06-04 16:56:38 +00:00
|
|
|
->andReturn($this->mediaPath.'/blank.mp3');
|
|
|
|
|
2016-09-26 06:30:00 +00:00
|
|
|
$this->getAsUser("api/download/album/{$album->id}")
|
2020-09-06 18:21:39 +00:00
|
|
|
->assertOk();
|
2016-06-04 16:56:38 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function testDownloadArtist(): void
|
2016-06-04 16:56:38 +00:00
|
|
|
{
|
|
|
|
$artist = Artist::first();
|
|
|
|
|
2018-08-18 11:51:52 +00:00
|
|
|
$this->downloadService
|
|
|
|
->shouldReceive('from')
|
2016-06-04 16:56:38 +00:00
|
|
|
->once()
|
2018-08-22 20:25:01 +00:00
|
|
|
->with(Mockery::on(static function (Artist $retrievedArtist) use ($artist) {
|
|
|
|
return $retrievedArtist->id === $artist->id;
|
|
|
|
}))
|
2016-06-04 16:56:38 +00:00
|
|
|
->andReturn($this->mediaPath.'/blank.mp3');
|
|
|
|
|
2016-09-26 06:30:00 +00:00
|
|
|
$this->getAsUser("api/download/artist/{$artist->id}")
|
2020-09-06 18:21:39 +00:00
|
|
|
->assertOk();
|
2016-06-04 16:56:38 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function testDownloadPlaylist(): void
|
2016-06-04 16:56:38 +00:00
|
|
|
{
|
2020-09-06 18:21:39 +00:00
|
|
|
/** @var User $user */
|
2016-06-04 16:56:38 +00:00
|
|
|
$user = factory(User::class)->create();
|
|
|
|
|
2018-08-22 20:25:01 +00:00
|
|
|
/** @var Playlist $playlist */
|
2016-06-04 16:56:38 +00:00
|
|
|
$playlist = factory(Playlist::class)->create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
]);
|
|
|
|
|
2018-08-18 11:51:52 +00:00
|
|
|
$this->downloadService
|
|
|
|
->shouldReceive('from')
|
2018-08-22 20:25:01 +00:00
|
|
|
->with(Mockery::on(static function (Playlist $retrievedPlaylist) use ($playlist) {
|
|
|
|
return $retrievedPlaylist->id === $playlist->id;
|
|
|
|
}))
|
2016-06-04 16:56:38 +00:00
|
|
|
->once()
|
|
|
|
->andReturn($this->mediaPath.'/blank.mp3');
|
|
|
|
|
2016-09-26 06:30:00 +00:00
|
|
|
$this->getAsUser("api/download/playlist/{$playlist->id}", $user)
|
2020-09-06 18:21:39 +00:00
|
|
|
->assertOk();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNonOwnerCannotDownloadPlaylist(): void
|
|
|
|
{
|
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = factory(Playlist::class)->create();
|
|
|
|
|
|
|
|
$this->getAsUser("api/download/playlist/{$playlist->id}")
|
|
|
|
->assertStatus(403);
|
2016-06-04 16:56:38 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function testDownloadFavorites(): void
|
2016-06-04 16:56:38 +00:00
|
|
|
{
|
2018-08-22 20:25:01 +00:00
|
|
|
/** @var User $user */
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
$favorites = Collection::make();
|
|
|
|
|
2020-04-27 18:55:12 +00:00
|
|
|
static::mockIocDependency(InteractionRepository::class)
|
2018-08-22 20:25:01 +00:00
|
|
|
->shouldReceive('getUserFavorites')
|
|
|
|
->once()
|
|
|
|
->with(Mockery::on(static function (User $retrievedUser) use ($user) {
|
|
|
|
return $retrievedUser->id === $user->id;
|
|
|
|
}))
|
|
|
|
->andReturn($favorites);
|
|
|
|
|
2018-08-18 11:51:52 +00:00
|
|
|
$this->downloadService
|
|
|
|
->shouldReceive('from')
|
2018-08-22 20:25:01 +00:00
|
|
|
->with($favorites)
|
2016-06-04 16:56:38 +00:00
|
|
|
->once()
|
|
|
|
->andReturn($this->mediaPath.'/blank.mp3');
|
|
|
|
|
2018-08-22 20:25:01 +00:00
|
|
|
$this->getAsUser('api/download/favorites', $user)
|
2020-09-06 18:21:39 +00:00
|
|
|
->assertStatus(200);
|
2016-06-04 16:56:38 +00:00
|
|
|
}
|
|
|
|
}
|