Create a folder repository

parent 18aed5a7
......@@ -19,7 +19,9 @@ use Modules\Media\Events\Handlers\RegisterMediaSidebar;
use Modules\Media\Events\Handlers\RemovePolymorphicLink;
use Modules\Media\Image\ThumbnailManager;
use Modules\Media\Repositories\Eloquent\EloquentFileRepository;
use Modules\Media\Repositories\Eloquent\EloquentFolderRepository;
use Modules\Media\Repositories\FileRepository;
use Modules\Media\Repositories\FolderRepository;
use Modules\Tag\Repositories\TagManager;
class MediaServiceProvider extends ServiceProvider
......@@ -90,6 +92,9 @@ class MediaServiceProvider extends ServiceProvider
$this->app->bind(FileRepository::class, function () {
return new EloquentFileRepository(new File());
});
$this->app->bind(FolderRepository::class, function () {
return new EloquentFolderRepository(new File());
});
}
/**
......
<?php
namespace Modules\Media\Repositories\Eloquent;
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
use Modules\Media\Repositories\FolderRepository;
class EloquentFolderRepository extends EloquentBaseRepository implements FolderRepository
{
public function create($data)
{
return $this->model->create([
'filename' => array_get($data, 'name'),
'path' => str_slug(array_get($data, 'name')),
'is_folder' => true,
]);
}
}
<?php
namespace Modules\Media\Repositories;
use Modules\Core\Repositories\BaseRepository;
interface FolderRepository extends BaseRepository
{
}
<?php
namespace Modules\Media\Tests;
use Modules\Media\Repositories\Eloquent\EloquentFolderRepository;
use Modules\Media\Repositories\FolderRepository;
use PHPUnit\Framework\TestCase;
final class EloquentFolderRepositoryTest extends MediaTestCase
{
/**
* @var FolderRepository
*/
private $folder;
public function setUp()
{
parent::setUp();
$this->resetDatabase();
$this->folder = app(FolderRepository::class);
}
/** @test */
public function it_can_create_a_folder()
{
$folder = $this->folder->create(['name' => 'My Folder']);
$this->assertCount(1, $this->folder->all());
$this->assertEquals('My Folder', $folder->filename);
$this->assertEquals('my-folder', $folder->path->getRelativeUrl());
$this->assertTrue( $folder->is_folder);
}
private function resetDatabase()
{
// Makes sure the migrations table is created
$this->artisan('migrate', [
'--database' => 'sqlite',
]);
// We empty all tables
$this->artisan('migrate:reset', [
'--database' => 'sqlite',
]);
// Migrate
$this->artisan('migrate', [
'--database' => 'sqlite',
]);
$this->artisan('migrate', [
'--database' => 'sqlite',
'--path' => 'Modules/Tag/Database/Migrations',
]);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment