mirror of
https://github.com/koel/koel
synced 2024-12-30 06:23:05 +00:00
97 lines
3.4 KiB
PHP
97 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Services\ApiClients;
|
|
|
|
use App\Exceptions\SpotifyIntegrationDisabledException;
|
|
use App\Services\ApiClients\SpotifyClient;
|
|
use Illuminate\Cache\Repository as Cache;
|
|
use Mockery;
|
|
use Mockery\LegacyMockInterface;
|
|
use Mockery\MockInterface;
|
|
use SpotifyWebAPI\Session as SpotifySession;
|
|
use SpotifyWebAPI\SpotifyWebAPI;
|
|
use Tests\TestCase;
|
|
|
|
class SpotifyClientTest extends TestCase
|
|
{
|
|
private SpotifySession|LegacyMockInterface|MockInterface $session;
|
|
private SpotifyWebAPI|LegacyMockInterface|MockInterface $wrapped;
|
|
private Cache|LegacyMockInterface|MockInterface $cache;
|
|
|
|
private SpotifyClient $client;
|
|
|
|
public function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
config([
|
|
'koel.spotify.client_id' => 'fake-client-id',
|
|
'koel.spotify.client_secret' => 'fake-client-secret',
|
|
]);
|
|
|
|
$this->session = Mockery::mock(SpotifySession::class);
|
|
$this->wrapped = Mockery::mock(SpotifyWebAPI::class);
|
|
$this->cache = Mockery::mock(Cache::class);
|
|
}
|
|
|
|
public function testAccessTokenIsSetUponInitialization(): void
|
|
{
|
|
$this->mockSetAccessToken();
|
|
|
|
$this->client = new SpotifyClient($this->wrapped, $this->session, $this->cache);
|
|
self::addToAssertionCount(1);
|
|
}
|
|
|
|
public function testAccessTokenIsRetrievedFromCacheWhenApplicable(): void
|
|
{
|
|
$this->wrapped->shouldReceive('setOptions')->with(['return_assoc' => true]);
|
|
$this->cache->shouldReceive('get')->with('spotify.access_token')->andReturn('fake-access-token');
|
|
$this->session->shouldNotReceive('requestCredentialsToken');
|
|
$this->session->shouldNotReceive('getAccessToken');
|
|
$this->cache->shouldNotReceive('put');
|
|
$this->wrapped->shouldReceive('setAccessToken')->with('fake-access-token');
|
|
|
|
$this->client = new SpotifyClient($this->wrapped, $this->session, $this->cache);
|
|
}
|
|
|
|
public function testCallForwarding(): void
|
|
{
|
|
$this->mockSetAccessToken();
|
|
$this->wrapped->shouldReceive('search')->with('foo', 'track')->andReturn('bar');
|
|
|
|
$this->client = new SpotifyClient($this->wrapped, $this->session, $this->cache);
|
|
|
|
self::assertSame('bar', $this->client->search('foo', 'track'));
|
|
}
|
|
|
|
public function testCallForwardingThrowsIfIntegrationIsDisabled(): void
|
|
{
|
|
config([
|
|
'koel.spotify.client_id' => null,
|
|
'koel.spotify.client_secret' => null,
|
|
]);
|
|
|
|
self::expectException(SpotifyIntegrationDisabledException::class);
|
|
(new SpotifyClient($this->wrapped, $this->session, $this->cache))->search('foo', 'track');
|
|
}
|
|
|
|
private function mockSetAccessToken(): void
|
|
{
|
|
$this->wrapped->shouldReceive('setOptions')->with(['return_assoc' => true]);
|
|
$this->cache->shouldReceive('get')->with('spotify.access_token')->andReturnNull();
|
|
$this->session->shouldReceive('requestCredentialsToken');
|
|
$this->session->shouldReceive('getAccessToken')->andReturn('fake-access-token');
|
|
$this->cache->shouldReceive('put')->with('spotify.access_token', 'fake-access-token', 3_540);
|
|
$this->wrapped->shouldReceive('setAccessToken')->with('fake-access-token');
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
config([
|
|
'koel.spotify.client_id' => null,
|
|
'koel.spotify.client_secret' => null,
|
|
]);
|
|
|
|
parent::tearDown();
|
|
}
|
|
}
|