From 97ff39263017c4aecdaeea57502a4dbb4c1336c1 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 13 Sep 2023 16:51:11 -0400 Subject: [PATCH] initial sofdelete trait tests --- tests/Database/Traits/SoftDeleteTest.php | 132 +++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 tests/Database/Traits/SoftDeleteTest.php diff --git a/tests/Database/Traits/SoftDeleteTest.php b/tests/Database/Traits/SoftDeleteTest.php new file mode 100644 index 000000000..fba13d0c9 --- /dev/null +++ b/tests/Database/Traits/SoftDeleteTest.php @@ -0,0 +1,132 @@ +seeded = [ + 'posts' => [], + 'categories' => [] + ]; + + $this->createTables(); + } + + protected function createTables() + { + $this->getBuilder()->create('posts', function ($table) { + $table->increments('id'); + $table->string('title')->default(''); + $table->timestamps(); + $table->timestamp('deleted_at')->nullable(); + }); + + $this->getBuilder()->create('categories', function ($table) { + $table->increments('id'); + $table->string('name'); + $table->timestamps(); + }); + + $this->getBuilder()->create('categories_posts', function ($table) { + $table->primary(['post_id', 'category_id']); + $table->unsignedInteger('post_id'); + $table->unsignedInteger('category_id'); + $table->timestamp('deleted_at')->nullable(); + }); + + $this->seedTables(); + } + + protected function seedTables() + { + $this->seeded['posts'][] = Post::create([ + 'title' => 'First Post', + ]); + $this->seeded['posts'][] = Post::create([ + 'title' => 'Second Post', + ]); + + $this->seeded['categories'][] = Category::create([ + 'name' => 'Category 1' + ]); + $this->seeded['categories'][] = Category::create([ + 'name' => 'Category 2' + ]); + + $this->seeded['posts'][0]->categories()->attach($this->seeded['categories'][0]); + $this->seeded['posts'][0]->categories()->attach($this->seeded['categories'][1]); + + $this->seeded['posts'][1]->categories()->attach($this->seeded['categories'][0]); + $this->seeded['posts'][1]->categories()->attach($this->seeded['categories'][1]); + } + + public function testDeleteAndRestore() + { + $post = Post::where('title', 'First Post')->first(); + $this->assertTrue($post->deleted_at === null); + $this->assertTrue($post->categories()->where('deleted_at', null)->count() === 2); + + $post->delete(); + + $post = Post::withTrashed()->first(); + $this->assertTrue($post->deleted_at != null); + $this->assertTrue($post->categories()->where('deleted_at', '!=', null)->count() === 2); + $post->restore(); + + $post = Post::first(); + $this->assertTrue($post->deleted_at === null); + $this->assertTrue($post->categories()->where('deleted_at', null)->count() === 2); + } +} + +class Post extends \Winter\Storm\Database\Model +{ + use \Winter\Storm\Database\Traits\SoftDelete; + + public $table = 'posts'; + + public $fillable = ['title']; + + protected $dates = [ + 'created_at', + 'updated_at', + 'deleted_at', + ]; + + public $belongsToMany = [ + 'categories' => [ + Category::class, + 'table' => 'categories_posts', + 'key' => 'post_id', + 'otherKey' => 'category_id', + 'softDelete' => true, + ], + ]; +} + +class Category extends \Winter\Storm\Database\Model +{ + public $table = 'categories'; + + public $fillable = ['name']; + + protected $dates = [ + 'created_at', + 'updated_at', + ]; + + public $belongsToMany = [ + 'posts' => [ + Post::class, + 'table' => 'categories_posts', + 'key' => 'category_id', + 'otherKey' => 'post_id', + ], + ]; +}