koel/tests/Feature/LastfmTest.php

133 lines
4.2 KiB
PHP
Raw Normal View History

<?php
2017-02-14 06:53:02 +00:00
namespace Tests\Feature;
2015-12-21 13:49:00 +00:00
use App\Events\SongLikeToggled;
2015-12-23 06:26:16 +00:00
use App\Events\SongStartedPlaying;
2015-12-20 12:30:28 +00:00
use App\Http\Controllers\API\LastfmController;
use App\Http\Requests\API\LastfmCallbackRequest;
2015-12-21 13:49:00 +00:00
use App\Listeners\LoveTrackOnLastfm;
2015-12-23 06:26:16 +00:00
use App\Listeners\UpdateLastfmNowPlaying;
2015-12-21 13:49:00 +00:00
use App\Models\Interaction;
use App\Models\Song;
2015-12-20 12:30:28 +00:00
use App\Models\User;
2018-08-18 13:19:40 +00:00
use App\Services\LastfmService;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
2015-12-20 12:30:28 +00:00
use Illuminate\Contracts\Auth\Guard;
2015-12-19 17:08:03 +00:00
use Illuminate\Foundation\Testing\WithoutMiddleware;
2015-12-20 12:17:35 +00:00
use Illuminate\Routing\Redirector;
use Mockery as m;
use Mockery\MockInterface;
2015-12-30 04:14:47 +00:00
use Tymon\JWTAuth\JWTAuth;
2017-08-05 16:56:11 +00:00
class LastfmTest extends TestCase
{
2017-08-05 16:56:11 +00:00
use WithoutMiddleware;
2015-12-20 12:17:35 +00:00
public function testGetSessionKey()
{
/** @var Client $client */
2015-12-20 12:17:35 +00:00
$client = m::mock(Client::class, [
2017-02-14 06:53:02 +00:00
'get' => new Response(200, [], file_get_contents(__DIR__.'../../blobs/lastfm/session-key.xml')),
2015-12-20 12:17:35 +00:00
]);
$this->assertEquals('foo', (new LastfmService($client))->getSessionKey('bar'));
2015-12-20 12:17:35 +00:00
}
public function testSetSessionKey()
2016-01-26 06:32:29 +00:00
{
$user = factory(User::class)->create();
2017-02-14 06:53:02 +00:00
$this->postAsUser('api/lastfm/session-key', ['key' => 'foo'], $user);
$user = User::find($user->id);
$this->assertEquals('foo', $user->lastfm_session_key);
2016-01-26 06:32:29 +00:00
}
2017-08-05 18:55:02 +00:00
/** @test */
public function user_can_connect_to_lastfm()
2015-12-20 12:17:35 +00:00
{
/** @var Redirector|MockInterface $redirector */
2015-12-20 12:17:35 +00:00
$redirector = m::mock(Redirector::class);
$redirector->shouldReceive('to')->once();
/** @var Guard|MockInterface $guard */
2015-12-20 12:17:35 +00:00
$guard = m::mock(Guard::class, ['user' => factory(User::class)->create()]);
2015-12-30 04:14:47 +00:00
$auth = m::mock(JWTAuth::class, [
'parseToken' => '',
'getToken' => '',
]);
2015-12-20 12:17:35 +00:00
(new LastfmController($guard))->connect($redirector, app(LastfmService::class), $auth);
2015-12-20 12:17:35 +00:00
}
public function testRetrieveAndStoreSessionKey()
2015-12-20 12:17:35 +00:00
{
/** @var LastfmCallbackRequest $request */
$request = m::mock(LastfmCallbackRequest::class);
2017-04-29 02:55:41 +00:00
$request->token = 'foo';
/** @var LastfmService $lastfm */
2018-08-18 13:19:40 +00:00
$lastfm = m::mock(LastfmService::class, ['getSessionKey' => 'bar']);
2015-12-20 12:17:35 +00:00
$user = factory(User::class)->create();
/** @var Guard $guard */
2015-12-20 12:17:35 +00:00
$guard = m::mock(Guard::class, ['user' => $user]);
(new LastfmController($guard))->callback($request, $lastfm);
$this->assertEquals('bar', $user->lastfm_session_key);
2015-12-20 12:17:35 +00:00
}
public function testDisconnectUser()
2015-12-20 12:17:35 +00:00
{
$user = factory(User::class)->create(['preferences' => ['lastfm_session_key' => 'bar']]);
2017-02-14 06:53:02 +00:00
$this->deleteAsUser('api/lastfm/disconnect', [], $user);
$user = User::find($user->id);
$this->assertNull($user->lastfm_session_key);
2015-12-21 13:49:00 +00:00
}
/**
* @throws Exception
*/
public function testLoveTrack()
2015-12-21 13:49:00 +00:00
{
$this->withoutEvents();
$this->createSampleMediaSet();
$user = factory(User::class)->create(['preferences' => ['lastfm_session_key' => 'bar']]);
$interaction = Interaction::create([
'user_id' => $user->id,
'song_id' => Song::first()->id,
]);
/** @var LastfmService|MockInterface $lastfm */
2018-08-18 13:19:40 +00:00
$lastfm = m::mock(LastfmService::class, ['enabled' => true]);
2015-12-21 13:49:00 +00:00
$lastfm->shouldReceive('toggleLoveTrack')
->once()
2015-12-23 06:26:16 +00:00
->with($interaction->song->title, $interaction->song->album->artist->name, 'bar', false);
2015-12-21 13:49:00 +00:00
(new LoveTrackOnLastfm($lastfm))->handle(new SongLikeToggled($interaction, $user));
2015-12-20 12:17:35 +00:00
}
2015-12-23 06:26:16 +00:00
/**
* @throws Exception
*/
public function testUpdateNowPlayingStatus()
2015-12-23 06:26:16 +00:00
{
$this->withoutEvents();
$this->createSampleMediaSet();
$user = factory(User::class)->create(['preferences' => ['lastfm_session_key' => 'bar']]);
$song = Song::first();
/** @var LastfmService|MockInterface $lastfm */
2018-08-18 13:19:40 +00:00
$lastfm = m::mock(LastfmService::class, ['enabled' => true]);
2015-12-23 06:26:16 +00:00
$lastfm->shouldReceive('updateNowPlaying')
->once()
2015-12-23 06:26:16 +00:00
->with($song->album->artist->name, $song->title, $song->album->name, $song->length, 'bar');
(new UpdateLastfmNowPlaying($lastfm))->handle(new SongStartedPlaying($song, $user));
}
}