2015-12-19 16:36:44 +00:00
|
|
|
<?php
|
|
|
|
|
2015-12-21 13:49:00 +00:00
|
|
|
use App\Events\SongLikeToggled;
|
2015-12-23 06:26:16 +00:00
|
|
|
use App\Events\SongStartedPlaying;
|
2015-12-20 12:30:28 +00:00
|
|
|
use App\Http\Controllers\API\LastfmController;
|
2015-12-21 13:49:00 +00:00
|
|
|
use App\Listeners\LoveTrackOnLastfm;
|
2015-12-23 06:26:16 +00:00
|
|
|
use App\Listeners\UpdateLastfmNowPlaying;
|
2015-12-21 13:49:00 +00:00
|
|
|
use App\Models\Interaction;
|
|
|
|
use App\Models\Song;
|
2015-12-20 12:30:28 +00:00
|
|
|
use App\Models\User;
|
2015-12-19 17:08:03 +00:00
|
|
|
use App\Services\Lastfm;
|
2015-12-19 16:36:44 +00:00
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
2015-12-20 12:30:28 +00:00
|
|
|
use Illuminate\Contracts\Auth\Guard;
|
2015-12-19 17:08:03 +00:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
2015-12-20 12:30:28 +00:00
|
|
|
use Illuminate\Http\Request;
|
2015-12-20 12:17:35 +00:00
|
|
|
use Illuminate\Routing\Redirector;
|
|
|
|
use Mockery as m;
|
2015-12-30 04:14:47 +00:00
|
|
|
use Tymon\JWTAuth\JWTAuth;
|
2015-12-19 16:36:44 +00:00
|
|
|
|
|
|
|
class LastfmTest extends TestCase
|
|
|
|
{
|
|
|
|
use DatabaseTransactions, WithoutMiddleware;
|
|
|
|
|
|
|
|
public function testGetArtistInfo()
|
|
|
|
{
|
2015-12-20 12:17:35 +00:00
|
|
|
$client = m::mock(Client::class, [
|
2015-12-19 16:36:44 +00:00
|
|
|
'get' => new Response(200, [], file_get_contents(dirname(__FILE__).'/blobs/lastfm/artist.xml')),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$api = new Lastfm(null, null, $client);
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
'url' => 'http://www.last.fm/music/Kamelot',
|
|
|
|
'image' => 'http://foo.bar/extralarge.jpg',
|
|
|
|
'bio' => [
|
|
|
|
'summary' => 'Quisque ut nisi.',
|
|
|
|
'full' => 'Quisque ut nisi. Vestibulum ullamcorper mauris at ligula.',
|
|
|
|
],
|
|
|
|
], $api->getArtistInfo('foo'));
|
|
|
|
|
|
|
|
// Is it cached?
|
|
|
|
$this->assertNotNull(Cache::get(md5('lastfm_artist_foo')));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetArtistInfoFailed()
|
|
|
|
{
|
2015-12-20 12:17:35 +00:00
|
|
|
$client = m::mock(Client::class, [
|
2015-12-19 16:36:44 +00:00
|
|
|
'get' => new Response(400, [], file_get_contents(dirname(__FILE__).'/blobs/lastfm/artist-notfound.xml')),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$api = new Lastfm(null, null, $client);
|
|
|
|
|
|
|
|
$this->assertFalse($api->getArtistInfo('foo'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAlbumInfo()
|
|
|
|
{
|
2015-12-20 12:17:35 +00:00
|
|
|
$client = m::mock(Client::class, [
|
2015-12-19 16:36:44 +00:00
|
|
|
'get' => new Response(200, [], file_get_contents(dirname(__FILE__).'/blobs/lastfm/album.xml')),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$api = new Lastfm(null, null, $client);
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
'url' => 'http://www.last.fm/music/Kamelot/Epica',
|
|
|
|
'image' => 'http://foo.bar/extralarge.jpg',
|
|
|
|
'tracks' => [
|
|
|
|
[
|
|
|
|
'title' => 'Track 1',
|
|
|
|
'url' => 'http://foo/track1',
|
|
|
|
'length' => 100,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'title' => 'Track 2',
|
|
|
|
'url' => 'http://foo/track2',
|
|
|
|
'length' => 150,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'wiki' => [
|
|
|
|
'summary' => 'Quisque ut nisi.',
|
|
|
|
'full' => 'Quisque ut nisi. Vestibulum ullamcorper mauris at ligula.',
|
|
|
|
],
|
|
|
|
], $api->getAlbumInfo('foo', 'bar'));
|
|
|
|
|
|
|
|
// Is it cached?
|
|
|
|
$this->assertNotNull(Cache::get(md5('lastfm_album_foo_bar')));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAlbumInfoFailed()
|
|
|
|
{
|
2015-12-20 12:17:35 +00:00
|
|
|
$client = m::mock(Client::class, [
|
2015-12-19 16:36:44 +00:00
|
|
|
'get' => new Response(400, [], file_get_contents(dirname(__FILE__).'/blobs/lastfm/album-notfound.xml')),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$api = new Lastfm(null, null, $client);
|
|
|
|
|
|
|
|
$this->assertFalse($api->getAlbumInfo('foo', 'bar'));
|
|
|
|
}
|
2015-12-20 12:17:35 +00:00
|
|
|
|
|
|
|
public function testBuildAuthCallParams()
|
|
|
|
{
|
|
|
|
$api = new Lastfm('key', 'secret');
|
|
|
|
$params = [
|
|
|
|
'qux' => '安',
|
|
|
|
'bar' => 'baz',
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
'api_key' => 'key',
|
|
|
|
'bar' => 'baz',
|
|
|
|
'qux' => '安',
|
|
|
|
'api_sig' => '7f21233b54edea994aa0f23cf55f18a2',
|
|
|
|
], $api->buildAuthCallParams($params));
|
|
|
|
|
2015-12-20 12:30:28 +00:00
|
|
|
$this->assertEquals('api_key=key&bar=baz&qux=安&api_sig=7f21233b54edea994aa0f23cf55f18a2',
|
2015-12-20 12:17:35 +00:00
|
|
|
$api->buildAuthCallParams($params, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSessionKey()
|
|
|
|
{
|
|
|
|
$client = m::mock(Client::class, [
|
|
|
|
'get' => new Response(200, [], file_get_contents(dirname(__FILE__).'/blobs/lastfm/session-key.xml')),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$api = new Lastfm(null, null, $client);
|
|
|
|
|
|
|
|
$this->assertEquals('foo', $api->getSessionKey('bar'));
|
|
|
|
}
|
|
|
|
|
2016-01-26 06:32:29 +00:00
|
|
|
public function testSetSessionKey()
|
|
|
|
{
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
$this->actingAs($user)->post('api/lastfm/session-key', ['key' => 'foo']);
|
|
|
|
$this->assertEquals('foo', $user->getLastfmSessionKey());
|
|
|
|
}
|
|
|
|
|
2015-12-20 12:17:35 +00:00
|
|
|
public function testControllerConnect()
|
|
|
|
{
|
|
|
|
$redirector = m::mock(Redirector::class);
|
|
|
|
$redirector->shouldReceive('to')->once();
|
|
|
|
|
|
|
|
$guard = m::mock(Guard::class, ['user' => factory(User::class)->create()]);
|
2015-12-30 04:14:47 +00:00
|
|
|
$auth = m::mock(JWTAuth::class, [
|
|
|
|
'parseToken' => '',
|
|
|
|
'getToken' => '',
|
|
|
|
]);
|
2015-12-20 12:17:35 +00:00
|
|
|
|
2015-12-30 04:14:47 +00:00
|
|
|
(new LastfmController($guard))->connect($redirector, new Lastfm(), $auth);
|
2015-12-20 12:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testControllerCallback()
|
|
|
|
{
|
|
|
|
$request = m::mock(Request::class, ['input' => 'token']);
|
|
|
|
$lastfm = m::mock(Lastfm::class, ['getSessionKey' => 'bar']);
|
|
|
|
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
$guard = m::mock(Guard::class, ['user' => $user]);
|
|
|
|
|
|
|
|
(new LastfmController($guard))->callback($request, $lastfm);
|
|
|
|
|
2015-12-21 13:49:00 +00:00
|
|
|
$this->assertEquals('bar', $user->getLastfmSessionKey());
|
2015-12-20 12:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testControllerDisconnect()
|
|
|
|
{
|
|
|
|
$user = factory(User::class)->create(['preferences' => ['lastfm_session_key' => 'bar']]);
|
|
|
|
$this->actingAs($user)->delete('api/lastfm/disconnect');
|
2015-12-21 13:49:00 +00:00
|
|
|
$this->assertNull($user->getLastfmSessionKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLoveTrack()
|
|
|
|
{
|
|
|
|
$this->withoutEvents();
|
|
|
|
$this->createSampleMediaSet();
|
|
|
|
|
|
|
|
$user = factory(User::class)->create(['preferences' => ['lastfm_session_key' => 'bar']]);
|
|
|
|
|
|
|
|
$interaction = Interaction::create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'song_id' => Song::first()->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$lastfm = m::mock(Lastfm::class, ['enabled' => true]);
|
|
|
|
$lastfm->shouldReceive('toggleLoveTrack')
|
2015-12-23 06:26:16 +00:00
|
|
|
->with($interaction->song->title, $interaction->song->album->artist->name, 'bar', false);
|
2015-12-21 13:49:00 +00:00
|
|
|
|
|
|
|
(new LoveTrackOnLastfm($lastfm))->handle(new SongLikeToggled($interaction, $user));
|
2015-12-20 12:17:35 +00:00
|
|
|
}
|
2015-12-23 06:26:16 +00:00
|
|
|
|
|
|
|
public function testUpdateNowPlaying()
|
|
|
|
{
|
|
|
|
$this->withoutEvents();
|
|
|
|
$this->createSampleMediaSet();
|
|
|
|
|
|
|
|
$user = factory(User::class)->create(['preferences' => ['lastfm_session_key' => 'bar']]);
|
|
|
|
$song = Song::first();
|
|
|
|
|
|
|
|
$lastfm = m::mock(Lastfm::class, ['enabled' => true]);
|
|
|
|
$lastfm->shouldReceive('updateNowPlaying')
|
|
|
|
->with($song->album->artist->name, $song->title, $song->album->name, $song->length, 'bar');
|
|
|
|
|
|
|
|
(new UpdateLastfmNowPlaying($lastfm))->handle(new SongStartedPlaying($song, $user));
|
|
|
|
}
|
2015-12-19 16:36:44 +00:00
|
|
|
}
|