fix(test): sync tests

This commit is contained in:
Phan An 2022-07-07 12:45:57 +02:00
parent 09f54d26d5
commit a1f0309b0a
No known key found for this signature in database
GPG key ID: A81E4477F0BB6FDC
11 changed files with 141 additions and 130 deletions

View file

@ -158,7 +158,7 @@ class SongTest extends TestCase
->assertStatus(200);
/** @var Album $album */
$album = Album::whereName('One by One')->first();
$album = Album::where('name', 'One by One')->first();
/** @var Artist $albumArtist */
$albumArtist = Artist::whereName('John Lennon')->first();

View file

@ -73,8 +73,8 @@ class UploadTest extends TestCase
$this->postAsUser('/api/upload', ['file' => $file], User::factory()->admin()->create())
->assertJsonStructure([
'song',
'album',
'artist',
]);
}
}

View file

@ -30,7 +30,7 @@ class UserTest extends TestCase
'password' => 'secret',
'is_admin' => true,
], User::factory()->admin()->create())
->assertOk();
->assertSuccessful();
/** @var User $user */
$user = User::firstWhere('email', 'bar@baz.com');

View file

@ -7,16 +7,6 @@ use Tests\TestCase;
class SongTest extends TestCase
{
public function testLyricsHaveNewlinesReplacedByBrTags(): void
{
/** @var Song $song */
$song = Song::factory()->create([
'lyrics' => "foo\rbar\nbaz\r\nqux",
]);
self::assertEquals('foo<br />bar<br />baz<br />qux', $song->lyrics);
}
public function testGettingS3HostedSongs(): void
{
/** @var Song $song */

View file

@ -18,12 +18,11 @@ class FileSynchronizerTest extends TestCase
public function testGetFileInfo(): void
{
$info = $this->fileSynchronizer->setFile(__DIR__ . '/../../songs/full.mp3')->getFileInfo();
$info = $this->fileSynchronizer->setFile(__DIR__ . '/../../songs/full.mp3')->getFileScanInformation();
$expectedData = [
'artist' => 'Koel',
'album' => 'Koel Testing Vol. 1',
'compilation' => false,
'title' => 'Amet',
'track' => 5,
'disc' => 3,
@ -38,13 +37,13 @@ class FileSynchronizerTest extends TestCase
'description' => '',
'datalength' => 7627,
],
'path' => __DIR__ . '/../../songs/full.mp3',
'path' => realpath(__DIR__ . '/../../songs/full.mp3'),
'mtime' => filemtime(__DIR__ . '/../../songs/full.mp3'),
'albumartist' => '',
];
self::assertArraySubset($expectedData, $info);
self::assertEqualsWithDelta(10.083, $info['length'], 0.001);
self::assertArraySubset($expectedData, $info->toArray());
self::assertEqualsWithDelta(10, $info->length, 0.001);
}
/** @test */
@ -52,6 +51,6 @@ class FileSynchronizerTest extends TestCase
{
$this->fileSynchronizer->setFile(__DIR__ . '/../../songs/blank.mp3');
self::assertSame('blank', $this->fileSynchronizer->getFileInfo()['title']);
self::assertSame('blank', $this->fileSynchronizer->getFileScanInformation()->title);
}
}

View file

@ -6,14 +6,18 @@ use App\Events\AlbumInformationFetched;
use App\Events\ArtistInformationFetched;
use App\Models\Album;
use App\Models\Artist;
use App\Repositories\AlbumRepository;
use App\Repositories\ArtistRepository;
use App\Services\LastfmService;
use App\Services\MediaInformationService;
use Mockery;
use Mockery\LegacyMockInterface;
use Mockery\MockInterface;
use Tests\TestCase;
class MediaInformationServiceTest extends TestCase
{
private $lastFmService;
private LastfmService|MockInterface|LegacyMockInterface $lastFmService;
private MediaInformationService $mediaInformationService;
public function setUp(): void
@ -21,7 +25,14 @@ class MediaInformationServiceTest extends TestCase
parent::setUp();
$this->lastFmService = Mockery::mock(LastfmService::class);
$this->mediaInformationService = new MediaInformationService($this->lastFmService);
$this->albumRepository = Mockery::mock(AlbumRepository::class);
$this->artistRepository = Mockery::mock(ArtistRepository::class);
$this->mediaInformationService = new MediaInformationService(
$this->lastFmService,
app(AlbumRepository::class),
app(ArtistRepository::class)
);
}
public function testGetAlbumInformation(): void

