Refactor tests

This commit is contained in:
Phan An 2017-08-05 17:56:11 +01:00
parent 9cd1e86533
commit a8f98b2377
26 changed files with 228 additions and 224 deletions

View file

@ -7,7 +7,11 @@ use App\Traits\SupportsDeleteWhereIDsNotIn;
use AWS;
use Aws\AwsClient;
use Cache;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Lastfm;
use YouTube;
@ -55,16 +59,31 @@ class Song extends Model
*/
public $incrementing = false;
/**
* A song belongs to an artist.
*
* @return BelongsTo
*/
public function artist()
{
return $this->belongsTo(Artist::class);
}
/**
* A song belongs to a album.
*
* @return BelongsTo
*/
public function album()
{
return $this->belongsTo(Album::class);
}
/**
* A song can belong to many playlists.
*
* @return BelongsToMany
*/
public function playlists()
{
return $this->belongsToMany(Playlist::class);
@ -130,7 +149,7 @@ class Song extends Model
/*
* A collection of the updated songs.
*
* @var \Illuminate\Support\Collection
* @var Collection
*/
$updatedSongs = collect();
@ -222,10 +241,10 @@ class Song extends Model
/**
* Scope a query to only include songs in a given directory.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $path Full path of the directory
* @param Builder $query
* @param string $path Full path of the directory
*
* @return \Illuminate\Database\Eloquent\Builder
* @return Builder
*/
public function scopeInDirectory($query, $path)
{
@ -241,7 +260,7 @@ class Song extends Model
* @param User $user
* @param bool $toArray
*
* @return \Illuminate\Database\Eloquent\Collection|array
* @return Collection|array
*/
public static function getFavorites(User $user, $toArray = false)
{
@ -302,7 +321,7 @@ class Song extends Model
* Sometimes the tags extracted from getID3 are HTML entity encoded.
* This makes sure they are always sane.
*
* @param $value
* @param string $value
*/
public function setTitleAttribute($value)
{
@ -313,7 +332,7 @@ class Song extends Model
* Some songs don't have a title.
* Fall back to the file name (without extension) for such.
*
* @param $value
* @param string $value
*
* @return string
*/
@ -353,6 +372,11 @@ class Song extends Model
return compact('bucket', 'key');
}
/**
* Return the ID of the song when it's converted to string.
*
* @return string
*/
public function __toString()
{
return $this->id;

View file

@ -9,13 +9,17 @@
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Feature Tests">
<testsuite name="feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Unit Tests">
<testsuite name="unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="integration">
<directory suffix="Test.php">./tests/Integration</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">

View file

@ -8,13 +8,9 @@ use App\Models\Playlist;
use App\Models\Song;
use App\Models\User;
use Download;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\BrowserKitTestCase;
class DownloadTest extends BrowserKitTestCase
class DownloadTest extends TestCase
{
use DatabaseTransactions;
public function setUp()
{
parent::setUp();

View file

@ -5,13 +5,11 @@ namespace Tests\Feature;
use App\Events\SongLikeToggled;
use App\Models\Song;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Tests\BrowserKitTestCase;
class InteractionTest extends BrowserKitTestCase
class InteractionTest extends TestCase
{
use DatabaseTransactions, WithoutMiddleware;
use WithoutMiddleware;
public function setUp()
{

View file

@ -14,17 +14,15 @@ use App\Services\Lastfm;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Mockery as m;
use Tests\BrowserKitTestCase;
use Tymon\JWTAuth\JWTAuth;
class LastfmTest extends BrowserKitTestCase
class LastfmTest extends TestCase
{
use DatabaseTransactions, WithoutMiddleware;
use WithoutMiddleware;
public function testGetSessionKey()
{

View file

@ -9,14 +9,12 @@ use App\Models\Artist;
use App\Models\File;
use App\Models\Song;
use App\Services\Media;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Mockery as m;
use Tests\BrowserKitTestCase;
class MediaTest extends BrowserKitTestCase
class MediaTest extends TestCase
{
use DatabaseTransactions, WithoutMiddleware;
use WithoutMiddleware;
public function testSync()
{

View file

@ -3,13 +3,12 @@
namespace Tests\Feature\ObjectStorage;
use App\Events\LibraryChanged;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Tests\BrowserKitTestCase;
use Tests\Feature\TestCase;
class S3Test extends BrowserKitTestCase
class S3Test extends TestCase
{
use DatabaseTransactions, WithoutMiddleware;
use WithoutMiddleware;
public function setUp()
{

View file

@ -5,13 +5,9 @@ namespace Tests\Feature;
use App\Models\Playlist;
use App\Models\Song;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\BrowserKitTestCase;
class PlaylistTest extends BrowserKitTestCase
class PlaylistTest extends TestCase
{
use DatabaseTransactions;
public function setUp()
{
parent::setUp();

View file

@ -3,13 +3,11 @@
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Tests\BrowserKitTestCase;
class ProfileTest extends BrowserKitTestCase
class ProfileTest extends TestCase
{
use WithoutMiddleware, DatabaseTransactions;
use WithoutMiddleware;
public function testUpdate()
{

View file

@ -5,14 +5,12 @@ namespace Tests\Feature;
use App\Services\RESTfulService;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Mockery as m;
use Tests\BrowserKitTestCase;
class RESTfulAPIServiceTest extends BrowserKitTestCase
class RESTfulAPIServiceTest extends TestCase
{
use DatabaseTransactions, WithoutMiddleware;
use WithoutMiddleware;
public function testUrlConstruction()
{

View file

@ -4,14 +4,12 @@ namespace Tests\Feature;
use App\Models\Song;
use App\Services\Lastfm;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Mockery as m;
use Tests\BrowserKitTestCase;
class ScrobbleTest extends BrowserKitTestCase
class ScrobbleTest extends TestCase
{
use DatabaseTransactions, WithoutMiddleware;
use WithoutMiddleware;
public function testScrobble()
{

View file

@ -4,14 +4,12 @@ namespace Tests\Feature;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Media;
use Tests\BrowserKitTestCase;
class SettingTest extends BrowserKitTestCase
class SettingTest extends TestCase
{
use DatabaseTransactions, WithoutMiddleware;
use WithoutMiddleware;
public function testSetSingleKeyValue()
{

View file

@ -7,17 +7,11 @@ use App\Models\Album;
use App\Models\Artist;
use App\Models\Song;
use App\Models\User;
use Aws\AwsClient;
use Cache;
use GuzzleHttp\Psr7\Request;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Mockery as m;
use Tests\BrowserKitTestCase;
class SongTest extends BrowserKitTestCase
class SongTest extends TestCase
{
use DatabaseTransactions, WithoutMiddleware;
use WithoutMiddleware;
public function setUp()
{

View file

@ -1,17 +1,19 @@
<?php
namespace Tests;
namespace Tests\Feature;
use App\Models\Album;
use App\Models\Artist;
use App\Models\Song;
use App\Models\User;
use JWTAuth;
use Laravel\BrowserKitTesting\TestCase as BaseBrowserKitTestCase;
use Laravel\BrowserKitTesting\DatabaseTransactions;
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;
use Tests\CreatesApplication;
abstract class BrowserKitTestCase extends BaseBrowserKitTestCase
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use CreatesApplication, DatabaseTransactions;
public function setUp()
{

View file

@ -3,13 +3,9 @@
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\BrowserKitTestCase;
class UserTest extends BrowserKitTestCase
class UserTest extends TestCase
{
use DatabaseTransactions;
public function testCreateUser()
{
// Non-admins can't do shit

View file

@ -6,15 +6,13 @@ use App\Models\Song;
use App\Services\YouTube;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Mockery as m;
use Tests\BrowserKitTestCase;
use YouTube as YouTubeFacade;
class YouTubeTest extends BrowserKitTestCase
class YouTubeTest extends TestCase
{
use DatabaseTransactions, WithoutMiddleware;
use WithoutMiddleware;
public function testSearch()
{

View file

@ -0,0 +1,29 @@
<?php
namespace Tests\Integration;
use App\Models\Album;
use Lastfm;
use Tests\TestCase;
class AlbumTest extends TestCase
{
/** @test */
public function extra_info_can_be_retrieved_for_an_album()
{
// Given there's an album
/** @var Album $album */
$album = factory(Album::class)->create();
// When I get the extra info for the album
Lastfm::shouldReceive('getAlbumInfo')
->once()
->with($album->name, $album->artist->name)
->andReturn(['foo' => 'bar']);
$info = $album->getInfo();
// Then I receive the extra info
$this->assertEquals(['foo' => 'bar'], $info);
}
}

View file

@ -0,0 +1,29 @@
<?php
namespace Tests\Integration;
use App\Models\Artist;
use Lastfm;
use Tests\TestCase;
class ArtistTest extends TestCase
{
/** @test */
public function extra_info_can_be_retrieved_for_an_artist()
{
// Given there's an artist
/** @var Artist $artist */
$artist = factory(Artist::class)->create();
// When I get the extra info
Lastfm::shouldReceive('getArtistInfo')
->once()
->with($artist->name)
->andReturn(['foo' => 'bar']);
$info = $artist->getInfo();
// Then I receive the extra info
$this->assertEquals(['foo' => 'bar'], $info);
}
}

View file

@ -0,0 +1,101 @@
<?php
namespace Tests\Integration;
use App\Models\Song;
use App\Models\User;
use Aws\AwsClient;
use Cache;
use Lastfm;
use Mockery as m;
use Tests\TestCase;
use YouTube;
class SongTest extends TestCase
{
/** @test */
public function it_returns_object_storage_public_url()
{
// Given there's a song hosted on Amazon S3
/** @var Song $song */
$song = factory(Song::class)->create(['path' => 's3://foo/bar']);
$mockedURL = 'http://aws.com/foo/bar';
// When I get the song's object storage public URL
$client = m::mock(AwsClient::class, [
'getCommand' => null,
'createPresignedRequest' => m::mock(Request::class, [
'getUri' => $mockedURL,
]),
]);
Cache::shouldReceive('get')->once()->with("OSUrl/{$song->id}");
Cache::shouldReceive('put')->once()->with("OSUrl/{$song->id}", $mockedURL, 60);
$url = $song->getObjectStoragePublicUrl($client);
// Then I should receive the correct S3 public URL
$this->assertEquals($mockedURL, $url);
}
/** @test */
public function it_scrobbles_if_the_user_is_connected_to_lastfm()
{
// Given there's a song
/** @var Song $song */
$song = factory(Song::class)->create();
// And a user who's connected to lastfm
/** @var User $user */
$user = factory(User::class)->create();
$user->setPreference('lastfm_session_key', 'foo');
// When I call the scrobble method
$time = time();
Lastfm::shouldReceive('scrobble')
->once()
->with($song->artist->name, $song->title, $time, $song->album->name, 'foo');
$song->scrobble($user, $time);
// Then I see the song is scrobbled
}
/** @test */
public function it_does_not_scrobble_if_the_user_is_not_connected_to_lastfm()
{
// Given there's a song
/** @var Song $song */
$song = factory(Song::class)->create();
// And a user who is not connected to lastfm
/** @var User $user */
$user = factory(User::class)->create();
$user->setPreference('lastfm_session_key', false);
// When I call the scrobble method
Lastfm::shouldNotReceive('scrobble');
$song->scrobble($user, time());
// The the song shouldn't be scrobbled
}
/** @test */
public function it_gets_related_youtube_videos()
{
// Given there's a song
/** @var Song $song */
$song = factory(Song::class)->create();
// When I get is related YouTube videos
YouTube::shouldReceive('searchVideosRelatedToSong')
->once()
->with($song, 'foo')
->andReturn(['bar' => 'baz']);
$videos = $song->getRelatedYouTubeVideos('foo');
// Then I see the related YouTube videos returned
$this->assertEquals(['bar' => 'baz'], $videos);
}
}

View file

@ -2,11 +2,12 @@
namespace Tests;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use DatabaseTransactions, CreatesApplication;
public function setUp()
{

View file

@ -2,17 +2,13 @@
namespace Tests\Unit;
use App\Facades\Lastfm;
use App\Models\Album;
use App\Models\Artist;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use org\bovigo\vfs\vfsStream;
use Tests\TestCase;
class AlbumTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_can_be_instantiated()
{
@ -119,23 +115,4 @@ class AlbumTest extends TestCase
// And the album's cover attribute is updated
$this->assertEquals('http://localhost/public/img/covers/bar.jpg', Album::find($album->id)->cover);
}
/** @test */
public function extra_info_can_be_retrieved_for_an_album()
{
// Given there's an album
/** @var Album $album */
$album = factory(Album::class)->create();
// When I get the extra info for the album
Lastfm::shouldReceive('getAlbumInfo')
->once()
->with($album->name, $album->artist->name)
->andReturn(['foo' => 'bar']);
$info = $album->getInfo();
// Then I receive the extra info
$this->assertEquals(['foo' => 'bar'], $info);
}
}

View file

@ -4,7 +4,6 @@ namespace Tests\Unit;
use App\Models\Artist;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Lastfm;
use org\bovigo\vfs\vfsStream;
use Tests\TestCase;
@ -91,25 +90,6 @@ class ArtistTest extends TestCase
$this->assertEquals('http://localhost/public/img/artists/foo.jpg', Artist::find($artist->id)->image);
}
/** @test */
public function extra_info_can_be_retrieved_for_an_artist()
{
// Given there's an artist
/** @var Artist $artist */
$artist = factory(Artist::class)->create();
// When I get the extra info
Lastfm::shouldReceive('getArtistInfo')
->once()
->with($artist->name)
->andReturn(['foo' => 'bar']);
$info = $artist->getInfo();
// Then I receive the extra info
$this->assertEquals(['foo' => 'bar'], $info);
}
/** @test */
public function artists_with_name_in_utf16_encoding_are_retrieved_correctly()
{
@ -122,6 +102,5 @@ class ArtistTest extends TestCase
// Then I receive the artist
$this->assertEquals($artist->id, $retrieved->id);
}
}

View file

@ -9,12 +9,9 @@ use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class LastfmTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_builds_lastfm_compatible_api_parameters()
{

View file

@ -4,14 +4,11 @@ namespace Tests\Unit;
use App\Models\Song;
use Cache;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use MediaCache;
use Tests\TestCase;
class MediaCacheTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_queries_fresh_data_from_database_if_a_cache_is_not_found()
{

View file

@ -4,13 +4,9 @@ namespace Tests\Unit;
use App\Models\Setting;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class SettingTest extends TestCase
{
use DatabaseMigrations, DatabaseTransactions;
/** @test */
public function it_can_be_instantiated()
{

View file

@ -2,70 +2,17 @@
namespace Tests\Unit;
use App\Facades\YouTube;
use App\Http\Requests\API\ObjectStorage\S3\Request;
use App\Models\Song;
use App\Models\User;
use Aws\AwsClient;
use Cache;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Lastfm;
use Mockery as m;
use Tests\TestCase;
class SongTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_can_be_instantiated()
{
$this->assertInstanceOf(Song::class, new Song());
}
/** @test */
public function it_scrobbles_if_the_user_is_connected_to_lastfm()
{
// Given there's a song
/** @var Song $song */
$song = factory(Song::class)->create();
// And a user who's connected to lastfm
/** @var User $user */
$user = factory(User::class)->create();
$user->setPreference('lastfm_session_key', 'foo');
// When I call the scrobble method
$time = time();
Lastfm::shouldReceive('scrobble')
->once()
->with($song->artist->name, $song->title, $time, $song->album->name, 'foo');
$song->scrobble($user, $time);
// Then I see the song is scrobbled
}
/** @test */
public function it_does_not_scrobble_if_the_user_is_not_connected_to_lastfm()
{
// Given there's a song
/** @var Song $song */
$song = factory(Song::class)->create();
// And a user who is not connected to lastfm
/** @var User $user */
$user = factory(User::class)->create();
$user->setPreference('lastfm_session_key', false);
// When I call the scrobble method
Lastfm::shouldNotReceive('scrobble');
$song->scrobble($user, time());
// The the song shouldn't be scrobbled
}
/** @test */
public function it_can_be_retrieved_using_its_path()
{
@ -79,50 +26,6 @@ class SongTest extends TestCase
// Then the song is retrieved
$this->assertEquals($song->id, $retrieved->id);
}
/** @test */
public function it_returns_object_storage_public_url()
{
// Given there's a song hosted on Amazon S3
/** @var Song $song */
$song = factory(Song::class)->create(['path' => 's3://foo/bar']);
$mockedURL = 'http://aws.com/foo/bar';
// When I get the song's object storage public URL
$client = m::mock(AwsClient::class, [
'getCommand' => null,
'createPresignedRequest' => m::mock(Request::class, [
'getUri' => $mockedURL,
]),
]);
Cache::shouldReceive('get')->once()->with("OSUrl/{$song->id}");
Cache::shouldReceive('put')->once()->with("OSUrl/{$song->id}", $mockedURL, 60);
$url = $song->getObjectStoragePublicUrl($client);
// Then I should receive the correct S3 public URL
$this->assertEquals($mockedURL, $url);
}
/** @test */
public function it_gets_related_youtube_videos()
{
// Given there's a song
/** @var Song $song */
$song = factory(Song::class)->create();
// When I get is related YouTube videos
YouTube::shouldReceive('searchVideosRelatedToSong')
->once()
->with($song, 'foo')
->andReturn(['bar' => 'baz']);
$videos = $song->getRelatedYouTubeVideos('foo');
// Then I see the related YouTube videos returned
$this->assertEquals(['bar' => 'baz'], $videos);
}
/** @test */
public function its_lyrics_has_all_new_line_characters_replace_by_br_tags()
{