From 25eb66af025f3ce540a8622dbf0ac546fc5b77c2 Mon Sep 17 00:00:00 2001 From: Sean Fraser Date: Tue, 8 Feb 2022 22:28:06 -0500 Subject: [PATCH] don't purge meta when soft deleting --- src/Metable.php | 3 ++ tests/Integration/MetableTest.php | 30 +++++++++++++++++++ tests/Mocks/SampleMetableSoftDeletes.php | 15 ++++++++++ tests/factories/MetableFactory.php | 5 ++++ ...01_000000_create_sample_classes_tables.php | 1 + 5 files changed, 54 insertions(+) create mode 100644 tests/Mocks/SampleMetableSoftDeletes.php diff --git a/src/Metable.php b/src/Metable.php index 5a9034d..d41e29a 100644 --- a/src/Metable.php +++ b/src/Metable.php @@ -40,6 +40,9 @@ public static function bootMetable() { // delete all attached meta on deletion static::deleted(function (self $model) { + if (method_exists($model, 'isForceDeleting') && !$model->isForceDeleting()) { + return; + } $model->purgeMeta(); }); } diff --git a/tests/Integration/MetableTest.php b/tests/Integration/MetableTest.php index 28342ea..8434d97 100644 --- a/tests/Integration/MetableTest.php +++ b/tests/Integration/MetableTest.php @@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\Collection; use Plank\Metable\Meta; use Plank\Metable\Tests\Mocks\SampleMetable; +use Plank\Metable\Tests\Mocks\SampleMetableSoftDeletes; use Plank\Metable\Tests\TestCase; use ReflectionClass; @@ -214,6 +215,30 @@ public function test_it_clears_meta_on_deletion() $this->assertEquals(0, $meta->count()); } + public function test_it_does_not_clear_meta_on_soft_deletion() + { + $this->useDatabase(); + $metable = $this->createMetableSoftDeletes(); + $metable->setMeta('foo', 'bar'); + + $metable->delete(); + $meta = Meta::all(); + + $this->assertEquals(1, $meta->count()); + } + + public function test_it_does_clear_meta_on_force_deletion() + { + $this->useDatabase(); + $metable = $this->createMetableSoftDeletes(); + $metable->setMeta('foo', 'bar'); + + $metable->forceDelete(); + $meta = Meta::all(); + + $this->assertEquals(0, $meta->count()); + } + public function test_it_can_be_queried_by_single_meta_key() { $this->useDatabase(); @@ -502,4 +527,9 @@ private function createMetable(array $attributes = []): SampleMetable { return factory(SampleMetable::class)->create($attributes); } + + private function createMetableSoftDeletes(array $attributes = []): SampleMetableSoftDeletes + { + return factory(SampleMetableSoftDeletes::class)->create($attributes); + } } diff --git a/tests/Mocks/SampleMetableSoftDeletes.php b/tests/Mocks/SampleMetableSoftDeletes.php new file mode 100644 index 0000000..fdb4221 --- /dev/null +++ b/tests/Mocks/SampleMetableSoftDeletes.php @@ -0,0 +1,15 @@ +define(SampleMetable::class, function (Faker\Generator $faker) { return []; }); + +$factory->define(SampleMetableSoftDeletes::class, function (Faker\Generator $faker) { + return []; +}); diff --git a/tests/migrations/2017_01_01_000000_create_sample_classes_tables.php b/tests/migrations/2017_01_01_000000_create_sample_classes_tables.php index e4bb743..6ec45ce 100644 --- a/tests/migrations/2017_01_01_000000_create_sample_classes_tables.php +++ b/tests/migrations/2017_01_01_000000_create_sample_classes_tables.php @@ -15,6 +15,7 @@ public function up() { Schema::create('sample_metables', function (Blueprint $table) { $table->increments('id'); + $table->softDeletes(); $table->timestamps(); }); }