mirror of
https://github.com/koel/koel
synced 2024-11-10 06:34:14 +00:00
Complete unit tests for Lastfm service
This commit is contained in:
parent
2eb0d7089c
commit
fd80aaf291
2 changed files with 153 additions and 95 deletions
|
@ -26,101 +26,6 @@ class LastfmTest extends BrowserKitTestCase
|
|||
{
|
||||
use DatabaseTransactions, WithoutMiddleware;
|
||||
|
||||
public function testGetArtistInfo()
|
||||
{
|
||||
$client = m::mock(Client::class, [
|
||||
'get' => new Response(200, [], file_get_contents(__DIR__.'../../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(md5('lastfm_artist_foo')));
|
||||
}
|
||||
|
||||
public function testGetArtistInfoFailed()
|
||||
{
|
||||
$client = m::mock(Client::class, [
|
||||
'get' => new Response(400, [], file_get_contents(__DIR__.'../../blobs/lastfm/artist-notfound.xml')),
|
||||
]);
|
||||
|
||||
$api = new Lastfm(null, null, $client);
|
||||
|
||||
$this->assertFalse($api->getArtistInfo('foo'));
|
||||
}
|
||||
|
||||
public function testGetAlbumInfo()
|
||||
{
|
||||
$client = m::mock(Client::class, [
|
||||
'get' => new Response(200, [], file_get_contents(__DIR__.'../../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(md5('lastfm_album_foo_bar')));
|
||||
}
|
||||
|
||||
public function testGetAlbumInfoFailed()
|
||||
{
|
||||
$client = m::mock(Client::class, [
|
||||
'get' => new Response(400, [], file_get_contents(__DIR__.'../../blobs/lastfm/album-notfound.xml')),
|
||||
]);
|
||||
|
||||
$api = new Lastfm(null, null, $client);
|
||||
|
||||
$this->assertFalse($api->getAlbumInfo('foo', 'bar'));
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
$this->assertEquals('api_key=key&bar=baz&qux=安&api_sig=7f21233b54edea994aa0f23cf55f18a2',
|
||||
$api->buildAuthCallParams($params, true));
|
||||
}
|
||||
|
||||
public function testGetSessionKey()
|
||||
{
|
||||
$client = m::mock(Client::class, [
|
||||
|
|
153
tests/Unit/LastfmTest.php
Normal file
153
tests/Unit/LastfmTest.php
Normal file
|
@ -0,0 +1,153 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\Album;
|
||||
use App\Models\Artist;
|
||||
use App\Services\Lastfm;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class LastfmTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_builds_lastfm_compatible_api_parameters()
|
||||
{
|
||||
// Given there are raw parameters
|
||||
$api = new Lastfm('key', 'secret');
|
||||
$params = [
|
||||
'qux' => '安',
|
||||
'bar' => 'baz',
|
||||
];
|
||||
|
||||
// When I build Last.fm-compatible API parameters using the raw parameters
|
||||
$builtParams = $api->buildAuthCallParams($params);
|
||||
$builtParamsAsString = $api->buildAuthCallParams($params, true);
|
||||
|
||||
// Then I receive the Last.fm-compatible API parameters
|
||||
$this->assertEquals([
|
||||
'api_key' => 'key',
|
||||
'bar' => 'baz',
|
||||
'qux' => '安',
|
||||
'api_sig' => '7f21233b54edea994aa0f23cf55f18a2',
|
||||
], $builtParams);
|
||||
|
||||
// And the string version as well
|
||||
$this->assertEquals('api_key=key&bar=baz&qux=安&api_sig=7f21233b54edea994aa0f23cf55f18a2', $builtParamsAsString);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_returns_artist_info_if_artist_is_found_on_lastfm()
|
||||
{
|
||||
// Given an artist that exists on Last.fm
|
||||
/** @var Artist $artist */
|
||||
$artist = factory(Artist::class)->create(['name' => 'foo']);
|
||||
|
||||
// When I request the service for the artist's info
|
||||
$client = m::mock(Client::class, [
|
||||
'get' => new Response(200, [], file_get_contents(__DIR__.'../../blobs/lastfm/artist.xml')),
|
||||
]);
|
||||
|
||||
$api = new Lastfm(null, null, $client);
|
||||
$info = $api->getArtistInfo($artist->name);
|
||||
|
||||
// Then I see the info when the request is the successful
|
||||
$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'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_returns_false_if_artist_info_is_not_found_on_lastfm()
|
||||
{
|
||||
// Given an artist that doesn't exist on Last.fm
|
||||
/** @var Artist $artist */
|
||||
$artist = factory(Artist::class)->create();
|
||||
|
||||
// When I request the service for the artist info
|
||||
$client = m::mock(Client::class, [
|
||||
'get' => new Response(400, [], file_get_contents(__DIR__.'../../blobs/lastfm/artist-notfound.xml')),
|
||||
]);
|
||||
|
||||
$api = new Lastfm(null, null, $client);
|
||||
$result = $api->getArtistInfo($artist->name);
|
||||
|
||||
// Then I receive boolean false
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_returns_album_info_if_album_is_found_on_lastfm()
|
||||
{
|
||||
// Given an album that exists on Last.fm
|
||||
/** @var Album $album */
|
||||
$album = factory(Album::class)->create([
|
||||
'artist_id' => factory(Artist::class)->create(['name' => 'bar'])->id,
|
||||
'name' => 'foo',
|
||||
]);
|
||||
|
||||
// When I request the service for the album's info
|
||||
$client = m::mock(Client::class, [
|
||||
'get' => new Response(200, [], file_get_contents(__DIR__.'../../blobs/lastfm/album.xml')),
|
||||
]);
|
||||
|
||||
$api = new Lastfm(null, null, $client);
|
||||
$info = $api->getAlbumInfo($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);
|
||||
|
||||
// And the response is cached
|
||||
$this->assertNotNull(cache('fca889d13b3222589d7d020669cc5a38'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_returns_false_if_album_info_is_not_found_on_lastfm()
|
||||
{
|
||||
// Given there's an album which doesn't exist on Last.fm
|
||||
$album = factory(Album::class)->create();
|
||||
|
||||
// When I request the service for the album's info
|
||||
$client = m::mock(Client::class, [
|
||||
'get' => new Response(400, [], file_get_contents(__DIR__.'../../blobs/lastfm/album-notfound.xml')),
|
||||
]);
|
||||
|
||||
$api = new Lastfm(null, null, $client);
|
||||
$result = $api->getAlbumInfo($album->name, $album->artist->name);
|
||||
|
||||
// Then I receive a boolean false
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue