2017-06-10 00:40:44 +00:00
|
|
|
<?php
|
|
|
|
|
2022-07-29 06:47:10 +00:00
|
|
|
namespace Tests\Unit\Models;
|
2017-06-10 00:40:44 +00:00
|
|
|
|
|
|
|
use App\Models\Album;
|
|
|
|
use App\Models\Artist;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class AlbumTest extends TestCase
|
|
|
|
{
|
2020-11-14 16:57:25 +00:00
|
|
|
public function testExistingAlbumCanBeRetrievedUsingArtistAndName(): void
|
2017-06-10 00:40:44 +00:00
|
|
|
{
|
2017-06-10 15:09:56 +00:00
|
|
|
/** @var Album $album */
|
2020-11-14 16:57:25 +00:00
|
|
|
$album = Album::factory()->create();
|
2017-06-10 00:40:44 +00:00
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
self::assertTrue(Album::getOrCreate($album->artist, $album->name)->is($album));
|
2017-06-10 00:40:44 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
public function testNewAlbumIsAutomaticallyCreatedWithArtistAndName(): void
|
2017-06-10 00:40:44 +00:00
|
|
|
{
|
2020-11-14 16:57:25 +00:00
|
|
|
/** @var Artist $artist */
|
|
|
|
$artist = Artist::factory()->create();
|
2017-06-10 00:40:44 +00:00
|
|
|
$name = 'Foo';
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
self::assertNull(Album::query()->where('artist_id', $artist->id)->where('name', $name)->first());
|
2017-06-10 00:40:44 +00:00
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
$album = Album::getOrCreate($artist, $name);
|
|
|
|
self::assertSame('Foo', $album->name);
|
|
|
|
self::assertTrue($album->artist->is($artist));
|
2017-06-10 00:40:44 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
/** @return array<mixed> */
|
2020-11-14 16:57:25 +00:00
|
|
|
public function provideEmptyAlbumNames(): array
|
2017-06-10 00:40:44 +00:00
|
|
|
{
|
2020-11-14 16:57:25 +00:00
|
|
|
return [
|
|
|
|
[''],
|
|
|
|
[' '],
|
|
|
|
[null],
|
|
|
|
[false],
|
|
|
|
];
|
2017-06-10 00:40:44 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
/** @dataProvider provideEmptyAlbumNames */
|
|
|
|
public function testNewAlbumWithoutNameIsCreatedAsUnknownAlbum($name): void
|
2017-06-10 00:40:44 +00:00
|
|
|
{
|
2021-07-26 21:21:36 +00:00
|
|
|
/** @var Artist $artist */
|
|
|
|
$artist = Artist::factory()->create();
|
|
|
|
|
|
|
|
$album = Album::getOrCreate($artist, $name);
|
|
|
|
|
|
|
|
self::assertEquals('Unknown Album', $album->name);
|
2020-11-14 16:57:25 +00:00
|
|
|
}
|
2017-06-10 00:40:44 +00:00
|
|
|
}
|