Skip to content

Commit

Permalink
Make tests green
Browse files Browse the repository at this point in the history
  • Loading branch information
phanan committed Aug 5, 2017
1 parent 4e27363 commit f344b4c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 66 deletions.
33 changes: 14 additions & 19 deletions app/Models/Song.php
Expand Up @@ -281,28 +281,23 @@ public static function getFavorites(User $user, $toArray = false)
*/
public function getObjectStoragePublicUrl(AwsClient $s3 = null)
{
// If we have a cached version, just return it.
if ($cached = Cache::get("OSUrl/{$this->id}")) {
return $cached;
}

// Otherwise, we query S3 for the presigned request.
if (!$s3) {
$s3 = AWS::createClient('s3');
}
return Cache::remember("OSUrl/{$this->id}", 60, function () use ($s3) {
if (!$s3) {
$s3 = AWS::createClient('s3');
}

$cmd = $s3->getCommand('GetObject', [
'Bucket' => $this->s3_params['bucket'],
'Key' => $this->s3_params['key'],
]);
$cmd = $s3->getCommand('GetObject', [
'Bucket' => $this->s3_params['bucket'],
'Key' => $this->s3_params['key'],
]);

// Here we specify that the request is valid for 1 hour.
// We'll also cache the public URL for future reuse.
$request = $s3->createPresignedRequest($cmd, '+1 hour');
$url = (string) $request->getUri();
Cache::put("OSUrl/{$this->id}", $url, 60);
// Here we specify that the request is valid for 1 hour.
// We'll also cache the public URL for future reuse.
$request = $s3->createPresignedRequest($cmd, '+1 hour');
$url = (string) $request->getUri();

return $url;
return $url;
});
}

/**
Expand Down
47 changes: 23 additions & 24 deletions app/Services/iTunes.php
Expand Up @@ -2,6 +2,7 @@

namespace App\Services;

use Cache;
use Exception;
use GuzzleHttp\Client;
use Log;
Expand Down Expand Up @@ -50,35 +51,33 @@ public function used()
public function getTrackUrl($term, $album = '', $artist = '')
{
try {
$cacheKey = md5("itunes_track_url_{$term}{$album}{$artist}");
if (!$trackUrl = cache($cacheKey)) {
$params = [
'term' => $term.($album ? ' '.$album : '').($artist ? ' '.$artist : ''),
'media' => 'music',
'entity' => 'song',
'limit' => 1,
];
return Cache::remember(md5("itunes_track_url_{$term}{$album}{$artist}"), 24 * 60 * 7,
function () use ($term, $album, $artist) {
$params = [
'term' => $term.($album ? " $album" : '').($artist ? " $artist" : ''),
'media' => 'music',
'entity' => 'song',
'limit' => 1,
];

$response = (string) $this->client->get($this->endPoint, ['query' => $params])->getBody();
$response = json_decode($response);
$response = (string) $this->client->get($this->endPoint, ['query' => $params])->getBody();
$response = json_decode($response);

if (!$response->resultCount) {
return false;
}

$trackUrl = $response->results[0]->trackViewUrl;
if (!$response->resultCount) {
return false;
}

// Returns a string if the URL has parameters or NULL if not
if (parse_url($trackUrl, PHP_URL_QUERY)) {
$trackUrl .= '&at='.config('koel.itunes.affiliate_id');
} else {
$trackUrl .= '?at='.config('koel.itunes.affiliate_id');
}
$trackUrl = $response->results[0]->trackViewUrl;

cache([$cacheKey => $trackUrl], 24 * 60 * 7);
}
if (parse_url($trackUrl, PHP_URL_QUERY)) {
$trackUrl .= '&at='.config('koel.itunes.affiliate_id');
} else {
$trackUrl .= '?at='.config('koel.itunes.affiliate_id');
}

return $trackUrl;
return $trackUrl;
}
);
} catch (Exception $e) {
Log::error($e);

Expand Down
3 changes: 1 addition & 2 deletions tests/Integration/SongTest.php
Expand Up @@ -29,8 +29,7 @@ public function it_returns_object_storage_public_url()
]),
]);

Cache::shouldReceive('get')->once()->with("OSUrl/{$song->id}");
Cache::shouldReceive('put')->once()->with("OSUrl/{$song->id}", $mockedURL, 60);
Cache::shouldReceive('remember')->andReturn($mockedURL);
$url = $song->getObjectStoragePublicUrl($client);

// Then I should receive the correct S3 public URL
Expand Down
32 changes: 11 additions & 21 deletions tests/Unit/MediaCacheTest.php
Expand Up @@ -2,6 +2,8 @@

namespace Tests\Unit;

use App\Models\Album;
use App\Models\Artist;
use App\Models\Song;
use Cache;
use MediaCache;
Expand All @@ -10,14 +12,17 @@
class MediaCacheTest extends TestCase
{
/** @test */
public function it_queries_fresh_data_from_database_if_a_cache_is_not_found()
public function it_queries_fresh_data_from_database_or_use_the_cache()
{
Cache::shouldReceive('get')->andReturnNull();
Cache::shouldReceive('forever')->once();

// Given a database with artists, albums, and songs
factory(Song::class, 5)->create();

Cache::shouldReceive('rememberForever')->andReturn([
'albums' => Album::orderBy('name')->get(),
'artists' => Artist::orderBy('name')->get(),
'songs' => Song::all(),
]);

// When I get data from the MediaCache service
$data = MediaCache::get();

Expand All @@ -31,7 +36,7 @@ public function it_queries_fresh_data_from_database_if_a_cache_is_not_found()
public function it_get_the_cached_data_if_found()
{
// Given there are media data cached
Cache::shouldReceive('get')->andReturn('dummy');
Cache::shouldReceive('rememberForever')->andReturn('dummy');

// And koel.cache_media configuration is TRUE
config(['koel.cache_media' => true]);
Expand All @@ -43,25 +48,10 @@ public function it_get_the_cached_data_if_found()
$this->assertEquals('dummy', $data);
}

/** @test */
public function it_caches_queried_data_if_cache_media_is_configured_to_true()
{
Cache::shouldReceive('forever')->once();
Cache::shouldReceive('get')->once()->andReturnNull();

// Given koel.cache_media configuration is TRUE
config(['koel.cache_media' => true]);

// When I get data from the MediaCache service
MediaCache::get();

// Then I see the cache-related methods being called
}

/** @test */
public function it_does_not_cache_queried_data_if_cache_media_is_configured_to_false()
{
Cache::shouldReceive('forever')->never();
Cache::shouldReceive('rememberForever')->never();

// Given koel.cache_media configuration is FALSE
config(['koel.cache_media' => false]);
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/iTunesTest.php
Expand Up @@ -10,6 +10,12 @@

class iTunesTest extends TestCase
{
public function tearDown()
{
m::close();
parent::tearDown();
}

/** @test */
public function it_can_be_instantiated()
{
Expand Down

0 comments on commit f344b4c

Please sign in to comment.