Skip to content

Commit

Permalink
don't purge meta when soft deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
frasmage committed Feb 9, 2022
1 parent a8c4ba6 commit 25eb66a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Metable.php
Expand Up @@ -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();
});
}
Expand Down
30 changes: 30 additions & 0 deletions tests/Integration/MetableTest.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
15 changes: 15 additions & 0 deletions tests/Mocks/SampleMetableSoftDeletes.php
@@ -0,0 +1,15 @@
<?php

namespace Plank\Metable\Tests\Mocks;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Plank\Metable\Metable;

class SampleMetableSoftDeletes extends Model
{
use Metable;
use SoftDeletes;

protected $table = 'sample_metables';
}
5 changes: 5 additions & 0 deletions tests/factories/MetableFactory.php
@@ -1,7 +1,12 @@
<?php

use Plank\Metable\Tests\Mocks\SampleMetable;
use Plank\Metable\Tests\Mocks\SampleMetableSoftDeletes;

$factory->define(SampleMetable::class, function (Faker\Generator $faker) {
return [];
});

$factory->define(SampleMetableSoftDeletes::class, function (Faker\Generator $faker) {
return [];
});
Expand Up @@ -15,6 +15,7 @@ public function up()
{
Schema::create('sample_metables', function (Blueprint $table) {
$table->increments('id');
$table->softDeletes();
$table->timestamps();
});
}
Expand Down

0 comments on commit 25eb66a

Please sign in to comment.