Skip to content

Commit

Permalink
test: Added test for custom key name
Browse files Browse the repository at this point in the history
Added test media model with custom key name, migration to alter media table with custom key name and test to rename files on a model with custom key name
  • Loading branch information
esadewater committed Sep 19, 2024
1 parent d7e28eb commit 3bd4db4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tests/Feature/Media/RenameTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use Spatie\MediaLibrary\MediaLibraryServiceProvider;
use Spatie\MediaLibrary\Tests\TestSupport\TestModels\TestCustomMediaWithCustomKeyName;

it('will rename the file if it is changed on the media object', function () {
$testFile = $this->getTestFilesDirectory('test.jpg');

Expand All @@ -14,6 +17,26 @@
$this->assertFileExists($this->getMediaDirectory($media->id.'/test-new-name.jpg'));
});

it('will rename the file with a custom model with custom key name', function () {
config()->set('media-library.media_model', TestCustomMediaWithCustomKeyName::class);

(new MediaLibraryServiceProvider(app()))->register()->boot();

$this->setUpDatabaseCustomKeyName();

$testFile = $this->getTestFilesDirectory('test.jpg');

$media = $this->testModel->addMedia($testFile)->toMediaCollection();

$this->assertFileExists($this->getMediaDirectory($media->getKey().'/test.jpg'));

$media->file_name = 'test-new-name.jpg';
$media->save();

$this->assertFileDoesNotExist($this->getMediaDirectory($media->getKey().'/test.jpg'));
$this->assertFileExists($this->getMediaDirectory($media->getKey().'/test-new-name.jpg'));
});

it('will rename conversions', function () {
$testFile = $this->getTestFilesDirectory('test.jpg');

Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use CreateTemporaryUploadsTable;
use Dotgetenv\Dotgetenv;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\File;
use Orchestra\Testbench\TestCase as Orchestra;
use Schema;
use Spatie\MediaLibrary\MediaLibraryServiceProvider;
use Spatie\MediaLibrary\Support\MediaLibraryPro;
use Spatie\MediaLibrary\Tests\TestSupport\TestModels\TestModel;
Expand Down Expand Up @@ -153,6 +155,21 @@ protected function setUpDatabase($app)
$mediaTableMigration->up();
}

protected function setUpDatabaseCustomKeyName()
{
$customKeyNameMigration = new class extends Migration
{
public function up()
{
Schema::table('media', function (Blueprint $table) {
$table->renameColumn('id', 'custom_key_id');
});
}
};

$customKeyNameMigration->up();
}

protected function setUpTempTestFiles()
{
$this->initializeDirectory($this->getTestFilesDirectory());
Expand Down
12 changes: 12 additions & 0 deletions tests/TestSupport/TestModels/TestCustomMediaWithCustomKeyName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Spatie\MediaLibrary\Tests\TestSupport\TestModels;

use Spatie\MediaLibrary\MediaCollections\Models\Media;

class TestCustomMediaWithCustomKeyName extends Media
{
protected $table = 'media';

protected $primaryKey = 'custom_key_id';
}

0 comments on commit 3bd4db4

Please sign in to comment.