koel/tests/Integration/Services/LastfmServiceTest.php

120 lines
3.8 KiB
PHP
Raw Normal View History

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;
use Exception;
2017-12-09 22:39:34 +00:00
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
2018-08-29 07:05:24 +00:00
use Illuminate\Contracts\Cache\Repository as Cache;
2018-08-31 13:47:15 +00:00
use Illuminate\Log\Logger;
2018-08-29 07:05:24 +00:00
use Mockery;
2017-12-09 22:39:34 +00:00
use Tests\TestCase;
class LastfmServiceTest extends TestCase
2017-12-09 22:39:34 +00:00
{
2017-12-10 00:23:37 +00:00
/**
* @throws Exception
2017-12-10 00:23:37 +00:00
*/
2018-08-31 13:47:15 +00:00
public function testGetArtistInformation(): void
2017-12-10 00:23:37 +00:00
{
/** @var Artist $artist */
$artist = factory(Artist::class)->make(['name' => 'foo']);
2017-12-10 00:23:37 +00:00
/** @var Client $client */
2018-08-29 07:05:24 +00:00
$client = Mockery::mock(Client::class, [
'get' => new Response(200, [], file_get_contents(__DIR__.'../../../blobs/lastfm/artist.json')),
2017-12-10 00:23:37 +00:00
]);
2018-09-04 06:25:24 +00:00
$api = new LastfmService($client, app(Cache::class), app(Logger::class));
$info = $api->getArtistInformation($artist->name);
2017-12-10 00:23:37 +00:00
$this->assertEquals([
'url' => 'https://www.last.fm/music/Kamelot',
2017-12-10 00:23:37 +00:00
'image' => 'http://foo.bar/extralarge.jpg',
'bio' => [
'summary' => 'Quisque ut nisi.',
'full' => 'Quisque ut nisi. Vestibulum ullamcorper mauris at ligula.',
],
], $info);
2018-08-31 13:47:15 +00:00
self::assertNotNull(cache('0aff3bc1259154f0e9db860026cda7a6'));
2017-12-10 00:23:37 +00:00
}
2018-08-31 13:47:15 +00:00
public function testGetArtistInformationForNonExistentArtist(): void
2017-12-10 00:23:37 +00:00
{
/** @var Artist $artist */
$artist = factory(Artist::class)->make();
2017-12-10 00:23:37 +00:00
/** @var Client $client */
2018-08-29 07:05:24 +00:00
$client = Mockery::mock(Client::class, [
'get' => new Response(400, [], file_get_contents(__DIR__.'../../../blobs/lastfm/artist-notfound.json')),
2017-12-10 00:23:37 +00:00
]);
2018-09-04 06:25:24 +00:00
$api = new LastfmService($client, app(Cache::class), app(Logger::class));
self::assertNull($api->getArtistInformation($artist->name));
2017-12-10 00:23:37 +00:00
}
/**
* @throws Exception
2017-12-10 00:23:37 +00:00
*/
2018-08-31 13:47:15 +00:00
public function testGetAlbumInformation(): void
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',
]);
/** @var Client $client */
2018-08-29 07:05:24 +00:00
$client = Mockery::mock(Client::class, [
'get' => new Response(200, [], file_get_contents(__DIR__.'../../../blobs/lastfm/album.json')),
2017-12-10 00:23:37 +00:00
]);
2018-09-04 06:25:24 +00:00
$api = new LastfmService($client, app(Cache::class), app(Logger::class));
$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' => 'https://www.last.fm/music/Kamelot/Epica',
2017-12-10 00:23:37 +00:00
'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);
2018-08-31 13:47:15 +00:00
self::assertNotNull(cache('fca889d13b3222589d7d020669cc5a38'));
2017-12-10 00:23:37 +00:00
}
2018-08-31 13:47:15 +00:00
public function testGetAlbumInformationForNonExistentAlbum(): void
2017-12-10 00:23:37 +00:00
{
/** @var Album $album */
2017-12-10 00:23:37 +00:00
$album = factory(Album::class)->create();
/** @var Client $client */
2018-08-29 07:05:24 +00:00
$client = Mockery::mock(Client::class, [
'get' => new Response(400, [], file_get_contents(__DIR__.'../../../blobs/lastfm/album-notfound.json')),
2017-12-10 00:23:37 +00:00
]);
2018-09-04 06:25:24 +00:00
$api = new LastfmService($client, app(Cache::class), app(Logger::class));
2017-12-10 00:23:37 +00:00
2018-08-31 13:47:15 +00:00
self::assertNull($api->getAlbumInformation($album->name, $album->artist->name));
2017-12-10 00:23:37 +00:00
}
2017-12-09 22:39:34 +00:00
}