2017-12-09 22:39:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services;
|
|
|
|
|
2018-09-04 05:34:02 +00:00
|
|
|
use App\Models\User;
|
2018-08-18 13:19:40 +00:00
|
|
|
use App\Services\LastfmService;
|
2018-09-04 05:34:02 +00:00
|
|
|
use App\Services\UserPreferenceService;
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use Illuminate\Contracts\Cache\Repository as Cache;
|
|
|
|
use Illuminate\Log\Logger;
|
2018-08-19 11:08:16 +00:00
|
|
|
use Mockery;
|
2018-09-03 12:41:49 +00:00
|
|
|
use Mockery\Mock;
|
2018-09-04 05:34:02 +00:00
|
|
|
use Mockery\MockInterface;
|
2017-12-09 22:39:34 +00:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
2018-08-19 11:08:16 +00:00
|
|
|
class LastfmServiceTest extends TestCase
|
2017-12-09 22:39:34 +00:00
|
|
|
{
|
2018-09-04 05:34:02 +00:00
|
|
|
/** @var Client */
|
|
|
|
private $client;
|
|
|
|
|
|
|
|
/** @var Cache */
|
|
|
|
private $cache;
|
|
|
|
|
|
|
|
/** @var Logger */
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
/** @var UserPreferenceService|MockInterface */
|
|
|
|
private $userPreferenceService;
|
|
|
|
|
|
|
|
/** @var LastfmService */
|
|
|
|
private $lastfmService;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->client = Mockery::mock(Client::class);
|
|
|
|
$this->cache = Mockery::mock(Cache::class);
|
|
|
|
$this->logger = Mockery::mock(Logger::class);
|
|
|
|
$this->userPreferenceService = Mockery::mock(UserPreferenceService::class);
|
|
|
|
$this->lastfmService = new LastfmService(
|
|
|
|
$this->client,
|
|
|
|
$this->cache,
|
|
|
|
$this->logger,
|
|
|
|
$this->userPreferenceService
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-03 12:41:49 +00:00
|
|
|
public function testBuildAuthCallParams(): void
|
2017-12-09 22:39:34 +00:00
|
|
|
{
|
2018-09-03 12:41:49 +00:00
|
|
|
/** @var Mock|LastfmService $lastfm */
|
2018-08-19 11:08:16 +00:00
|
|
|
$lastfm = Mockery::mock(LastfmService::class)->makePartial();
|
|
|
|
$lastfm->shouldReceive('getKey')->andReturn('key');
|
|
|
|
$lastfm->shouldReceive('getSecret')->andReturn('secret');
|
|
|
|
|
2017-12-09 22:39:34 +00:00
|
|
|
$params = [
|
|
|
|
'qux' => '安',
|
|
|
|
'bar' => 'baz',
|
|
|
|
];
|
|
|
|
|
|
|
|
// When I build Last.fm-compatible API parameters using the raw parameters
|
2018-08-19 11:08:16 +00:00
|
|
|
$builtParams = $lastfm->buildAuthCallParams($params);
|
|
|
|
$builtParamsAsString = $lastfm->buildAuthCallParams($params, true);
|
2017-12-09 22:39:34 +00:00
|
|
|
|
|
|
|
// Then I receive the Last.fm-compatible API parameters
|
|
|
|
$this->assertEquals([
|
|
|
|
'api_key' => 'key',
|
|
|
|
'bar' => 'baz',
|
|
|
|
'qux' => '安',
|
|
|
|
'api_sig' => '7f21233b54edea994aa0f23cf55f18a2',
|
|
|
|
], $builtParams);
|
|
|
|
|
|
|
|
// And the string version as well
|
|
|
|
$this->assertEquals(
|
|
|
|
'api_key=key&bar=baz&qux=安&api_sig=7f21233b54edea994aa0f23cf55f18a2',
|
|
|
|
$builtParamsAsString
|
|
|
|
);
|
|
|
|
}
|
2018-09-04 05:34:02 +00:00
|
|
|
|
|
|
|
public function testGetUserSessionKey(): void
|
|
|
|
{
|
|
|
|
/** @var User $user */
|
|
|
|
$user = Mockery::mock(User::class);
|
|
|
|
|
|
|
|
$this->userPreferenceService->shouldReceive('get')
|
|
|
|
->with($user, 'lastfm_session_key')
|
|
|
|
->andReturn('foo');
|
|
|
|
|
|
|
|
self::assertSame('foo', $this->lastfmService->getUserSessionKey($user));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetUserSessionKey(): void
|
|
|
|
{
|
|
|
|
/** @var User $user */
|
|
|
|
$user = Mockery::mock(User::class);
|
|
|
|
|
|
|
|
$this->userPreferenceService->shouldReceive('set')
|
|
|
|
->with($user, 'lastfm_session_key', 'foo');
|
|
|
|
|
|
|
|
$this->lastfmService->setUserSessionKey($user, 'foo');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteUserSessionKey(): void
|
|
|
|
{
|
|
|
|
/** @var User $user */
|
|
|
|
$user = Mockery::mock(User::class);
|
|
|
|
|
|
|
|
$this->userPreferenceService->shouldReceive('delete')
|
|
|
|
->with($user, 'lastfm_session_key');
|
|
|
|
|
|
|
|
$this->lastfmService->deleteUserSessionKey($user);
|
|
|
|
}
|
2017-12-09 22:39:34 +00:00
|
|
|
}
|