koel/tests/Unit/Services/UploadServiceTest.php

108 lines
3.1 KiB
PHP
Raw Normal View History

2020-06-07 20:43:04 +00:00
<?php
namespace Tests\Unit\Services;
use App\Exceptions\MediaPathNotSetException;
use App\Exceptions\SongUploadFailedException;
use App\Models\Setting;
use App\Models\Song;
use App\Services\FileSynchronizer;
use App\Services\UploadService;
use Illuminate\Http\UploadedFile;
use Mockery;
2022-07-07 10:45:57 +00:00
use Mockery\LegacyMockInterface;
2020-06-07 20:43:04 +00:00
use Mockery\MockInterface;
use Tests\TestCase;
class UploadServiceTest extends TestCase
{
2022-07-07 10:45:57 +00:00
private FileSynchronizer|MockInterface|LegacyMockInterface $fileSynchronizer;
private UploadService $uploadService;
2020-06-07 20:43:04 +00:00
public function setUp(): void
{
parent::setUp();
2020-12-22 20:11:22 +00:00
2020-06-07 20:43:04 +00:00
$this->fileSynchronizer = Mockery::mock(FileSynchronizer::class);
$this->uploadService = new UploadService($this->fileSynchronizer);
}
public function testHandleUploadedFileWithMediaPathNotSet(): void
{
2022-07-06 16:08:55 +00:00
Setting::set('media_path');
2020-06-07 20:43:04 +00:00
self::expectException(MediaPathNotSetException::class);
$this->uploadService->handleUploadedFile(Mockery::mock(UploadedFile::class));
}
public function testHandleUploadedFileFails(): void
{
Setting::set('media_path', '/media/koel');
/** @var UploadedFile|MockInterface $file */
$file = Mockery::mock(UploadedFile::class);
$file->shouldReceive('getClientOriginalName')
->andReturn('foo.mp3');
$file->shouldReceive('move')
->once()
->with('/media/koel/__KOEL_UPLOADS__/', 'foo.mp3');
$this->fileSynchronizer
->shouldReceive('setFile')
->once()
2022-07-07 10:45:57 +00:00
->with('/media/koel/__KOEL_UPLOADS__/foo.mp3')
->andReturnSelf();
2020-06-07 20:43:04 +00:00
$this->fileSynchronizer
->shouldReceive('sync')
->once()
->with()
2020-06-07 20:43:04 +00:00
->andReturn(FileSynchronizer::SYNC_RESULT_BAD_FILE);
$this->fileSynchronizer
->shouldReceive('getSyncError')
->once()
->andReturn('A monkey ate your file oh no');
self::expectException(SongUploadFailedException::class);
self::expectExceptionMessage('A monkey ate your file oh no');
$this->uploadService->handleUploadedFile($file);
}
public function testHandleUploadedFile(): void
{
Setting::set('media_path', '/media/koel');
/** @var UploadedFile|MockInterface $file */
$file = Mockery::mock(UploadedFile::class);
$file->shouldReceive('getClientOriginalName')
->andReturn('foo.mp3');
$file->shouldReceive('move')
->once()
->with('/media/koel/__KOEL_UPLOADS__/', 'foo.mp3');
$this->fileSynchronizer
->shouldReceive('setFile')
->once()
2022-07-07 10:45:57 +00:00
->with('/media/koel/__KOEL_UPLOADS__/foo.mp3')
->andReturnSelf();
2020-06-07 20:43:04 +00:00
$this->fileSynchronizer
->shouldReceive('sync')
->once()
->andReturn(FileSynchronizer::SYNC_RESULT_SUCCESS);
$song = new Song();
$this->fileSynchronizer
->shouldReceive('getSong')
->once()
->andReturn($song);
self::assertSame($song, $this->uploadService->handleUploadedFile($file));
}
}