koel/tests/Unit/Services/iTunesServiceTest.php

82 lines
2.3 KiB
PHP
Raw Normal View History

2018-08-19 14:40:25 +00:00
<?php
namespace Tests\Unit\Services;
use App\Services\iTunesService;
2019-04-07 21:09:25 +00:00
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;
2018-08-19 14:40:25 +00:00
use Tests\TestCase;
class iTunesServiceTest extends TestCase
{
2019-04-07 21:09:25 +00:00
public function testConfiguration(): void
2018-08-19 14:40:25 +00:00
{
config(['koel.itunes.enabled' => true]);
/** @var iTunesService $iTunes */
$iTunes = app()->make(iTunesService::class);
2020-09-06 18:21:39 +00:00
self::assertTrue($iTunes->used());
2018-08-19 14:40:25 +00:00
config(['koel.itunes.enabled' => false]);
2020-09-06 18:21:39 +00:00
self::assertFalse($iTunes->used());
2018-08-19 14:40:25 +00:00
}
2019-04-07 21:09:25 +00:00
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
2019-04-07 21:09:51 +00:00
): void {
2019-04-07 21:09:25 +00:00
config(['koel.itunes.affiliate_id' => 'foo']);
$mock = new MockHandler([
new Response(200, [], json_encode([
'resultCount' => 1,
2019-04-07 21:09:51 +00:00
'results' => [['trackViewUrl' => $trackViewUrl]],
2019-04-07 21:09:25 +00:00
])),
]);
$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')
2019-04-07 21:09:51 +00:00
->with($cacheKey, 10080, m::on(static function (callable $generator) use ($affiliateUrl): bool {
2019-04-07 21:09:25 +00:00
return $generator() === $affiliateUrl;
}));
$service->getTrackUrl($term, $album, $artist);
}
2018-08-19 14:40:25 +00:00
}