View file

@ -33,7 +33,7 @@ class MediaMetadataServiceTest extends TestCase
public function testGetAlbumThumbnailUrlWithNoCover(): void
{
/** @var Album $album */
$album = Album::factory()->create(['cover' => null]);
$album = Album::factory()->create(['cover' => '']);
self::assertNull(app(MediaMetadataService::class)->getAlbumThumbnailUrl($album));
}
@ -41,6 +41,7 @@ class MediaMetadataServiceTest extends TestCase
{
@unlink(album_cover_path('album-cover-for-thumbnail-test.jpg'));
@unlink(album_cover_path('album-cover-for-thumbnail-test_thumb.jpg'));
self::assertFileDoesNotExist(album_cover_path('album-cover-for-thumbnail-test.jpg'));
self::assertFileDoesNotExist(album_cover_path('album-cover-for-thumbnail-test_thumb.jpg'));
}

View file

@ -7,178 +7,180 @@ use App\Events\MediaSyncCompleted;
use App\Libraries\WatchRecord\InotifyWatchRecord;
use App\Models\Album;
use App\Models\Artist;
use App\Models\Setting;
use App\Models\Song;
use App\Services\FileSynchronizer;
use App\Services\MediaSyncService;
use getID3;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Support\Arr;
use Mockery;
use Tests\Feature\TestCase;
class MediaSyncServiceTest extends TestCase
{
use WithoutMiddleware;
/** @var MediaSyncService */
private $mediaService;
private MediaSyncService $mediaService;
public function setUp(): void
{
parent::setUp();
Setting::set('media_path', realpath($this->mediaPath));
$this->mediaService = app(MediaSyncService::class);
}
private function path($subPath): string
{
return realpath($this->mediaPath . $subPath);
}
public function testSync(): void
{
$this->expectsEvents(LibraryChanged::class, MediaSyncCompleted::class);
$this->expectsEvents(MediaSyncCompleted::class);
$this->mediaService->sync($this->mediaPath);
$this->mediaService->sync();
// Standard mp3 files under root path should be recognized
self::assertDatabaseHas(Song::class, [
'path' => $this->mediaPath . '/full.mp3',
// Track # should be recognized
'path' => $this->path('/full.mp3'),
'track' => 5,
]);
// Ogg files and audio files in subdirectories should be recognized
self::assertDatabaseHas('songs', ['path' => $this->mediaPath . '/subdir/back-in-black.ogg']);
self::assertDatabaseHas(Song::class, ['path' => $this->path('/subdir/back-in-black.ogg')]);
// GitHub issue #380. folder.png should be copied and used as the cover for files
// under subdir/
$song = Song::wherePath($this->mediaPath . '/subdir/back-in-black.ogg')->first();
self::assertNotNull($song->album->cover);
/** @var Song $song */
$song = Song::where('path', $this->path('/subdir/back-in-black.ogg'))->first();
self::assertNotEmpty($song->album->cover);
// File search shouldn't be case-sensitive.
self::assertDatabaseHas('songs', ['path' => $this->mediaPath . '/subdir/no-name.mp3']);
self::assertDatabaseHas(Song::class, ['path' => $this->path('/subdir/no-name.mp3')]);
// Non-audio files shouldn't be recognized
self::assertDatabaseMissing('songs', ['path' => $this->mediaPath . '/rubbish.log']);
self::assertDatabaseMissing(Song::class, ['path' => $this->path('/rubbish.log')]);
// Broken/corrupted audio files shouldn't be recognized
self::assertDatabaseMissing('songs', ['path' => $this->mediaPath . '/fake.mp3']);
self::assertDatabaseMissing(Song::class, ['path' => $this->path('/fake.mp3')]);
// Artists should be created
self::assertDatabaseHas('artists', ['name' => 'Cuckoo']);
self::assertDatabaseHas('artists', ['name' => 'Koel']);
self::assertDatabaseHas(Artist::class, ['name' => 'Cuckoo']);
self::assertDatabaseHas(Artist::class, ['name' => 'Koel']);
// Albums should be created
self::assertDatabaseHas('albums', ['name' => 'Koel Testing Vol. 1']);
self::assertDatabaseHas(Album::class, ['name' => 'Koel Testing Vol. 1']);
// Albums and artists should be correctly linked
$album = Album::whereName('Koel Testing Vol. 1')->first();
/** @var Album $album */
$album = Album::where('name', 'Koel Testing Vol. 1')->first();
self::assertEquals('Koel', $album->artist->name);
// Compilation albums, artists and songs must be recognized
$song = Song::whereTitle('This song belongs to a compilation')->first();
self::assertNotNull($song->artist_id);
self::assertTrue($song->album->is_compilation);
self::assertEquals(Artist::VARIOUS_ID, $song->album->artist_id);
$currentCover = $album->cover;
$song = Song::orderBy('id', 'desc')->first();
// Modified file should be recognized
touch($song->path, $time = time());
$this->mediaService->sync($this->mediaPath);
$song = Song::find($song->id);
self::assertEquals($time, $song->mtime);
// Albums with a non-default cover should have their covers overwritten
self::assertEquals($currentCover, Album::find($album->id)->cover);
/** @var Song $song */
$song = Song::where('title', 'This song belongs to a compilation')->first();
self::assertFalse($song->album->artist->is($song->artist));
self::assertSame('Koel', $song->album->artist->name);
self::assertSame('Cuckoo', $song->artist->name);
}
public function testForceSync(): void
public function testModifiedFileIsResynced(): void
{
$this->expectsEvents(LibraryChanged::class, MediaSyncCompleted::class);
$this->mediaService->sync();
$this->mediaService->sync($this->mediaPath);
$song = Song::first();
// Make some modification to the records
/** @var Song $song */
$song = Song::orderBy('id', 'desc')->first();
$originalTitle = $song->title;
$originalLyrics = $song->lyrics;
touch($song->path, $time = time() + 1000);
$this->mediaService->sync();
self::assertSame($time, $song->refresh()->mtime);
}
public function testResyncWithoutForceDoesNotResetData(): void
{
$this->expectsEvents(MediaSyncCompleted::class);
$this->mediaService->sync();
$song = Song::first();
$song->update([
'title' => "It's John Cena!",
'lyrics' => 'Booom Wroooom',
]);
// Resync without forcing
$this->mediaService->sync($this->mediaPath);
$this->mediaService->sync();
// Validate that the changes are not lost
/** @var Song $song */
$song = Song::orderBy('id', 'desc')->first();
self::assertEquals("It's John Cena!", $song->title);
self::assertEquals('Booom Wroooom', $song->lyrics);
// Resync with force
$this->mediaService->sync($this->mediaPath, [], true);
// All is lost.
/** @var Song $song */
$song = Song::orderBy('id', 'desc')->first();
self::assertEquals($originalTitle, $song->title);
self::assertEquals($originalLyrics, $song->lyrics);
$song->refresh();
self::assertSame("It's John Cena!", $song->title);
self::assertSame('Booom Wroooom', $song->lyrics);
}
public function testSelectiveSync(): void
public function testForceSyncResetsData(): void
{
$this->expectsEvents(LibraryChanged::class, MediaSyncCompleted::class);
$this->expectsEvents(MediaSyncCompleted::class);
$this->mediaService->sync($this->mediaPath);
$this->mediaService->sync();
// Make some modification to the records
/** @var Song $song */
$song = Song::orderBy('id', 'desc')->first();
$originalTitle = $song->title;
$song = Song::first();
$song->update([
'title' => "It's John Cena!",
'lyrics' => 'Booom Wroooom',
]);
// Sync only the selective tags
$this->mediaService->sync($this->mediaPath, ['title'], true);
$this->mediaService->sync(force: true);
// Validate that the specified tags are changed, other remains the same
$song = Song::orderBy('id', 'desc')->first();
self::assertEquals($originalTitle, $song->title);
self::assertEquals('Booom Wroooom', $song->lyrics);
$song->refresh();
self::assertNotSame("It's John Cena!", $song->title);
self::assertNotSame('Booom Wroooom', $song->lyrics);
}
public function testSyncAllTagsForNewFiles(): void
public function testSyncWithIgnoredTags(): void
{
// First we sync the test directory to get the data
$this->mediaService->sync($this->mediaPath);
$this->expectsEvents(MediaSyncCompleted::class);
// Now delete the first song.
$song = Song::orderBy('id')->first();
$this->mediaService->sync();
$song = Song::first();
$song->update([
'title' => "It's John Cena!",
'lyrics' => 'Booom Wroooom',
]);
$this->mediaService->sync(ignores: ['title'], force: true);
$song->refresh();
self::assertSame("It's John Cena!", $song->title);
self::assertNotSame('Booom Wroooom', $song->lyrics);
}
public function testSyncAllTagsForNewFilesRegardlessOfIgnoredOption(): void
{
$this->mediaService->sync();
$song = Song::first();
$song->delete();
// Selectively sync only one tag
$this->mediaService->sync($this->mediaPath, ['track'], true);
$this->mediaService->sync(ignores: ['title', 'disc', 'track'], force: true);
// but we still expect the whole song to be added back with all info
$addedSong = Song::findOrFail($song->id)->toArray();
$song = $song->toArray();
array_forget($addedSong, 'created_at');
array_forget($song, 'created_at');
self::assertEquals($song, $addedSong);
// Song should be added back with all info
self::assertEquals(
Arr::except(Song::findOrFail($song->id)->toArray(), 'created_at'),
Arr::except($song->toArray(), 'created_at')
);
}
public function testSyncAddedSongViaWatch(): void
{
$this->expectsEvents(LibraryChanged::class);
$path = $this->mediaPath . '/blank.mp3';
$path = $this->path('/blank.mp3');
$this->mediaService->syncByWatchRecord(new InotifyWatchRecord("CLOSE_WRITE,CLOSE $path"));
self::assertDatabaseHas('songs', ['path' => $path]);
self::assertDatabaseHas(Song::class, ['path' => $path]);
}
public function testSyncDeletedSongViaWatch(): void
@ -186,32 +188,36 @@ class MediaSyncServiceTest extends TestCase
$this->expectsEvents(LibraryChanged::class);
static::createSampleMediaSet();
$song = Song::orderBy('id', 'desc')->first();
$song = Song::first();
self::assertModelExists($song);
$this->mediaService->syncByWatchRecord(new InotifyWatchRecord("DELETE {$song->path}"));
$this->mediaService->syncByWatchRecord(new InotifyWatchRecord("DELETE $song->path"));
self::assertDatabaseMissing('songs', ['id' => $song->id]);
self::assertModelMissing($song);
}
public function testSyncDeletedDirectoryViaWatch(): void
{
$this->expectsEvents(LibraryChanged::class, MediaSyncCompleted::class);
$this->mediaService->sync($this->mediaPath);
$this->mediaService->sync();
$this->mediaService->syncByWatchRecord(new InotifyWatchRecord("MOVED_FROM,ISDIR $this->mediaPath/subdir"));
self::assertDatabaseMissing('songs', ['path' => $this->mediaPath . '/subdir/sic.mp3']);
self::assertDatabaseMissing('songs', ['path' => $this->mediaPath . '/subdir/no-name.mp3']);
self::assertDatabaseMissing('songs', ['path' => $this->mediaPath . '/subdir/back-in-black.mp3']);
self::assertDatabaseMissing('songs', ['path' => $this->path('/subdir/sic.mp3')]);
self::assertDatabaseMissing('songs', ['path' => $this->path('/subdir/no-name.mp3')]);
self::assertDatabaseMissing('songs', ['path' => $this->path('/subdir/back-in-black.mp3')]);
}
public function testHtmlEntities(): void
{
$path = $this->path('/songs/blank.mp3');
$this->swap(
getID3::class,
Mockery::mock(getID3::class, [
'analyze' => [
'filenamepath' => $path,
'tags' => [
'id3v2' => [
'title' => ['&#27700;&#35895;&#24195;&#23455;'],
@ -227,21 +233,21 @@ class MediaSyncServiceTest extends TestCase
/** @var FileSynchronizer $fileSynchronizer */
$fileSynchronizer = app(FileSynchronizer::class);
$info = $fileSynchronizer->setFile(__DIR__ . '/songs/blank.mp3')->getFileInfo();
$info = $fileSynchronizer->setFile($path)->getFileScanInformation();
self::assertEquals('佐倉綾音 Unknown', $info['artist']);
self::assertEquals('小岩井こ Random', $info['album']);
self::assertEquals('水谷広実', $info['title']);
self::assertSame('佐倉綾音 Unknown', $info->artistName);
self::assertSame('小岩井こ Random', $info->albumName);
self::assertSame('水谷広実', $info->title);
}
public function testOptionallyIgnoreHiddenFiles(): void
{
config(['koel.ignore_dot_files' => false]);
$this->mediaService->sync($this->mediaPath);
self::assertDatabaseHas('albums', ['name' => 'Hidden Album']);
$this->mediaService->sync();
self::assertDatabaseHas(Album::class, ['name' => 'Hidden Album']);
config(['koel.ignore_dot_files' => true]);
$this->mediaService->sync($this->mediaPath);
self::assertDatabaseMissing('albums', ['name' => 'Hidden Album']);
$this->mediaService->sync();
self::assertDatabaseMissing(Album::class, ['name' => 'Hidden Album']);
}
}

View file

@ -15,6 +15,8 @@ trait CreatesApplication
public function createApplication(): Application
{
$this->mediaPath = realpath($this->mediaPath);
/** @var Application $app */
$app = require __DIR__ . '/../../bootstrap/app.php';

View file

@ -10,13 +10,14 @@ use App\Services\FileSynchronizer;
use App\Services\UploadService;
use Illuminate\Http\UploadedFile;
use Mockery;
use Mockery\LegacyMockInterface;
use Mockery\MockInterface;
use Tests\TestCase;
class UploadServiceTest extends TestCase
{
private $fileSynchronizer;
private $uploadService;
private FileSynchronizer|MockInterface|LegacyMockInterface $fileSynchronizer;
private UploadService $uploadService;
public function setUp(): void
{
@ -50,7 +51,8 @@ class UploadServiceTest extends TestCase
$this->fileSynchronizer
->shouldReceive('setFile')
->once()
->with('/media/koel/__KOEL_UPLOADS__/foo.mp3');
->with('/media/koel/__KOEL_UPLOADS__/foo.mp3')
->andReturnSelf();
$this->fileSynchronizer
->shouldReceive('sync')
@ -85,12 +87,12 @@ class UploadServiceTest extends TestCase
$this->fileSynchronizer
->shouldReceive('setFile')
->once()
->with('/media/koel/__KOEL_UPLOADS__/foo.mp3');
->with('/media/koel/__KOEL_UPLOADS__/foo.mp3')
->andReturnSelf();
$this->fileSynchronizer
->shouldReceive('sync')
->once()
->with()
->andReturn(FileSynchronizer::SYNC_RESULT_SUCCESS);
$song = new Song();

Binary file not shown.