2017-08-05 16:56:11 +00:00
|
|
|
<?php
|
|
|
|
|
2017-12-09 22:39:34 +00:00
|
|
|
namespace Tests\Integration\Models;
|
2017-08-05 16:56:11 +00:00
|
|
|
|
|
|
|
use App\Models\Song;
|
|
|
|
use App\Models\User;
|
|
|
|
use Aws\AwsClient;
|
|
|
|
use Cache;
|
|
|
|
use Lastfm;
|
|
|
|
use Mockery as m;
|
|
|
|
use Tests\TestCase;
|
|
|
|
use YouTube;
|
|
|
|
|
|
|
|
class SongTest extends TestCase
|
|
|
|
{
|
2017-12-09 22:39:34 +00:00
|
|
|
protected function tearDown()
|
|
|
|
{
|
|
|
|
parent::tearDown();
|
|
|
|
m::close();
|
|
|
|
}
|
|
|
|
|
2017-08-05 16:56:11 +00:00
|
|
|
/** @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,
|
|
|
|
]),
|
|
|
|
]);
|
|
|
|
|
2017-08-05 22:27:26 +00:00
|
|
|
Cache::shouldReceive('remember')->andReturn($mockedURL);
|
2017-08-05 16:56:11 +00:00
|
|
|
$url = $song->getObjectStoragePublicUrl($client);
|
|
|
|
|
|
|
|
// Then I should receive the correct S3 public URL
|
|
|
|
$this->assertEquals($mockedURL, $url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @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()
|
|
|
|
{
|
2017-08-28 11:29:39 +00:00
|
|
|
Lastfm::shouldReceive('scrobble')->times(0);
|
2017-08-06 09:43:59 +00:00
|
|
|
|
2017-08-05 16:56:11 +00:00
|
|
|
// 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
|
|
|
|
|
|
|
|
$song->scrobble($user, time());
|
|
|
|
|
2017-08-06 09:43:59 +00:00
|
|
|
// Then the song shouldn't be scrobbled
|
2017-08-05 16:56:11 +00:00
|
|
|
}
|
|
|
|
|
2017-12-09 22:39:34 +00:00
|
|
|
/** @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 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);
|
|
|
|
}
|
2017-08-05 16:56:11 +00:00
|
|
|
}
|