2017-12-09 22:39:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Integration\Services;
|
|
|
|
|
|
|
|
use App\Models\Album;
|
|
|
|
use App\Models\Artist;
|
2018-08-18 13:19:40 +00:00
|
|
|
use App\Services\LastfmService;
|
2018-08-19 11:08:16 +00:00
|
|
|
use Exception;
|
2017-12-09 22:39:34 +00:00
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
|
|
|
use Mockery as m;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
2018-08-19 11:08:16 +00:00
|
|
|
class LastfmServiceTest extends TestCase
|
2017-12-09 22:39:34 +00:00
|
|
|
{
|
2017-12-10 00:23:37 +00:00
|
|
|
/**
|
2018-08-19 11:08:16 +00:00
|
|
|
* @throws Exception
|
2017-12-10 00:23:37 +00:00
|
|
|
*/
|
2018-08-19 11:08:16 +00:00
|
|
|
public function testGetArtistInformation()
|
2017-12-10 00:23:37 +00:00
|
|
|
{
|
|
|
|
/** @var Artist $artist */
|
2018-08-19 11:08:16 +00:00
|
|
|
$artist = factory(Artist::class)->make(['name' => 'foo']);
|
2017-12-10 00:23:37 +00:00
|
|
|
|
2018-08-19 11:08:16 +00:00
|
|
|
/** @var Client $client */
|
2017-12-10 00:23:37 +00:00
|
|
|
$client = m::mock(Client::class, [
|
|
|
|
'get' => new Response(200, [], file_get_contents(__DIR__.'../../../blobs/lastfm/artist.xml')),
|
|
|
|
]);
|
|
|
|
|
2018-08-19 11:08:16 +00:00
|
|
|
$api = new LastfmService($client);
|
|
|
|
$info = $api->getArtistInformation($artist->name
|
|
|
|
|
|
|
|
);
|
2017-12-10 00:23:37 +00:00
|
|
|
|
|
|
|
$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.',
|
|
|
|
],
|
|
|
|
], $info);
|
|
|
|
|
|
|
|
// And the response XML is cached as well
|
|
|
|
$this->assertNotNull(cache('0aff3bc1259154f0e9db860026cda7a6'));
|
|
|
|
}
|
|
|
|
|
2018-08-19 11:08:16 +00:00
|
|
|
public function testGetArtistInformationForNonExistentArtist()
|
2017-12-10 00:23:37 +00:00
|
|
|
{
|
|
|
|
/** @var Artist $artist */
|
2018-08-19 11:08:16 +00:00
|
|
|
$artist = factory(Artist::class)->make();
|
2017-12-10 00:23:37 +00:00
|
|
|
|
2018-08-19 11:08:16 +00:00
|
|
|
/** @var Client $client */
|
2017-12-10 00:23:37 +00:00
|
|
|
$client = m::mock(Client::class, [
|
|
|
|
'get' => new Response(400, [], file_get_contents(__DIR__.'../../../blobs/lastfm/artist-notfound.xml')),
|
|
|
|
]);
|
|
|
|
|
2018-08-19 11:08:16 +00:00
|
|
|
$api = new LastfmService($client);
|
2017-12-10 00:23:37 +00:00
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
$this->assertNull($api->getArtistInformation($artist->name));
|
2017-12-10 00:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-19 11:08:16 +00:00
|
|
|
* @throws Exception
|
2017-12-10 00:23:37 +00:00
|
|
|
*/
|
2018-08-19 11:08:16 +00:00
|
|
|
public function testGetAlbumInformation()
|
2017-12-10 00:23:37 +00:00
|
|
|
{
|
|
|
|
/** @var Album $album */
|
|
|
|
$album = factory(Album::class)->create([
|
|
|
|
'artist_id' => factory(Artist::class)->create(['name' => 'bar'])->id,
|
|
|
|
'name' => 'foo',
|
|
|
|
]);
|
|
|
|
|
2018-08-19 11:08:16 +00:00
|
|
|
/** @var Client $client */
|
2017-12-10 00:23:37 +00:00
|
|
|
$client = m::mock(Client::class, [
|
|
|
|
'get' => new Response(200, [], file_get_contents(__DIR__.'../../../blobs/lastfm/album.xml')),
|
|
|
|
]);
|
|
|
|
|
2018-08-19 11:08:16 +00:00
|
|
|
$api = new LastfmService($client);
|
|
|
|
$info = $api->getAlbumInformation($album->name, $album->artist->name);
|
2017-12-10 00:23:37 +00:00
|
|
|
|
|
|
|
// Then I get the album's info
|
|
|
|
$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.',
|
|
|
|
],
|
|
|
|
], $info);
|
|
|
|
|
|
|
|
$this->assertNotNull(cache('fca889d13b3222589d7d020669cc5a38'));
|
|
|
|
}
|
|
|
|
|
2018-08-19 11:08:16 +00:00
|
|
|
public function testGetAlbumInformationForNonExistentAlbum()
|
2017-12-10 00:23:37 +00:00
|
|
|
{
|
2018-08-19 11:08:16 +00:00
|
|
|
/** @var Album $album */
|
2017-12-10 00:23:37 +00:00
|
|
|
$album = factory(Album::class)->create();
|
|
|
|
|
2018-08-19 11:08:16 +00:00
|
|
|
/** @var Client $client */
|
2017-12-10 00:23:37 +00:00
|
|
|
$client = m::mock(Client::class, [
|
|
|
|
'get' => new Response(400, [], file_get_contents(__DIR__.'../../../blobs/lastfm/album-notfound.xml')),
|
|
|
|
]);
|
|
|
|
|
2018-08-19 11:08:16 +00:00
|
|
|
$api = new LastfmService($client);
|
2017-12-10 00:23:37 +00:00
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
$this->assertNull($api->getAlbumInformation($album->name, $album->artist->name));
|
2017-12-10 00:23:37 +00:00
|
|
|
}
|
2017-12-09 22:39:34 +00:00
|
|
|
}
|