chore: sandbox tests

This commit is contained in:
Phan An 2020-06-13 14:19:24 +02:00
parent c707a0be65
commit 3fbf14efd2
13 changed files with 102 additions and 40 deletions

27
app/Helpers.php Normal file
View file

@ -0,0 +1,27 @@
<?php
namespace App\Helpers;
function album_cover_path(string $fileName): string {
return public_path(config('koel.album_cover_dir') . $fileName);
}
function album_cover_url(string $fileName): string {
return app()->staticUrl(config('koel.album_cover_dir') . $fileName);
}
/**
* @see album_cover_url()
*/
function album_thumbnail_url(string $fileName): string {
return album_cover_url($fileName);
}
function artist_image_path(string $fileName): string {
return public_path(config('koel.artist_image_dir') . $fileName);
}
function artist_image_url(string $fileName): string {
return app()->staticUrl(config('koel.artist_image_dir') . $fileName);
}

View file

@ -7,6 +7,8 @@ use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use function App\Helpers\album_cover_path;
use function App\Helpers\album_cover_url;
/**
* @property string $cover The album cover's file name
@ -79,7 +81,7 @@ class Album extends Model
public function getCoverAttribute(?string $value): string
{
return app()->staticUrl('public/img/covers/'.($value ?: self::UNKNOWN_COVER));
return album_cover_url($value ?: self::UNKNOWN_COVER);
}
public function getHasCoverAttribute(): bool
@ -94,18 +96,14 @@ class Album extends Model
return false;
}
return file_exists(public_path("/public/img/covers/$cover"));
return file_exists(album_cover_path($cover));
}
public function getCoverPathAttribute(): ?string
{
$cover = array_get($this->attributes, 'cover');
if (!$cover) {
return null;
}
return public_path("/public/img/covers/$cover");
return $cover ? album_cover_path($cover) : null;
}
/**
@ -135,11 +133,11 @@ class Album extends Model
public function getThumbnailPathAttribute(): ?string
{
return $this->thumbnail_name ? public_path("/public/img/covers/{$this->thumbnail_name}") : null;
return $this->thumbnail_name ? album_cover_path($this->thumbnail_name) : null;
}
public function getThumbnailAttribute(): ?string
{
return $this->thumbnail_name ? app()->staticUrl("public/img/covers/$this->thumbnail_name") : null;
return $this->thumbnail_name ? album_cover_url($this->thumbnail_name) : null;
}
}

View file

@ -8,6 +8,8 @@ use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use function App\Helpers\artist_image_path;
use function App\Helpers\artist_image_url;
/**
* @property int $id
@ -93,7 +95,7 @@ class Artist extends Model
*/
public function getImageAttribute(?string $value): ?string
{
return $value ? app()->staticUrl("public/img/artists/$value") : null;
return $value ? artist_image_url($value) : null;
}
public function getImagePathAttribute(): ?string
@ -102,9 +104,7 @@ class Artist extends Model
return null;
}
$image = array_get($this->attributes, 'image');
return public_path("public/img/artists/$image");
return artist_image_path(array_get($this->attributes, 'image'));
}
public function getHasImageAttribute(): bool
@ -115,6 +115,6 @@ class Artist extends Model
return false;
}
return file_exists(public_path("public/img/artists/$image"));
return file_exists(artist_image_path($image));
}
}

View file

