fix: broken tests

This commit is contained in:
Phan An 2022-07-29 13:08:24 +02:00
parent 3c5c24c104
commit 4258873183
No known key found for this signature in database
GPG key ID: A81E4477F0BB6FDC
5 changed files with 10 additions and 22 deletions

View file

@ -294,7 +294,7 @@ class InitCommand extends Command
return;
}
if (static::isValidMediaPath($path)) {
if (self::isValidMediaPath($path)) {
Setting::set('media_path', $path);
} else {
$this->warn(sprintf('The path %s does not exist or not readable. Skipping.', $path));

View file

@ -17,10 +17,6 @@ use Throwable;
class FileSynchronizer
{
public const SYNC_RESULT_SUCCESS = 1;
public const SYNC_RESULT_BAD_FILE = 2;
public const SYNC_RESULT_UNMODIFIED = 3;
private ?int $fileModifiedTime = null;
private ?string $filePath = null;
@ -35,7 +31,7 @@ class FileSynchronizer
*/
private ?Song $song;
private ?string $syncError;
private ?string $syncError = null;
public function __construct(
private getID3 $getID3,
@ -53,7 +49,6 @@ class FileSynchronizer
$this->filePath = $file->getPathname();
$this->fileHash = Helper::getFileHash($this->filePath);
$this->song = $this->songRepository->getOneById($this->fileHash); // @phpstan-ignore-line
$this->syncError = null;
$this->fileModifiedTime = Helper::getModifiedTime($file);
return $this;
@ -192,11 +187,6 @@ class FileSynchronizer
return $this->isFileNew() || $this->isFileChanged();
}
public function getSyncError(): ?string
{
return $this->syncError;
}
public function getSong(): ?Song
{
return $this->song;

View file

@ -26,9 +26,9 @@ class UploadService
$targetPathName = $this->getUploadDirectory() . $targetFileName;
$result = $this->fileSynchronizer->setFile($targetPathName)->sync();
if ($result !== FileSynchronizer::SYNC_RESULT_SUCCESS) {
if ($result->isError()) {
@unlink($targetPathName);
throw new SongUploadFailedException($this->fileSynchronizer->getSyncError());
throw new SongUploadFailedException($result->error);
}
return $this->fileSynchronizer->getSong();

View file

@ -5,6 +5,7 @@ namespace Tests\Feature;
use App\Models\Setting;
use App\Models\User;
use App\Services\MediaSyncService;
use App\Values\SyncResultCollection;
use Mockery\MockInterface;
class SettingTest extends TestCase
@ -23,7 +24,8 @@ class SettingTest extends TestCase
/** @var User $admin */
$admin = User::factory()->admin()->create();
$this->mediaSyncService->shouldReceive('sync')->once();
$this->mediaSyncService->shouldReceive('sync')->once()
->andReturn(SyncResultCollection::create());
$this->putAs('/api/settings', ['media_path' => __DIR__], $admin)
->assertSuccessful();

View file

@ -8,6 +8,7 @@ use App\Models\Setting;
use App\Models\Song;
use App\Services\FileSynchronizer;
use App\Services\UploadService;
use App\Values\SyncResult;
use Illuminate\Http\UploadedFile;
use Mockery;
use Mockery\LegacyMockInterface;
@ -58,12 +59,7 @@ class UploadServiceTest extends TestCase
->shouldReceive('sync')
->once()
->with()
->andReturn(FileSynchronizer::SYNC_RESULT_BAD_FILE);
$this->fileSynchronizer
->shouldReceive('getSyncError')
->once()
->andReturn('A monkey ate your file oh no');
->andReturn(SyncResult::error('/media/koel/__KOEL_UPLOADS__/foo.mp3', 'A monkey ate your file oh no'));
self::expectException(SongUploadFailedException::class);
self::expectExceptionMessage('A monkey ate your file oh no');
@ -93,7 +89,7 @@ class UploadServiceTest extends TestCase
$this->fileSynchronizer
->shouldReceive('sync')
->once()
->andReturn(FileSynchronizer::SYNC_RESULT_SUCCESS);
->andReturn(SyncResult::success('/media/koel/__KOEL_UPLOADS__/foo.mp3'));
$song = new Song();