Skip to content
This repository has been archived by the owner on Nov 16, 2021. It is now read-only.

Use morph mapping for attachables #77

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<php>
<env name="DB_CONNECTION" value="testing"/>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need this setup?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one line of configuration replaces the entire getEnvironmentSetup method when you only need the default in memory database setup.

https://packages.tools/testbench/getting-started/configuration.html#in-memory-sqlite-connection

<env name="APP_KEY" value="base64:2fl+Ktvkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/>
<env name="APP_DEBUG" value="true"/>
</php>
</phpunit>
2 changes: 1 addition & 1 deletion src/Handlers/DeleteAttachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Models/PendingAttachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 4 additions & 5 deletions tests/Fixtures/TestServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
}
3 changes: 1 addition & 2 deletions tests/FroalaUploadControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
]);
}

Expand Down
19 changes: 2 additions & 17 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,26 +50,10 @@ protected function getPackageProviders($app)
NovaServiceProvider::class,
NovaApplicationServiceProvider::class,
FroalaFieldServiceProvider::class,
TestServiceProvider::class,
];
}

/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you have removed default DB connection setup?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// 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.
*
Expand Down
7 changes: 2 additions & 5 deletions tests/TrixDriverUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use function Froala\NovaFroalaField\nova_version_at_least;
use Froala\NovaFroalaField\Tests\Fixtures\Article;
use Froala\NovaFroalaField\Tests\Fixtures\TestServiceProvider;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Storage;
use Laravel\Nova\Trix\Attachment;
Expand All @@ -26,11 +25,9 @@ public function setUp(): void

protected function getPackageProviders($app)
{
$providers = parent::getPackageProviders($app);
$app['config']->set('nova.froala-field.attachments_driver', 'trix');

array_unshift($providers, TestServiceProvider::class);

return $providers;
return parent::getPackageProviders($app);
}

/** @test */
Expand Down
2 changes: 1 addition & 1 deletion tests/UploadsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down