medialibrary-helper/tests/TestCase.php

88 lines
2.2 KiB
PHP
Raw Normal View History

2023-03-07 15:31:24 +01:00
<?php
namespace Zoomyboy\MedialibraryHelper\Tests;
use Illuminate\Http\File;
use Illuminate\Support\Facades\Gate;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Spatie\LaravelData\LaravelDataServiceProvider;
use Spatie\MediaLibrary\MediaLibraryServiceProvider;
use Zoomyboy\MedialibraryHelper\ServiceProvider;
use Zoomyboy\MedialibraryHelper\Tests\Models\Post;
class TestCase extends BaseTestCase
{
/**
* Define database migrations.
*/
protected function defineDatabaseMigrations(): void
{
2024-01-02 01:28:49 +01:00
$this->loadMigrationsFrom(__DIR__ . '/migrations');
2023-03-07 15:31:24 +01:00
}
protected function getPackageProviders($app): array
{
return [
ServiceProvider::class,
MediaLibraryServiceProvider::class,
LaravelDataServiceProvider::class,
];
}
/**
2023-03-07 23:11:35 +01:00
* Generate a pdf file with a filename and get path.
2023-03-07 15:31:24 +01:00
*/
protected function pdfFile(?string $filename = null): File
{
return $this->getFile('pdf.pdf', $filename ?: 'pdf.pdf');
}
2023-03-08 00:58:15 +01:00
protected function jpgFile(?string $filename = null): File
{
return $this->getFile('jpg.jpg', $filename ?: 'jpg.jpg');
}
2023-03-07 15:31:24 +01:00
protected function getFile(string $location, string $as): File
{
2024-01-02 01:28:49 +01:00
$path = __DIR__ . '/stubs/' . $location;
$to = sys_get_temp_dir() . '/' . $as;
2023-03-07 15:31:24 +01:00
copy($path, $to);
return new File($to);
}
protected function registerModel(): static
{
app()->extend('media-library-helpers', fn ($p) => $p->put('post', Post::class));
return $this;
}
protected function newPost(): Post
{
return Post::create(['title' => 'Lorem', 'content' => 'aafff']);
}
protected function auth(array $policies = []): self
{
$policies = [
'storeMedia' => true,
'updateMedia' => true,
'destroyMedia' => true,
2023-03-08 00:04:00 +01:00
'listMedia' => true,
2023-03-07 15:31:24 +01:00
...$policies,
];
foreach ($policies as $ability => $result) {
2023-03-08 10:47:17 +01:00
Gate::define($ability, fn (?string $user, string $collectionName) => $result);
2023-03-07 15:31:24 +01:00
}
return $this;
}
2023-03-07 23:11:35 +01:00
protected function defineEnvironment($app)
{
2023-03-08 14:53:00 +01:00
$app['config']->set('media-library.middleware', ['web']);
2023-03-07 23:11:35 +01:00
}
2023-03-07 15:31:24 +01:00
}