2020-06-07 20:43:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2022-09-02 05:00:49 +00:00
|
|
|
use App\Events\LibraryChanged;
|
2020-06-07 20:43:04 +00:00
|
|
|
use App\Exceptions\MediaPathNotSetException;
|
|
|
|
use App\Exceptions\SongUploadFailedException;
|
|
|
|
use App\Models\Setting;
|
2022-08-08 16:00:59 +00:00
|
|
|
use Illuminate\Http\Response;
|
2020-06-07 20:43:04 +00:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2022-09-02 05:00:49 +00:00
|
|
|
use Illuminate\Support\Facades\Event;
|
2024-01-09 18:34:40 +00:00
|
|
|
use Tests\TestCase;
|
2020-06-07 20:43:04 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
use function Tests\create_admin;
|
|
|
|
use function Tests\test_path;
|
|
|
|
|
2020-06-07 20:43:04 +00:00
|
|
|
class UploadTest extends TestCase
|
|
|
|
{
|
2022-08-08 16:00:59 +00:00
|
|
|
private UploadedFile $file;
|
2020-06-07 20:43:04 +00:00
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2020-11-14 16:57:25 +00:00
|
|
|
|
2024-01-07 12:43:10 +00:00
|
|
|
$this->file = UploadedFile::fromFile(test_path('songs/full.mp3'), 'song.mp3'); //@phpstan-ignore-line
|
2020-06-07 20:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnauthorizedPost(): void
|
|
|
|
{
|
2024-02-23 16:03:54 +00:00
|
|
|
Setting::unset('media_path');
|
2020-06-07 20:43:04 +00:00
|
|
|
|
2022-08-08 16:00:59 +00:00
|
|
|
$this->postAs('/api/upload', ['file' => $this->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 [
|
2022-08-08 16:00:59 +00:00
|
|
|
[MediaPathNotSetException::class, Response::HTTP_FORBIDDEN],
|
|
|
|
[SongUploadFailedException::class, Response::HTTP_BAD_REQUEST],
|
2020-06-07 20:43:04 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
public function testUploadFailsIfMediaPathIsNotSet(): void
|
2020-06-07 20:43:04 +00:00
|
|
|
{
|
2024-02-23 16:03:54 +00:00
|
|
|
Setting::unset('media_path');
|
2024-01-04 21:51:32 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
$this->postAs('/api/upload', ['file' => $this->file], create_admin())->assertForbidden();
|
2020-06-07 20:43:04 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
public function testUploadSuccessful(): void
|
2020-06-07 20:43:04 +00:00
|
|
|
{
|
2022-09-02 05:00:49 +00:00
|
|
|
Event::fake(LibraryChanged::class);
|
2024-01-04 21:51:32 +00:00
|
|
|
Setting::set('media_path', public_path('sandbox/media'));
|
2022-07-27 15:32:36 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
$this->postAs('/api/upload', ['file' => $this->file], create_admin())->assertJsonStructure(['song', 'album']);
|
2022-09-02 05:00:49 +00:00
|
|
|
Event::assertDispatched(LibraryChanged::class);
|
2020-06-07 20:43:04 +00:00
|
|
|
}
|
|
|
|
}
|