koel/tests/Unit/Services/ITunesServiceTest.php

88 lines
2.5 KiB
PHP
Raw Normal View History

2018-08-19 14:40:25 +00:00
<?php
namespace Tests\Unit\Services;
2022-08-08 16:00:59 +00:00
use App\Services\ApiClients\ITunesClient;
2020-12-22 20:11:22 +00:00
use App\Services\ITunesService;
2022-08-08 16:00:59 +00:00
use Illuminate\Cache\Repository as Cache;
2020-12-22 20:11:22 +00:00
use Mockery;
2018-08-19 14:40:25 +00:00
use Tests\TestCase;
2020-12-22 20:11:22 +00:00
class ITunesServiceTest extends TestCase
2018-08-19 14:40:25 +00:00
{
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]);
2020-12-22 20:11:22 +00:00
/** @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
2020-12-22 20:11:22 +00:00
/** @return array<mixed> */
2019-04-07 21:09:25 +00:00
public function provideGetTrackUrlData(): array
{
return [
[
'Foo',
'Bar',
'Baz',
2022-08-08 16:00:59 +00:00
'Foo Bar Baz',
2019-04-07 21:09:25 +00:00
'https://itunes.apple.com/bar',
'https://itunes.apple.com/bar?at=foo',
'2ce68c30758ed9496c72c36ff49c50b2',
], [
'Foo',
'',
'Baz',
2022-08-08 16:00:59 +00:00
'Foo Baz',
2019-04-07 21:09:25 +00:00
'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,
2022-08-08 16:00:59 +00:00
string $constructedTerm,
2019-04-07 21:09:25 +00:00
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']);
2020-12-22 20:11:22 +00:00
$cache = Mockery::mock(Cache::class);
2022-08-08 16:00:59 +00:00
$client = Mockery::mock(ITunesClient::class);
$client->shouldReceive('get')
->with('/', [
'query' => [
'term' => $constructedTerm,
'media' => 'music',
'entity' => 'song',
'limit' => 1,
],
2022-08-08 16:00:59 +00:00
])
->andReturn(json_decode(json_encode([
'resultCount' => 1,
'results' => [['trackViewUrl' => $trackViewUrl]],
])));
2019-04-07 21:09:25 +00:00
2022-08-08 16:00:59 +00:00
$service = new ITunesService($client, $cache);
2019-04-07 21:09:25 +00:00
$cache
->shouldReceive('remember')
->with($cacheKey, 10_080, Mockery::on(static function (callable $generator) use ($affiliateUrl): bool {
self::assertSame($generator(), $affiliateUrl);
return true;
2019-04-07 21:09:25 +00:00
}));
$service->getTrackUrl($term, $album, $artist);
}
2018-08-19 14:40:25 +00:00
}