koel/tests/Feature/UploadTest.php

60 lines
1.5 KiB
PHP
Raw Normal View History

2020-06-07 20:43:04 +00:00
<?php
namespace Tests\Feature;
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;
use PHPUnit\Framework\Attributes\Test;
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();
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
}
#[Test]
public function unauthorizedPost(): void
2020-06-07 20:43:04 +00:00
{
Setting::set('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
];
}
#[Test]
public function uploadFailsIfMediaPathIsNotSet(): void
2020-06-07 20:43:04 +00:00
{
Setting::set('media_path', '');
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
}
#[Test]
public function uploadSuccessful(): void
2020-06-07 20:43:04 +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']);
2020-06-07 20:43:04 +00:00
}
}