Skip to content

Commit

Permalink
Add tests for iTunes service
Browse files Browse the repository at this point in the history
  • Loading branch information
phanan committed Apr 7, 2019
1 parent 42ab992 commit 57a5668
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @method object head($uri, ...$data)
* @method object delete($uri)
*/
abstract class ApiClient
abstract class AbstractApiClient
{
protected $responseFormat = 'json';
protected $client;
Expand Down
2 changes: 1 addition & 1 deletion app/Services/LastfmService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Exception;

class LastfmService extends ApiClient implements ApiConsumerInterface
class LastfmService extends AbstractApiClient implements ApiConsumerInterface
{
/**
* Specify the response format, since Last.fm only returns XML.
Expand Down
2 changes: 1 addition & 1 deletion app/Services/YouTubeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Models\Song;

class YouTubeService extends ApiClient implements ApiConsumerInterface
class YouTubeService extends AbstractApiClient implements ApiConsumerInterface
{
/**
* Determine if our application is using YouTube.
Expand Down
2 changes: 1 addition & 1 deletion app/Services/iTunesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Exception;

class iTunesService extends ApiClient implements ApiConsumerInterface
class iTunesService extends AbstractApiClient implements ApiConsumerInterface
{
/**
* Determines whether to use iTunes services.
Expand Down
2 changes: 1 addition & 1 deletion resources/assets
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Unit\Services;
namespace Tests\Unit\Models;

use App\Models\Playlist;
use Tests\TestCase;
Expand Down
5 changes: 0 additions & 5 deletions tests/Unit/Services/ApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ public function setUp(): void
{
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);
Expand Down
64 changes: 63 additions & 1 deletion tests/Unit/Services/iTunesServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -17,4 +24,59 @@ public function testConfiguration()
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);
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Stubs/ConcreteApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 57a5668

Please sign in to comment.