@ -6,6 +6,8 @@ use App\Models\Album;
use App\Models\Artist;
use Exception;
use Psr\Log\LoggerInterface;
use function App\Helpers\album_cover_path;
use function App\Helpers\artist_image_path;
class MediaMetadataService
{
@ -99,7 +101,7 @@ class MediaMetadataService
*/
private function generateAlbumCoverPath(string $extension): string
{
return public_path(sprintf('public/img/covers/%s.%s', sha1(uniqid()), $extension));
return album_cover_path(sprintf('%s.%s', sha1(uniqid()), $extension));
}
/**
@ -109,7 +111,7 @@ class MediaMetadataService
*/
private function generateArtistImagePath($extension): string
{
return public_path(sprintf('public/img/artists/%s.%s', sha1(uniqid()), $extension));
return artist_image_path(sprintf('%s.%s', sha1(uniqid()), $extension));
}
/**

View file

@ -51,7 +51,10 @@
"psr-4": {
"App\\": "app/",
"Tests\\": "tests/"
}
},
"files": [
"app/Helpers.php"
]
},
"autoload-dev": {
"classmap": [

View file

@ -19,6 +19,12 @@ return [
'media_path' => env('MEDIA_PATH'),
// The *relative* path to the directory to store album covers and thumbnails, *with* a trailing slash.
'album_cover_dir' => 'public/img/covers/',
// The *relative* path to the directory to store artist images, *with* a trailing slash.
'artist_image_dir' => 'public/img/artists/',
/*
|--------------------------------------------------------------------------
| Sync Options

View file

@ -6,6 +6,7 @@ use App\Models\Album;
use App\Services\MediaMetadataService;
use Mockery;
use Mockery\MockInterface;
use function App\Helpers\album_cover_url;
class AlbumThumbnailTest extends TestCase
{

View file

@ -11,8 +11,9 @@ use Illuminate\Foundation\Testing\DatabaseTransactions;
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;
use Mockery;
use ReflectionClass;
use Tests\CreatesApplication;
use Tests\Traits\CreatesApplication;
use Tests\Traits\InteractsWithIoc;
use Tests\Traits\SandboxesTests;
use Tymon\JWTAuth\JWTAuth;
abstract class TestCase extends BaseTestCase
@ -20,6 +21,7 @@ abstract class TestCase extends BaseTestCase
use CreatesApplication;
use DatabaseTransactions;
use InteractsWithIoc;
use SandboxesTests;
/** @var JWTAuth */
private $auth;
@ -29,8 +31,8 @@ abstract class TestCase extends BaseTestCase
parent::setUp();
$this->auth = app(JWTAuth::class);
$this->prepareForTests();
self::createSandbox();
}
/**
@ -103,8 +105,8 @@ abstract class TestCase extends BaseTestCase
protected function tearDown(): void
{
$this->addToAssertionCount(Mockery::getContainer()->mockery_getExpectationCount());
Mockery::close();
self::destroySandbox();
parent::tearDown();
}
}

View file

@ -5,6 +5,8 @@ namespace Tests\Integration\Services;
use App\Models\Album;
use App\Services\MediaMetadataService;
use Tests\TestCase;
use function App\Helpers\album_cover_path;
use function App\Helpers\album_cover_url;
class MediaMetadataServiceTest extends TestCase
{
@ -16,16 +18,16 @@ class MediaMetadataServiceTest extends TestCase
public function testGetAlbumThumbnailUrl(): void
{
copy(__DIR__ . '/../../blobs/cover.png', $this->coverPath . '/album-cover-for-thumbnail-test.jpg');
copy(__DIR__ . '/../../blobs/cover.png', album_cover_path('album-cover-for-thumbnail-test.jpg'));
$album = factory(Album::class)->create(['cover' => 'album-cover-for-thumbnail-test.jpg']);
self::assertSame(
app()->staticUrl('public/img/covers/album-cover-for-thumbnail-test_thumb.jpg'),
album_cover_url('album-cover-for-thumbnail-test_thumb.jpg'),
app()->get(MediaMetadataService::class)->getAlbumThumbnailUrl($album)
);
self::assertFileExists($this->coverPath . '/album-cover-for-thumbnail-test_thumb.jpg');
self::assertFileExists(album_cover_path('album-cover-for-thumbnail-test_thumb.jpg'));
}
public function testGetAlbumThumbnailUrlWithNoCover(): void
@ -36,10 +38,10 @@ class MediaMetadataServiceTest extends TestCase
private function cleanUp(): void
{
@unlink($this->coverPath . '/album-cover-for-thumbnail-test.jpg');
@unlink($this->coverPath . '/album-cover-for-thumbnail-test_thumb.jpg');
self::assertFileNotExists($this->coverPath . '/album-cover-for-thumbnail-test.jpg');
self::assertFileNotExists($this->coverPath . '/album-cover-for-thumbnail-test_thumb.jpg');
@unlink(album_cover_path('album-cover-for-thumbnail-test.jpg'));
@unlink(album_cover_path('album-cover-for-thumbnail-test_thumb.jpg'));
self::assertFileNotExists(album_cover_path('album-cover-for-thumbnail-test.jpg'));
self::assertFileNotExists(album_cover_path('album-cover-for-thumbnail-test_thumb.jpg'));
}
protected function tearDown(): void

View file

@ -5,25 +5,29 @@ namespace Tests;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Mockery;
use Tests\Traits\CreatesApplication;
use Tests\Traits\InteractsWithIoc;
use Tests\Traits\SandboxesTests;
abstract class TestCase extends BaseTestCase
{
use DatabaseTransactions;
use CreatesApplication;
use InteractsWithIoc;
use SandboxesTests;
public function setUp(): void
{
parent::setUp();
$this->prepareForTests();
self::createSandbox();
}
protected function tearDown(): void
{
$this->addToAssertionCount(Mockery::getContainer()->mockery_getExpectationCount());
Mockery::close();
self::destroySandbox();
parent::tearDown();
}
}

View file

@ -1,6 +1,6 @@
<?php
namespace Tests;
namespace Tests\Traits;
use App\Console\Kernel;
use App\Models\User;
@ -9,8 +9,7 @@ use Illuminate\Foundation\Application;
trait CreatesApplication
{
protected $coverPath;
protected $mediaPath = __DIR__.'/songs';
protected $mediaPath = __DIR__ . '/../songs';
/** @var Kernel */
private $artisan;
@ -25,13 +24,11 @@ trait CreatesApplication
public function createApplication(): Application
{
/** @var Application $app */
$app = require __DIR__.'/../bootstrap/app.php';
$app = require __DIR__ . '/../../bootstrap/app.php';
$this->artisan = $app->make(Artisan::class);
$this->artisan->bootstrap();
$this->coverPath = $app->basePath('/public/img/covers');
return $app;
}
@ -42,9 +39,5 @@ trait CreatesApplication
if (!User::all()->count()) {
$this->artisan->call('db:seed');
}
if (!file_exists($this->coverPath)) {
mkdir($this->coverPath, 0777, true);
}
}
}

