koel/tests/Feature/LastfmTest.php

128 lines
3.9 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;
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;
2015-12-19 17:08:03 +00:00
use App\Services\Lastfm;
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:30:28 +00:00
use Illuminate\Http\Request;
2015-12-20 12:17:35 +00:00
use Illuminate\Routing\Redirector;
use Mockery as m;
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;
2017-08-05 18:55:02 +00:00
protected function tearDown()
{
m::close();
parent::tearDown();
}
2015-12-20 12:17:35 +00:00
public function testGetSessionKey()
{
$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
]);
$api = new Lastfm(null, null, $client);
$this->assertEquals('foo', $api->getSessionKey('bar'));
}
2017-08-05 18:55:02 +00:00
/** @test */
public function session_key_can_be_set()
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
{
$redirector = m::mock(Redirector::class);
$redirector->shouldReceive('to')->once();
$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
2015-12-30 04:14:47 +00:00
(new LastfmController($guard))->connect($redirector, new Lastfm(), $auth);
2015-12-20 12:17:35 +00:00
}
2017-08-05 18:55:02 +00:00
/** @test */
public function lastfm_session_key_can_be_retrieved_and_stored()
2015-12-20 12:17:35 +00:00
{
2017-04-29 02:55:41 +00:00
$request = m::mock(Request::class);
$request->token = 'foo';
2015-12-20 12:17:35 +00:00
$lastfm = m::mock(Lastfm::class, ['getSessionKey' => 'bar']);
$user = factory(User::class)->create();
$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
}
2017-08-05 18:55:02 +00:00
/** @test */
public function user_can_disconnect_from_lastfm()
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
}
2017-08-05 18:55:02 +00:00
/** @test */
public function user_can_love_a_track_on_lastfm()
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,
]);
$lastfm = m::mock(Lastfm::class, ['enabled' => true]);
$lastfm->shouldReceive('toggleLoveTrack')
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
2017-08-05 18:55:02 +00:00
/** @test */
public function user_now_playing_status_can_be_updated_to_lastfm()
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();
$lastfm = m::mock(Lastfm::class, ['enabled' => true]);
$lastfm->shouldReceive('updateNowPlaying')
->with($song->album->artist->name, $song->title, $song->album->name, $song->length, 'bar');
(new UpdateLastfmNowPlaying($lastfm))->handle(new SongStartedPlaying($song, $user));
}
}