From 1a9344a1ef82a00da28552c4f4b37922b8d3f476 Mon Sep 17 00:00:00 2001 From: wimski Date: Wed, 5 May 2021 20:33:10 +0200 Subject: [PATCH 1/3] Replace database config with php evn vars --- phpunit.xml.dist | 5 +++++ tests/TestCase.php | 17 ----------------- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 4d11fda..194285f 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -19,4 +19,9 @@ src/ + + + + + diff --git a/tests/TestCase.php b/tests/TestCase.php index 1ae048a..816142b 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -52,23 +52,6 @@ protected function getPackageProviders($app) ]; } - /** - * Define environment setup. - * - * @param \Illuminate\Foundation\Application $app - * @return void - */ - protected function getEnvironmentSetUp($app) - { - // Setup default database to use sqlite :memory: - $app['config']->set('database.default', 'testbench'); - $app['config']->set('database.connections.testbench', [ - 'driver' => 'sqlite', - 'database' => ':memory:', - 'prefix' => '', - ]); - } - /** * Set up the database. * From 3e349b962c5727da72b40fcbbb9809491833421d Mon Sep 17 00:00:00 2001 From: wimski Date: Wed, 5 May 2021 20:33:45 +0200 Subject: [PATCH 2/3] Update config use in tests --- tests/TrixDriverUploadTest.php | 9 +++------ tests/UploadsHelper.php | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/tests/TrixDriverUploadTest.php b/tests/TrixDriverUploadTest.php index 7f4d7eb..1029b7e 100644 --- a/tests/TrixDriverUploadTest.php +++ b/tests/TrixDriverUploadTest.php @@ -2,9 +2,8 @@ namespace Froala\NovaFroalaField\Tests; -use function Froala\NovaFroalaField\nova_version_at_least; use Froala\NovaFroalaField\Tests\Fixtures\Article; -use Froala\NovaFroalaField\Tests\Fixtures\TestServiceProvider; +use function Froala\NovaFroalaField\nova_version_at_least; use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Storage; use Laravel\Nova\Trix\Attachment; @@ -26,11 +25,9 @@ public function setUp(): void protected function getPackageProviders($app) { - $providers = parent::getPackageProviders($app); - - array_unshift($providers, TestServiceProvider::class); + $app['config']->set('nova.froala-field.attachments_driver', 'trix'); - return $providers; + return parent::getPackageProviders($app); } /** @test */ diff --git a/tests/UploadsHelper.php b/tests/UploadsHelper.php index fa720f4..475f7f8 100644 --- a/tests/UploadsHelper.php +++ b/tests/UploadsHelper.php @@ -32,7 +32,7 @@ protected function regenerateUpload() protected function uploadPendingFile(): TestResponse { - $url = config('nova.froala-field.attachments_driver') === 'trix' + $url = $this->app['config']->get('nova.froala-field.attachments_driver') === 'trix' ? '/nova-api/articles/trix-attachment/content' : 'nova-vendor/froala-field/articles/attachments/content'; From 4d39bdcf8e642b8fa5fed4c24fdec61c7f6ea7f9 Mon Sep 17 00:00:00 2001 From: wimski Date: Wed, 5 May 2021 20:34:05 +0200 Subject: [PATCH 3/3] Use morph mapping for attachables --- src/Handlers/DeleteAttachments.php | 2 +- src/Models/PendingAttachment.php | 2 +- tests/Fixtures/TestServiceProvider.php | 9 ++++----- tests/FroalaUploadControllerTest.php | 3 +-- tests/TestCase.php | 2 ++ tests/TrixDriverUploadTest.php | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Handlers/DeleteAttachments.php b/src/Handlers/DeleteAttachments.php index 01cf377..bc26b3e 100644 --- a/src/Handlers/DeleteAttachments.php +++ b/src/Handlers/DeleteAttachments.php @@ -34,7 +34,7 @@ public function __construct($field) */ public function __invoke(Request $request, $model) { - Attachment::where('attachable_type', get_class($model)) + Attachment::where('attachable_type', $model->getMorphClass()) ->where('attachable_id', $model->getKey()) ->get() ->each diff --git a/src/Models/PendingAttachment.php b/src/Models/PendingAttachment.php index b7928a6..ba9f04d 100644 --- a/src/Models/PendingAttachment.php +++ b/src/Models/PendingAttachment.php @@ -46,7 +46,7 @@ public static function persistDraft($draftId, Froala $field, $model) public function persist(Froala $field, $model) { Attachment::create([ - 'attachable_type' => get_class($model), + 'attachable_type' => $model->getMorphClass(), 'attachable_id' => $model->getKey(), 'attachment' => $this->attachment, 'disk' => $field->disk, diff --git a/tests/Fixtures/TestServiceProvider.php b/tests/Fixtures/TestServiceProvider.php index 126c2ca..025c97e 100644 --- a/tests/Fixtures/TestServiceProvider.php +++ b/tests/Fixtures/TestServiceProvider.php @@ -2,16 +2,15 @@ namespace Froala\NovaFroalaField\Tests\Fixtures; +use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\ServiceProvider; class TestServiceProvider extends ServiceProvider { public function boot() { - } - - public function register() - { - $this->app['config']->set('nova.froala-field.attachments_driver', 'trix'); + Relation::morphMap([ + 'article' => Article::class, + ]); } } diff --git a/tests/FroalaUploadControllerTest.php b/tests/FroalaUploadControllerTest.php index f901193..9e110b1 100644 --- a/tests/FroalaUploadControllerTest.php +++ b/tests/FroalaUploadControllerTest.php @@ -5,7 +5,6 @@ use Froala\NovaFroalaField\Models\Attachment; use Froala\NovaFroalaField\Models\PendingAttachment; use function Froala\NovaFroalaField\nova_version_at_least; -use Froala\NovaFroalaField\Tests\Fixtures\Article; use Illuminate\Support\Facades\Storage; class FroalaUploadControllerTest extends TestCase @@ -58,7 +57,7 @@ public function store_attachment() 'attachment' => $this->getAttachmentLocation(), 'url' => Storage::disk(static::DISK)->url($this->getAttachmentLocation()), 'attachable_id' => $response->json('id'), - 'attachable_type' => Article::class, + 'attachable_type' => 'article', ]); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 816142b..d5aaf3e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -4,6 +4,7 @@ use Froala\NovaFroalaField\FroalaFieldServiceProvider; use Froala\NovaFroalaField\Tests\Fixtures\TestResource; +use Froala\NovaFroalaField\Tests\Fixtures\TestServiceProvider; use Froala\NovaFroalaField\Tests\Fixtures\User; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Route; @@ -49,6 +50,7 @@ protected function getPackageProviders($app) NovaServiceProvider::class, NovaApplicationServiceProvider::class, FroalaFieldServiceProvider::class, + TestServiceProvider::class, ]; } diff --git a/tests/TrixDriverUploadTest.php b/tests/TrixDriverUploadTest.php index 1029b7e..7faa4a1 100644 --- a/tests/TrixDriverUploadTest.php +++ b/tests/TrixDriverUploadTest.php @@ -2,8 +2,8 @@ namespace Froala\NovaFroalaField\Tests; -use Froala\NovaFroalaField\Tests\Fixtures\Article; use function Froala\NovaFroalaField\nova_version_at_least; +use Froala\NovaFroalaField\Tests\Fixtures\Article; use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Storage; use Laravel\Nova\Trix\Attachment;