View file

@ -0,0 +1,22 @@
<?php
namespace Tests\Traits;
use Illuminate\Support\Facades\File;
trait SandboxesTests
{
private static function createSandbox(): void
{
config(['koel.album_cover_dir' => 'public/sandbox/img/covers/']);
config(['koel.artist_image_dir' => 'public/sandbox/img/artists/']);
@mkdir(base_path(config('koel.album_cover_dir')), 0755, true);
@mkdir(base_path(config('koel.artist_image_dir')), 0755, true);
}
private static function destroySandbox(): void
{
File::deleteDirectory(base_path('public/sandbox'));
}
}

View file

@ -10,6 +10,8 @@ use Illuminate\Log\Logger;
use Mockery;
use Mockery\MockInterface;
use Tests\TestCase;
use function App\Helpers\album_cover_url;
use function App\Helpers\artist_image_url;
class MediaMetadataServiceTest extends TestCase
{
@ -40,7 +42,7 @@ class MediaMetadataServiceTest extends TestCase
->with('/koel/public/images/album/foo.jpg', 'dummy');
$this->mediaMetadataService->writeAlbumCover($album, $coverContent, 'jpg', $coverPath);
$this->assertEquals('http://localhost/public/img/covers/foo.jpg', Album::find($album->id)->cover);
$this->assertEquals(album_cover_url('foo.jpg'), Album::find($album->id)->cover);
}
public function testWriteArtistImage(): void
@ -56,6 +58,6 @@ class MediaMetadataServiceTest extends TestCase
->with('/koel/public/images/artist/foo.jpg', 'dummy');
$this->mediaMetadataService->writeArtistImage($artist, $imageContent, 'jpg', $imagePath);
$this->assertEquals('http://localhost/public/img/artists/foo.jpg', Artist::find($artist->id)->image);
$this->assertEquals(artist_image_url('foo.jpg'), Artist::find($artist->id)->image);
}
}