koel/tests/Integration/Services/LastfmServiceTest.php

112 lines
3.6 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;
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
{
2018-08-31 13:47:15 +00:00
public function testGetArtistInformation(): void
2017-12-10 00:23:37 +00:00
{
/** @var Artist $artist */
$artist = Artist::factory()->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, [
2020-12-22 20:11:22 +00:00
'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));
2022-07-16 22:42:29 +00:00
$info = $api->getArtistInformation($artist);
2017-12-10 00:23:37 +00:00
2020-09-06 18:21:39 +00:00
self::assertEquals([
'url' => 'https://www.last.fm/music/Kamelot',
2022-07-18 11:00:37 +00:00
'image' => null,
2017-12-10 00:23:37 +00:00
'bio' => [
'summary' => 'Quisque ut nisi.',
'full' => 'Quisque ut nisi. Vestibulum ullamcorper mauris at ligula.',
],
2022-07-18 11:00:37 +00:00
], $info->toArray());
2017-12-10 00:23:37 +00:00
2020-12-22 23:01:49 +00:00
self::assertNotNull(cache()->get('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 = Artist::factory()->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, [
2020-12-22 20:11:22 +00:00
'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));
2022-07-16 22:42:29 +00:00
self::assertNull($api->getArtistInformation($artist));
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
{
2021-07-26 21:21:36 +00:00
/** @var Artist $artist */
$artist = Artist::factory()->create(['name' => 'bar']);
2017-12-10 00:23:37 +00:00
/** @var Album $album */
2022-07-27 10:44:25 +00:00
$album = Album::factory()->for($artist)->create(['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, [
2020-12-22 20:11:22 +00:00
'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));
2022-07-16 22:42:29 +00:00
$info = $api->getAlbumInformation($album);
2017-12-10 00:23:37 +00:00
2020-09-06 18:21:39 +00:00
self::assertEquals([
'url' => 'https://www.last.fm/music/Kamelot/Epica',
2022-07-18 11:00:37 +00:00
'cover' => null,
2017-12-10 00:23:37 +00:00
'tracks' => [
[
'title' => 'Track 1',
2022-07-27 10:44:25 +00:00
'url' => 'https://foo/track1',
2017-12-10 00:23:37 +00:00
'length' => 100,
],
[
'title' => 'Track 2',
2022-07-27 10:44:25 +00:00
'url' => 'https://foo/track2',
2017-12-10 00:23:37 +00:00
'length' => 150,
],
],
'wiki' => [
'summary' => 'Quisque ut nisi.',
'full' => 'Quisque ut nisi. Vestibulum ullamcorper mauris at ligula.',
],
2022-07-18 11:00:37 +00:00
], $info->toArray());
2017-12-10 00:23:37 +00:00
2020-12-22 23:01:49 +00:00
self::assertNotNull(cache()->get('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 */
$album = Album::factory()->create();
2017-12-10 00:23:37 +00:00
/** @var Client $client */
2018-08-29 07:05:24 +00:00
$client = Mockery::mock(Client::class, [
2020-12-22 20:11:22 +00:00
'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
2022-07-16 22:42:29 +00:00
self::assertNull($api->getAlbumInformation($album));
2017-12-10 00:23:37 +00:00
}
2017-12-09 22:39:34 +00:00
}