mirror of
https://github.com/koel/koel
synced 2024-11-10 06:34:14 +00:00
Finish unit tests for Song
This commit is contained in:
parent
6e38f96eb2
commit
14b6c7154b
7 changed files with 167 additions and 71 deletions
|
@ -337,16 +337,6 @@ class Song extends Model
|
|||
return str_replace(["\r\n", "\r", "\n"], '<br />', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the song is an AWS S3 Object.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isS3ObjectAttribute()
|
||||
{
|
||||
return starts_with($this->path, 's3://');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the bucket and key name of an S3 object.
|
||||
*
|
||||
|
|
|
@ -33,7 +33,11 @@ $factory->define(App\Models\Album::class, function ($faker) {
|
|||
});
|
||||
|
||||
$factory->define(App\Models\Song::class, function ($faker) {
|
||||
$album = factory(\App\Models\Album::class)->create();
|
||||
|
||||
return [
|
||||
'album_id' => $album->id,
|
||||
'artist_id' => $album->artist->id,
|
||||
'title' => ucwords($faker->words(random_int(2, 5), true)),
|
||||
'length' => $faker->randomFloat(2, 10, 500),
|
||||
'track' => random_int(1, 20),
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Artist;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Tests\BrowserKitTestCase;
|
||||
|
||||
class ArtistTest extends BrowserKitTestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
public function testShouldBeCreatedWithUniqueNames()
|
||||
{
|
||||
$name = 'Foo Fighters';
|
||||
$artist = Artist::get($name);
|
||||
|
||||
$this->assertEquals($name, $artist->name);
|
||||
|
||||
// Should be only 3 records: UNKNOWN_ARTIST, VARIOUS_ARTISTS, and our Dave Grohl's band
|
||||
$this->assertEquals(3, Artist::all()->count());
|
||||
|
||||
Artist::get($name);
|
||||
|
||||
// Should still be 3.
|
||||
$this->assertEquals(3, Artist::all()->count());
|
||||
}
|
||||
|
||||
public function testArtistWithEmptyNameShouldBeUnknown()
|
||||
{
|
||||
$this->assertEquals(Artist::UNKNOWN_NAME, Artist::get('')->name);
|
||||
}
|
||||
|
||||
public function testUtf16Names()
|
||||
{
|
||||
$name = file_get_contents(__DIR__.'../../blobs/utf16');
|
||||
|
||||
$artist = Artist::get($name);
|
||||
$artist = Artist::get($name); // to make sure there's no constraint exception
|
||||
|
||||
$this->assertEquals($artist->id, Artist::get($name)->id);
|
||||
}
|
||||
}
|
|
@ -305,24 +305,6 @@ class SongTest extends BrowserKitTestCase
|
|||
]);
|
||||
}
|
||||
|
||||
public function testGetObjectStoragePublicUrl()
|
||||
{
|
||||
$song = Song::first();
|
||||
$song->path = 's3://foo/bar.mp3';
|
||||
$fakeUrl = 'http://aws.com/foo/bar.mp3';
|
||||
|
||||
$client = m::mock(AwsClient::class, [
|
||||
'getCommand' => null,
|
||||
'createPresignedRequest' => m::mock(Request::class, [
|
||||
'getUri' => $fakeUrl,
|
||||
]),
|
||||
]);
|
||||
|
||||
Cache::shouldReceive('get')->once()->with("OSUrl/{$song->id}");
|
||||
Cache::shouldReceive('put')->once()->with("OSUrl/{$song->id}", $fakeUrl, 60);
|
||||
$this->assertEquals($fakeUrl, $song->getObjectStoragePublicUrl($client));
|
||||
}
|
||||
|
||||
public function testDeletingByChunk()
|
||||
{
|
||||
$this->assertNotEquals(0, Song::count());
|
||||
|
|
|
@ -23,7 +23,9 @@ class AlbumTest extends TestCase
|
|||
public function exist_album_can_be_retrieved_using_artist_and_name()
|
||||
{
|
||||
// Given there's an exist album from an artist
|
||||
/** @var Artist $artist */
|
||||
$artist = factory(Artist::class)->create();
|
||||
/** @var Album $album */
|
||||
$album = factory(Album::class)->create([
|
||||
'artist_id' => $artist->id,
|
||||
]);
|
||||
|
@ -82,6 +84,7 @@ class AlbumTest extends TestCase
|
|||
public function it_can_write_a_cover_file_and_update_itself_with_the_cover_file()
|
||||
{
|
||||
// Given there's an album and a cover file content
|
||||
/** @var Album $album */
|
||||
$album = factory(Album::class)->create();
|
||||
$coverContent = 'dummy';
|
||||
$root = vfsStream::setup('home');
|
||||
|
@ -101,6 +104,7 @@ class AlbumTest extends TestCase
|
|||
public function it_can_copy_a_cover_file_and_update_itself_with_the_cover_file()
|
||||
{
|
||||
// Given there's an album and an original image file
|
||||
/** @var Album $album */
|
||||
$album = factory(Album::class)->create();
|
||||
$root = vfsStream::setup('home');
|
||||
$imageFile = vfsStream::newFile('foo.jpg')->at($root)->setContent('foo');
|
||||
|
@ -120,6 +124,7 @@ class AlbumTest extends TestCase
|
|||
public function extra_info_can_be_retrieved_for_an_album()
|
||||
{
|
||||
// Given there's an album
|
||||
/** @var Album $album */
|
||||
$album = factory(Album::class)->create();
|
||||
|
||||
// When I get the extra info for the album
|
||||
|
|
|
@ -32,6 +32,7 @@ class ArtistTest extends TestCase
|
|||
public function existing_artist_can_be_retrieved_using_name()
|
||||
{
|
||||
// Given an existing artist with a name
|
||||
/** @var Artist $artist */
|
||||
$artist = factory(Artist::class)->create(['name' => 'Foo']);
|
||||
|
||||
// When I get the artist by name
|
||||
|
@ -74,6 +75,7 @@ class ArtistTest extends TestCase
|
|||
public function it_can_write_an_image_file_and_update_itself_with_the_image()
|
||||
{
|
||||
// Given there's an artist and an image file content
|
||||
/** @var Artist $artist */
|
||||
$artist = factory(Artist::class)->create();
|
||||
$imageContent = 'dummy';
|
||||
$root = vfsStream::setup('home');
|
||||
|
@ -93,6 +95,7 @@ class ArtistTest extends TestCase
|
|||
public function extra_info_can_be_retrieved_for_an_artist()
|
||||
{
|
||||
// Given there's an artist
|
||||
/** @var Artist $artist */
|
||||
$artist = factory(Artist::class)->create();
|
||||
|
||||
// When I get the extra info
|
||||
|
|
155
tests/Unit/SongTest.php
Normal file
155
tests/Unit/SongTest.php
Normal file
|
@ -0,0 +1,155 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Facades\YouTube;
|
||||
use App\Http\Requests\API\ObjectStorage\S3\Request;
|
||||
use App\Models\Song;
|
||||
use App\Models\User;
|
||||
use Aws\AwsClient;
|
||||
use Cache;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Lastfm;
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SongTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_can_be_instantiated()
|
||||
{
|
||||
$this->assertInstanceOf(Song::class, new Song());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_scrobbles_if_the_user_is_connected_to_lastfm()
|
||||
{
|
||||
// Given there's a song
|
||||
/** @var Song $song */
|
||||
$song = factory(Song::class)->create();
|
||||
|
||||
// And a user who's connected to lastfm
|
||||
/** @var User $user */
|
||||
$user = factory(User::class)->create();
|
||||
$user->setPreference('lastfm_session_key', 'foo');
|
||||
|
||||
// When I call the scrobble method
|
||||
$time = time();
|
||||
Lastfm::shouldReceive('scrobble')
|
||||
->once()
|
||||
->with($song->artist->name, $song->title, $time, $song->album->name, 'foo');
|
||||
|
||||
$song->scrobble($user, $time);
|
||||
|
||||
// Then I see the song is scrobbled
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_does_not_scrobble_if_the_user_is_not_connected_to_lastfm()
|
||||
{
|
||||
// Given there's a song
|
||||
/** @var Song $song */
|
||||
$song = factory(Song::class)->create();
|
||||
|
||||
// And a user who is not connected to lastfm
|
||||
/** @var User $user */
|
||||
$user = factory(User::class)->create();
|
||||
$user->setPreference('lastfm_session_key', false);
|
||||
|
||||
// When I call the scrobble method
|
||||
Lastfm::shouldNotReceive('scrobble');
|
||||
|
||||
$song->scrobble($user, time());
|
||||
|
||||
// The the song shouldn't be scrobbled
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_be_retrieved_using_its_path()
|
||||
{
|
||||
// Given a song with a path
|
||||
/** @var Song $song */
|
||||
$song = factory(Song::class)->create(['path' => 'foo']);
|
||||
|
||||
// When I retrieve it using the path
|
||||
$retrieved = Song::byPath('foo');
|
||||
|
||||
// Then the song is retrieved
|
||||
$this->assertEquals($song->id, $retrieved->id);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_returns_object_storage_public_url()
|
||||
{
|
||||
// Given there's a song hosted on Amazon S3
|
||||
/** @var Song $song */
|
||||
$song = factory(Song::class)->create(['path' => 's3://foo/bar']);
|
||||
$mockedURL = 'http://aws.com/foo/bar';
|
||||
|
||||
// When I get the song's object storage public URL
|
||||
$client = m::mock(AwsClient::class, [
|
||||
'getCommand' => null,
|
||||
'createPresignedRequest' => m::mock(Request::class, [
|
||||
'getUri' => $mockedURL,
|
||||
]),
|
||||
]);
|
||||
|
||||
Cache::shouldReceive('get')->once()->with("OSUrl/{$song->id}");
|
||||
Cache::shouldReceive('put')->once()->with("OSUrl/{$song->id}", $mockedURL, 60);
|
||||
$url = $song->getObjectStoragePublicUrl($client);
|
||||
|
||||
// Then I should receive the correct S3 public URL
|
||||
$this->assertEquals($mockedURL, $url);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_gets_related_youtube_videos()
|
||||
{
|
||||
// Given there's a song
|
||||
/** @var Song $song */
|
||||
$song = factory(Song::class)->create();
|
||||
|
||||
// When I get is related YouTube videos
|
||||
YouTube::shouldReceive('searchVideosRelatedToSong')
|
||||
->once()
|
||||
->with($song, 'foo')
|
||||
->andReturn(['bar' => 'baz']);
|
||||
|
||||
$videos = $song->getRelatedYouTubeVideos('foo');
|
||||
|
||||
// Then I see the related YouTube videos returned
|
||||
$this->assertEquals(['bar' => 'baz'], $videos);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function its_lyrics_has_all_new_line_characters_replace_by_br_tags()
|
||||
{
|
||||
// Given a song with lyrics contains new line characters
|
||||
/** @var Song $song */
|
||||
$song = factory(Song::class)->create([
|
||||
'lyrics' => "foo\rbar\nbaz\r\nqux",
|
||||
]);
|
||||
|
||||
// When I retrieve its lyrics
|
||||
$lyrics = $song->lyrics;
|
||||
|
||||
// Then I see the new line characters replaced by <br />
|
||||
$this->assertEquals('foo<br />bar<br />baz<br />qux', $lyrics);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function amazon_s3_parameters_can_be_retrieved_from_s3_hosted_songs()
|
||||
{
|
||||
// Given a song hosted on S3
|
||||
/** @var Song $song */
|
||||
$song = factory(Song::class)->create(['path' => 's3://foo/bar']);
|
||||
|
||||
// When I check its S3 parameters
|
||||
$params = $song->s3_params;
|
||||
|
||||
// Then I receive the correct parameters
|
||||
$this->assertEquals(['bucket' => 'foo', 'key' => 'bar'], $params);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue