Add tests for iTunes service

This commit is contained in:
Phan An 2019-04-07 23:09:25 +02:00
parent 42ab992e4e
commit 57a566834b
9 changed files with 71 additions and 14 deletions

View file

@ -17,7 +17,7 @@ use SimpleXMLElement;
* @method object head($uri, ...$data)
* @method object delete($uri)
*/
abstract class ApiClient
abstract class AbstractApiClient
{
protected $responseFormat = 'json';
protected $client;

View file

@ -4,7 +4,7 @@ namespace App\Services;
use Exception;
class LastfmService extends ApiClient implements ApiConsumerInterface
class LastfmService extends AbstractApiClient implements ApiConsumerInterface
{
/**
* Specify the response format, since Last.fm only returns XML.

View file

@ -4,7 +4,7 @@ namespace App\Services;
use App\Models\Song;
class YouTubeService extends ApiClient implements ApiConsumerInterface
class YouTubeService extends AbstractApiClient implements ApiConsumerInterface
{
/**
* Determine if our application is using YouTube.

View file

@ -4,7 +4,7 @@ namespace App\Services;
use Exception;
class iTunesService extends ApiClient implements ApiConsumerInterface
class iTunesService extends AbstractApiClient implements ApiConsumerInterface
{
/**
* Determines whether to use iTunes services.

@ -1 +1 @@
Subproject commit 9d11914ec5d61eab959ddd34a1b371391187f701
Subproject commit f04915e0a25e50224a488e859a07183659839ab4

View file

@ -1,6 +1,6 @@
<?php
namespace Tests\Unit\Services;
namespace Tests\Unit\Models;
use App\Models\Playlist;
use Tests\TestCase;

View file

@ -29,11 +29,6 @@ class ApiClientTest extends TestCase
{
parent::setUp();
/*
* @var Client client
* @var Cache cache
* @var Logger logger
*/
$this->client = Mockery::mock(Client::class);
$this->cache = Mockery::mock(Cache::class);
$this->logger = Mockery::mock(Logger::class);

View file

@ -3,11 +3,18 @@
namespace Tests\Unit\Services;
use App\Services\iTunesService;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Log\Logger;
use Mockery as m;
use Tests\TestCase;
class iTunesServiceTest extends TestCase
{
public function testConfiguration()
public function testConfiguration(): void
{
config(['koel.itunes.enabled' => true]);
/** @var iTunesService $iTunes */
@ -17,4 +24,59 @@ class iTunesServiceTest extends TestCase
config(['koel.itunes.enabled' => false]);
$this->assertFalse($iTunes->used());
}
public function provideGetTrackUrlData(): array
{
return [
[
'Foo',
'Bar',
'Baz',
'https://itunes.apple.com/bar',
'https://itunes.apple.com/bar?at=foo',
'2ce68c30758ed9496c72c36ff49c50b2',
], [
'Foo',
'',
'Baz',
'https://itunes.apple.com/bar?qux=qux',
'https://itunes.apple.com/bar?qux=qux&at=foo',
'cda57916eb80c2ee79b16e218bdb70d2',
],
];
}
/** @dataProvider provideGetTrackUrlData */
public function testGetTrackUrl(
string $term,
string $album,
string $artist,
string $trackViewUrl,
string $affiliateUrl,
string $cacheKey
): void
{
config(['koel.itunes.affiliate_id' => 'foo']);
$mock = new MockHandler([
new Response(200, [], json_encode([
'resultCount' => 1,
'results' => [['trackViewUrl' => $trackViewUrl]]
])),
]);
$client = new Client(['handler' => HandlerStack::create($mock)]);
$cache = m::mock(Cache::class);
$logger = m::mock(Logger::class);
$service = new iTunesService($client, $cache, $logger);
$cache
->shouldReceive('remember')
->with($cacheKey, 10080, m::on(static function(callable $generator) use ($affiliateUrl): bool {
return $generator() === $affiliateUrl;
}));
$service->getTrackUrl($term, $album, $artist);
}
}

View file

@ -2,9 +2,9 @@
namespace Tests\Unit\Stubs;
use App\Services\ApiClient;
use App\Services\AbstractApiClient;
class ConcreteApiClient extends ApiClient
class ConcreteApiClient extends AbstractApiClient
{
public function getKey(): string
{