2020-06-07 20:43:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
|
|
use App\Exceptions\MediaPathNotSetException;
|
|
|
|
use App\Exceptions\SongUploadFailedException;
|
|
|
|
use App\Models\Setting;
|
|
|
|
use App\Models\Song;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Services\UploadService;
|
|
|
|
use Illuminate\Http\UploadedFile;
|
2022-06-10 10:47:46 +00:00
|
|
|
use Mockery\MockInterface;
|
2020-06-07 20:43:04 +00:00
|
|
|
|
|
|
|
class UploadTest extends TestCase
|
|
|
|
{
|
2022-07-27 15:32:36 +00:00
|
|
|
private UploadService|MockInterface $uploadService;
|
2020-06-07 20:43:04 +00:00
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2020-11-14 16:57:25 +00:00
|
|
|
|
2020-12-22 23:01:49 +00:00
|
|
|
$this->uploadService = self::mock(UploadService::class);
|
2020-06-07 20:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnauthorizedPost(): void
|
|
|
|
{
|
|
|
|
Setting::set('media_path', '/media/koel');
|
|
|
|
$file = UploadedFile::fake()->create('foo.mp3', 2048);
|
|
|
|
|
|
|
|
$this->uploadService
|
|
|
|
->shouldReceive('handleUploadedFile')
|
|
|
|
->never();
|
|
|
|
|
2022-07-27 15:32:36 +00:00
|
|
|
$this->postAs('/api/upload', ['file' => $file])->assertForbidden();
|
2020-06-07 20:43:04 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
/** @return array<mixed> */
|
2020-06-07 20:43:04 +00:00
|
|
|
public function provideUploadExceptions(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[MediaPathNotSetException::class, 403],
|
|
|
|
[SongUploadFailedException::class, 400],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
/** @dataProvider provideUploadExceptions */
|
2020-06-07 20:43:04 +00:00
|
|
|
public function testPostShouldFail(string $exceptionClass, int $statusCode): void
|
|
|
|
{
|
|
|
|
$file = UploadedFile::fake()->create('foo.mp3', 2048);
|
|
|
|
|
2022-07-27 15:32:36 +00:00
|
|
|
/** @var User $admin */
|
|
|
|
$admin = User::factory()->admin()->create();
|
|
|
|
|
2020-06-07 20:43:04 +00:00
|
|
|
$this->uploadService
|
|
|
|
->shouldReceive('handleUploadedFile')
|
|
|
|
->once()
|
|
|
|
->with($file)
|
|
|
|
->andThrow($exceptionClass);
|
|
|
|
|
2022-07-27 15:32:36 +00:00
|
|
|
$this->postAs('/api/upload', ['file' => $file], $admin)->assertStatus($statusCode);
|
2020-06-07 20:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPost(): void
|
|
|
|
{
|
|
|
|
Setting::set('media_path', '/media/koel');
|
|
|
|
$file = UploadedFile::fake()->create('foo.mp3', 2048);
|
2022-07-27 15:32:36 +00:00
|
|
|
|
2020-06-07 20:43:04 +00:00
|
|
|
/** @var Song $song */
|
2020-11-14 16:57:25 +00:00
|
|
|
$song = Song::factory()->create();
|
2022-07-27 15:32:36 +00:00
|
|
|
|
|
|
|
/** @var User $admin */
|
|
|
|
$admin = User::factory()->admin()->create();
|
|
|
|
|
2020-06-07 20:43:04 +00:00
|
|
|
$this->uploadService
|
|
|
|
->shouldReceive('handleUploadedFile')
|
|
|
|
->once()
|
|
|
|
->with($file)
|
|
|
|
->andReturn($song);
|
|
|
|
|
2022-07-27 15:32:36 +00:00
|
|
|
$this->postAs('/api/upload', ['file' => $file], $admin)->assertJsonStructure(['song', 'album']);
|
2020-06-07 20:43:04 +00:00
|
|
|
}
|
|
|
|
}
|