mirror of
https://github.com/koel/koel
synced 2024-11-10 06:34:14 +00:00
Use DI in Download controllers
This commit is contained in:
parent
168f70481c
commit
f4c00abe02
9 changed files with 68 additions and 48 deletions
|
@ -3,8 +3,6 @@
|
|||
namespace App\Http\Controllers\API\Download;
|
||||
|
||||
use App\Models\Album;
|
||||
use Download;
|
||||
use Exception;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
class AlbumController extends Controller
|
||||
|
@ -14,12 +12,10 @@ class AlbumController extends Controller
|
|||
*
|
||||
* @param Album $album
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return BinaryFileResponse
|
||||
*/
|
||||
public function download(Album $album)
|
||||
public function show(Album $album)
|
||||
{
|
||||
return response()->download(Download::from($album));
|
||||
return response()->download($this->downloadService->from($album));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,8 +3,6 @@
|
|||
namespace App\Http\Controllers\API\Download;
|
||||
|
||||
use App\Models\Artist;
|
||||
use Download;
|
||||
use Exception;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
class ArtistController extends Controller
|
||||
|
@ -16,12 +14,10 @@ class ArtistController extends Controller
|
|||
*
|
||||
* @param Artist $artist
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return BinaryFileResponse
|
||||
*/
|
||||
public function download(Artist $artist)
|
||||
public function show(Artist $artist)
|
||||
{
|
||||
return response()->download(Download::from($artist));
|
||||
return response()->download($this->downloadService->from($artist));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,14 @@
|
|||
namespace App\Http\Controllers\API\Download;
|
||||
|
||||
use App\Http\Controllers\API\Controller as BaseController;
|
||||
use App\Services\Download;
|
||||
|
||||
class Controller extends BaseController
|
||||
abstract class Controller extends BaseController
|
||||
{
|
||||
protected $downloadService;
|
||||
|
||||
public function __construct(Download $downloadService)
|
||||
{
|
||||
$this->downloadService = $downloadService;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,6 @@ namespace App\Http\Controllers\API\Download;
|
|||
|
||||
use App\Http\Requests\API\Download\Request;
|
||||
use App\Models\Song;
|
||||
use Download;
|
||||
use Exception;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
class FavoritesController extends Controller
|
||||
|
@ -15,12 +13,12 @@ class FavoritesController extends Controller
|
|||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return BinaryFileResponse
|
||||
*/
|
||||
public function download(Request $request)
|
||||
public function show(Request $request)
|
||||
{
|
||||
return response()->download(Download::from(Song::getFavorites($request->user())));
|
||||
$songs = Song::getFavorites($request->user());
|
||||
|
||||
return response()->download($this->downloadService->from($songs));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,8 +3,6 @@
|
|||
namespace App\Http\Controllers\API\Download;
|
||||
|
||||
use App\Models\Playlist;
|
||||
use Download;
|
||||
use Exception;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
|
@ -16,14 +14,13 @@ class PlaylistController extends Controller
|
|||
* @param Playlist $playlist
|
||||
*
|
||||
* @throws AuthorizationException
|
||||
* @throws Exception
|
||||
*
|
||||
* @return BinaryFileResponse
|
||||
*/
|
||||
public function download(Playlist $playlist)
|
||||
public function show(Playlist $playlist)
|
||||
{
|
||||
$this->authorize('owner', $playlist);
|
||||
|
||||
return response()->download(Download::from($playlist));
|
||||
return response()->download($this->downloadService->from($playlist));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,6 @@ namespace App\Http\Controllers\API\Download;
|
|||
|
||||
use App\Http\Requests\API\Download\SongRequest;
|
||||
use App\Models\Song;
|
||||
use Download;
|
||||
use Exception;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
class SongController extends Controller
|
||||
|
@ -15,14 +13,12 @@ class SongController extends Controller
|
|||
*
|
||||
* @param SongRequest $request
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return BinaryFileResponse
|
||||
*/
|
||||
public function download(SongRequest $request)
|
||||
public function show(SongRequest $request)
|
||||
{
|
||||
$songs = Song::whereIn('id', $request->songs)->get();
|
||||
|
||||
return response()->download(Download::from($songs));
|
||||
return response()->download($this->downloadService->from($songs));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -68,11 +68,11 @@ Route::group(['namespace' => 'API'], function () {
|
|||
|
||||
// Download routes
|
||||
Route::group(['prefix' => 'download', 'namespace' => 'Download'], function () {
|
||||
Route::get('songs', 'SongController@download');
|
||||
Route::get('album/{album}', 'AlbumController@download');
|
||||
Route::get('artist/{artist}', 'ArtistController@download');
|
||||
Route::get('playlist/{playlist}', 'PlaylistController@download');
|
||||
Route::get('favorites', 'FavoritesController@download');
|
||||
Route::get('songs', 'SongController@show');
|
||||
Route::get('album/{album}', 'AlbumController@show');
|
||||
Route::get('artist/{artist}', 'ArtistController@show');
|
||||
Route::get('playlist/{playlist}', 'PlaylistController@show');
|
||||
Route::get('favorites', 'FavoritesController@show');
|
||||
});
|
||||
|
||||
// Info routes
|
||||
|
|
|
@ -7,38 +7,48 @@ use App\Models\Artist;
|
|||
use App\Models\Playlist;
|
||||
use App\Models\Song;
|
||||
use App\Models\User;
|
||||
use Download;
|
||||
use App\Services\Download;
|
||||
use Mockery\MockInterface;
|
||||
|
||||
class DownloadTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var MockInterface|Download
|
||||
*/
|
||||
private $downloadService;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->createSampleMediaSet();
|
||||
$this->downloadService = $this->mockIocDependency(Download::class);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function a_single_song_can_be_downloaded()
|
||||
{
|
||||
$song = Song::first();
|
||||
Download::shouldReceive('from')
|
||||
$this->downloadService
|
||||
->shouldReceive('from')
|
||||
->once()
|
||||
->andReturn($this->mediaPath.'/blank.mp3');
|
||||
|
||||
$this->getAsUser("api/download/songs?songs[]={$song->id}")
|
||||
->seeStatusCode(200);
|
||||
->assertResponseOk();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function multiple_songs_can_be_downloaded()
|
||||
{
|
||||
$songs = Song::take(2)->get();
|
||||
Download::shouldReceive('from')
|
||||
|
||||
$this->downloadService
|
||||
->shouldReceive('from')
|
||||
->once()
|
||||
->andReturn($this->mediaPath.'/blank.mp3'); // should be a zip file, but we're testing here…
|
||||
|
||||
$this->getAsUser("api/download/songs?songs[]={$songs[0]->id}&songs[]={$songs[1]->id}")
|
||||
->seeStatusCode(200);
|
||||
->assertResponseOk();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
|
@ -46,12 +56,13 @@ class DownloadTest extends TestCase
|
|||
{
|
||||
$album = Album::first();
|
||||
|
||||
Download::shouldReceive('from')
|
||||
$this->downloadService
|
||||
->shouldReceive('from')
|
||||
->once()
|
||||
->andReturn($this->mediaPath.'/blank.mp3');
|
||||
|
||||
$this->getAsUser("api/download/album/{$album->id}")
|
||||
->seeStatusCode(200);
|
||||
->assertResponseOk();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
|
@ -59,12 +70,13 @@ class DownloadTest extends TestCase
|
|||
{
|
||||
$artist = Artist::first();
|
||||
|
||||
Download::shouldReceive('from')
|
||||
$this->downloadService
|
||||
->shouldReceive('from')
|
||||
->once()
|
||||
->andReturn($this->mediaPath.'/blank.mp3');
|
||||
|
||||
$this->getAsUser("api/download/artist/{$artist->id}")
|
||||
->seeStatusCode(200);
|
||||
->assertResponseOk();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
|
@ -77,20 +89,22 @@ class DownloadTest extends TestCase
|
|||
]);
|
||||
|
||||
$this->getAsUser("api/download/playlist/{$playlist->id}")
|
||||
->seeStatusCode(403);
|
||||
->assertResponseStatus(403);
|
||||
|
||||
Download::shouldReceive('from')
|
||||
$this->downloadService
|
||||
->shouldReceive('from')
|
||||
->once()
|
||||
->andReturn($this->mediaPath.'/blank.mp3');
|
||||
|
||||
$this->getAsUser("api/download/playlist/{$playlist->id}", $user)
|
||||
->seeStatusCode(200);
|
||||
->assertResponseOk();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function all_favorite_songs_can_be_downloaded()
|
||||
{
|
||||
Download::shouldReceive('from')
|
||||
$this->downloadService
|
||||
->shouldReceive('from')
|
||||
->once()
|
||||
->andReturn($this->mediaPath.'/blank.mp3');
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ use App\Models\User;
|
|||
use JWTAuth;
|
||||
use Laravel\BrowserKitTesting\DatabaseTransactions;
|
||||
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;
|
||||
use Mockery as m;
|
||||
use Tests\CreatesApplication;
|
||||
|
||||
abstract class TestCase extends BaseTestCase
|
||||
|
@ -85,4 +86,19 @@ abstract class TestCase extends BaseTestCase
|
|||
'Authorization' => 'Bearer '.JWTAuth::fromUser($user),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mock an IOC dependency, for example an injected service in controllers
|
||||
*
|
||||
* @param string $abstract
|
||||
* @param array $args
|
||||
*
|
||||
* @return m\MockInterface
|
||||
*/
|
||||
protected function mockIocDependency($abstract, ...$args)
|
||||
{
|
||||
return tap(m::mock($abstract, ...$args), function ($mocked) use ($abstract) {
|
||||
$this->instance($abstract, $mocked);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue