uploadService = self::mock(UploadService::class); $this->file = UploadedFile::fake() ->createWithContent('song.mp3', file_get_contents(__DIR__ . '/../songs/full.mp3')); } public function testUnauthorizedPost(): void { Setting::set('media_path', '/media/koel'); $this->uploadService ->shouldReceive('handleUploadedFile') ->never(); $this->postAs('/api/upload', ['file' => $this->file])->assertForbidden(); } /** @return array */ public function provideUploadExceptions(): array { return [ [MediaPathNotSetException::class, Response::HTTP_FORBIDDEN], [SongUploadFailedException::class, Response::HTTP_BAD_REQUEST], ]; } /** @dataProvider provideUploadExceptions */ public function testPostShouldFail(string $exceptionClass, int $statusCode): void { /** @var User $admin */ $admin = User::factory()->admin()->create(); $this->uploadService ->shouldReceive('handleUploadedFile') ->once() ->with($this->file) ->andThrow($exceptionClass); $this->postAs('/api/upload', ['file' => $this->file], $admin)->assertStatus($statusCode); } public function testPost(): void { Setting::set('media_path', '/media/koel'); /** @var Song $song */ $song = Song::factory()->create(); /** @var User $admin */ $admin = User::factory()->admin()->create(); $this->uploadService ->shouldReceive('handleUploadedFile') ->once() ->with($this->file) ->andReturn($song); $this->postAs('/api/upload', ['file' => $this->file], $admin)->assertJsonStructure(['song', 'album']); } }