koel/tests/Feature/DownloadTest.php

120 lines
3 KiB
PHP
Raw Normal View History

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-18 12:27:17 +00:00
use App\Services\DownloadService;
2018-08-19 09:05:33 +00:00
use Exception;
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
*/
2016-06-04 16:56:38 +00:00
public function setUp()
{
parent::setUp();
$this->createSampleMediaSet();
2018-08-18 12:27:17 +00:00
$this->downloadService = $this->mockIocDependency(DownloadService::class);
2016-06-04 16:56:38 +00:00
}
2017-08-05 18:55:02 +00:00
/** @test */
public function a_single_song_can_be_downloaded()
2016-06-04 16:56:38 +00:00
{
$song = Song::first();
2018-08-22 19:46:36 +00:00
2018-08-18 11:51:52 +00:00
$this->downloadService
->shouldReceive('from')
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/songs?songs[]={$song->id}")
2018-08-18 11:51:52 +00:00
->assertResponseOk();
2016-06-04 16:56:38 +00:00
}
2017-08-05 18:55:02 +00:00
/** @test */
public function multiple_songs_can_be_downloaded()
2016-06-04 16:56:38 +00:00
{
$songs = Song::take(2)->get();
2018-08-18 11:51:52 +00:00
$this->downloadService
->shouldReceive('from')
2016-06-04 16:56:38 +00:00
->once()
->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}")
2018-08-18 11:51:52 +00:00
->assertResponseOk();
2016-06-04 16:56:38 +00:00
}
2017-08-05 18:55:02 +00:00
/** @test */
public function a_whole_album_can_be_downloaded()
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()
->andReturn($this->mediaPath.'/blank.mp3');
2016-09-26 06:30:00 +00:00
$this->getAsUser("api/download/album/{$album->id}")
2018-08-18 11:51:52 +00:00
->assertResponseOk();
2016-06-04 16:56:38 +00:00
}
2017-08-05 18:55:02 +00:00
/** @test */
public function a_whole_artists_biography_can_be_downloaded()
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()
->andReturn($this->mediaPath.'/blank.mp3');
2016-09-26 06:30:00 +00:00
$this->getAsUser("api/download/artist/{$artist->id}")
2018-08-18 11:51:52 +00:00
->assertResponseOk();
2016-06-04 16:56:38 +00:00
}
2017-08-05 18:55:02 +00:00
/** @test */
public function a_whole_playlist_can_be_downloaded()
2016-06-04 16:56:38 +00:00
{
$user = factory(User::class)->create();
$playlist = factory(Playlist::class)->create([
'user_id' => $user->id,
]);
2016-09-26 06:30:00 +00:00
$this->getAsUser("api/download/playlist/{$playlist->id}")
2018-08-18 11:51:52 +00:00
->assertResponseStatus(403);
2016-06-04 16:56:38 +00:00
2018-08-18 11:51:52 +00:00
$this->downloadService
->shouldReceive('from')
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)
2018-08-18 11:51:52 +00:00
->assertResponseOk();
2016-06-04 16:56:38 +00:00
}
2017-08-05 18:55:02 +00:00
/** @test */
public function all_favorite_songs_can_be_downloaded()
2016-06-04 16:56:38 +00:00
{
2018-08-18 11:51:52 +00:00
$this->downloadService
->shouldReceive('from')
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/favorites')
2016-06-04 16:56:38 +00:00
->seeStatusCode(200);
}
}