koel/tests/Integration/Services/LastfmServiceTest.php
2018-08-19 13:13:08 +02:00

122 lines
3.7 KiB
PHP

<?php
namespace Tests\Integration\Services;
use App\Models\Album;
use App\Models\Artist;
use App\Services\LastfmService;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Mockery as m;
use Tests\TestCase;
class LastfmServiceTest extends TestCase
{
/**
* @throws Exception
*/
public function testGetArtistInformation()
{
/** @var Artist $artist */
$artist = factory(Artist::class)->make(['name' => 'foo']);
/** @var Client $client */
$client = m::mock(Client::class, [
'get' => new Response(200, [], file_get_contents(__DIR__.'../../../blobs/lastfm/artist.xml')),
]);
$api = new LastfmService($client);
$info = $api->getArtistInformation($artist->name
);
$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'));
}
public function testGetArtistInformationForNonExistentArtist()
{
/** @var Artist $artist */
$artist = factory(Artist::class)->make();
/** @var Client $client */
$client = m::mock(Client::class, [
'get' => new Response(400, [], file_get_contents(__DIR__.'../../../blobs/lastfm/artist-notfound.xml')),
]);
$api = new LastfmService($client);
$result = $api->getArtistInformation($artist->name);
$this->assertFalse($result);
}
/**
* @throws Exception
*/
public function testGetAlbumInformation()
{
/** @var Album $album */
$album = factory(Album::class)->create([
'artist_id' => factory(Artist::class)->create(['name' => 'bar'])->id,
'name' => 'foo',
]);
/** @var Client $client */
$client = m::mock(Client::class, [
'get' => new Response(200, [], file_get_contents(__DIR__.'../../../blobs/lastfm/album.xml')),
]);
$api = new LastfmService($client);
$info = $api->getAlbumInformation($album->name, $album->artist->name);
// 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'));
}
public function testGetAlbumInformationForNonExistentAlbum()
{
/** @var Album $album */
$album = factory(Album::class)->create();
/** @var Client $client */
$client = m::mock(Client::class, [
'get' => new Response(400, [], file_get_contents(__DIR__.'../../../blobs/lastfm/album-notfound.xml')),
]);
$api = new LastfmService($client);
$result = $api->getAlbumInformation($album->name, $album->artist->name);
$this->assertFalse($result);
}
}