From 7cd2b2ff53d98915cff9e9ecebb3312f0b06f723 Mon Sep 17 00:00:00 2001 From: Mario Aguilar Alves Date: Wed, 16 Aug 2017 12:57:20 +0200 Subject: [PATCH 1/3] Adding polymorphic migrations, entities and tests --- tests/App/Comment.php | 20 +++++++++++ tests/App/Post.php | 25 ++++++++++++++ tests/App/Video.php | 25 ++++++++++++++ .../2017_08_16_120341_create_posts_table.php | 33 +++++++++++++++++++ .../2017_08_16_120348_create_videos_table.php | 33 +++++++++++++++++++ ...017_08_16_120355_create_comments_table.php | 33 +++++++++++++++++++ tests/IntegrationTest.php | 29 ++++++++++++++++ tests/LumenIntegrationTest.php | 29 ++++++++++++++++ 8 files changed, 227 insertions(+) create mode 100644 tests/App/Comment.php create mode 100644 tests/App/Post.php create mode 100644 tests/App/Video.php create mode 100644 tests/App/database/migrations/2017_08_16_120341_create_posts_table.php create mode 100644 tests/App/database/migrations/2017_08_16_120348_create_videos_table.php create mode 100644 tests/App/database/migrations/2017_08_16_120355_create_comments_table.php diff --git a/tests/App/Comment.php b/tests/App/Comment.php new file mode 100644 index 0000000..148bb0f --- /dev/null +++ b/tests/App/Comment.php @@ -0,0 +1,20 @@ +morphTo(); + } +} \ No newline at end of file diff --git a/tests/App/Post.php b/tests/App/Post.php new file mode 100644 index 0000000..14008af --- /dev/null +++ b/tests/App/Post.php @@ -0,0 +1,25 @@ +morphMany('Askedio\Tests\App\Comment', 'commentable'); + } +} \ No newline at end of file diff --git a/tests/App/Video.php b/tests/App/Video.php new file mode 100644 index 0000000..382e037 --- /dev/null +++ b/tests/App/Video.php @@ -0,0 +1,25 @@ +morphMany('Askedio\Tests\App\Comment', 'commentable'); + } +} \ No newline at end of file diff --git a/tests/App/database/migrations/2017_08_16_120341_create_posts_table.php b/tests/App/database/migrations/2017_08_16_120341_create_posts_table.php new file mode 100644 index 0000000..f5eaa7a --- /dev/null +++ b/tests/App/database/migrations/2017_08_16_120341_create_posts_table.php @@ -0,0 +1,33 @@ +getSchemaBuilder()->create('posts', function (Blueprint $table) { + $table->increments('id'); + $table->string('title', 100); + $table->text('body'); + $table->timestamps(); + $table->softDeletes(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + DB::connection()->getSchemaBuilder()->dropIfExists('posts'); + } +} diff --git a/tests/App/database/migrations/2017_08_16_120348_create_videos_table.php b/tests/App/database/migrations/2017_08_16_120348_create_videos_table.php new file mode 100644 index 0000000..a036def --- /dev/null +++ b/tests/App/database/migrations/2017_08_16_120348_create_videos_table.php @@ -0,0 +1,33 @@ +getSchemaBuilder()->create('videos', function (Blueprint $table) { + $table->increments('id'); + $table->string('title', 100); + $table->string('url', 255); + $table->timestamps(); + $table->softDeletes(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + DB::connection()->getSchemaBuilder()->dropIfExists('videos'); + } +} diff --git a/tests/App/database/migrations/2017_08_16_120355_create_comments_table.php b/tests/App/database/migrations/2017_08_16_120355_create_comments_table.php new file mode 100644 index 0000000..d0e5373 --- /dev/null +++ b/tests/App/database/migrations/2017_08_16_120355_create_comments_table.php @@ -0,0 +1,33 @@ +getSchemaBuilder()->create('comments', function (Blueprint $table) { + $table->increments('id'); + $table->string('body', 255); + $table->morphs('commentable'); + $table->timestamps(); + $table->softDeletes(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + DB::connection()->getSchemaBuilder()->dropIfExists('comments'); + } +} diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index 91afbbb..bb55c8a 100755 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -6,9 +6,12 @@ use Askedio\Tests\App\BadRelation; use Askedio\Tests\App\BadRelationAction; use Askedio\Tests\App\BadRelationB; +use Askedio\Tests\App\Comment; use Askedio\Tests\App\Languages; +use Askedio\Tests\App\Post; use Askedio\Tests\App\Profiles; use Askedio\Tests\App\User; +use Askedio\Tests\App\Video; /** * TO-DO: Need better testing. @@ -42,6 +45,32 @@ private function createUserRaw() return $user; } + private function createCommentRaw() + { + $post = Post::create([ + 'title' => 'Post', + 'body' => 'Post chulo' + ])->comments()->saveMany([ + new Comment(['body' => 'comentario post']), + ]); + + $video = Video::create([ + 'title' => 'Video', + 'url' => 'Video chulo' + ])->comments()->saveMany([ + new Comment(['body' => 'comentario video']), + ]); + + return $this; + } + + public function testPolymorphicRelation() + { + $this->createCommentRaw(); + + Post::first()->delete(); + } + public function testBadRelation() { $this->createUserRaw(); diff --git a/tests/LumenIntegrationTest.php b/tests/LumenIntegrationTest.php index 0515f78..9e9b354 100644 --- a/tests/LumenIntegrationTest.php +++ b/tests/LumenIntegrationTest.php @@ -6,9 +6,12 @@ use Askedio\Tests\App\BadRelation; use Askedio\Tests\App\BadRelationAction; use Askedio\Tests\App\BadRelationB; +use Askedio\Tests\App\Comment; use Askedio\Tests\App\Languages; +use Askedio\Tests\App\Post; use Askedio\Tests\App\Profiles; use Askedio\Tests\App\User; +use Askedio\Tests\App\Video; /** * TO-DO: Need better testing. @@ -46,6 +49,32 @@ private function createUserRaw() return $user; } + private function createCommentRaw() + { + $post = Post::create([ + 'title' => 'Post', + 'body' => 'Post chulo' + ])->comments()->saveMany([ + new Comment(['body' => 'comentario post']), + ]); + + $video = Video::create([ + 'title' => 'Video', + 'url' => 'Video chulo' + ])->comments()->saveMany([ + new Comment(['body' => 'comentario video']), + ]); + + return $this; + } + + public function testPolymorphicRelation() + { + $this->createCommentRaw(); + + Post::first()->delete(); + } + public function testBadRelation() { $this->createUserRaw(); From c46900f49abeb2d11162dd9e520d050d0accf571 Mon Sep 17 00:00:00 2001 From: Mario Aguilar Alves Date: Wed, 16 Aug 2017 13:01:27 +0200 Subject: [PATCH 2/3] gettBelongsToManyData to getBelongsToManyData --- src/SoftCascade.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SoftCascade.php b/src/SoftCascade.php index 5d9e5e1..71ecf2e 100644 --- a/src/SoftCascade.php +++ b/src/SoftCascade.php @@ -91,7 +91,7 @@ protected function relations($model, $foreignKeyIds) //Many to many relations need to get related ids and related local key if (get_class($modelRelation) == 'Illuminate\Database\Eloquent\Relations\BelongsToMany') { - extract($this->gettBelongsToManyData($modelRelation, $foreignKeyIds)); + extract($this->getBelongsToManyData($modelRelation, $foreignKeyIds)); } $affectedRowsOnExecute = $this->affectedRowsOnExecute($modelRelation, $foreignKeyUse, $foreignKeyIdsUse); @@ -112,7 +112,7 @@ protected function relations($model, $foreignKeyIds) * @param array $foreignKeyIds * @return array */ - protected function gettBelongsToManyData($relation, $foreignKeyIds) + protected function getBelongsToManyData($relation, $foreignKeyIds) { $relationConnectionName = $relation->getConnection()->getName(); $relationTable = $relation->getTable(); From 91ec5b951d10aac81e1bb919b26243d8ad49cfb0 Mon Sep 17 00:00:00 2001 From: Mario Aguilar Alves Date: Wed, 16 Aug 2017 13:35:50 +0200 Subject: [PATCH 3/3] Added support to Polymorphic relations --- src/SoftCascade.php | 31 +++++++++++++++++++++++++++++-- tests/App/Comment.php | 2 ++ tests/App/Post.php | 1 + tests/App/Video.php | 1 + tests/IntegrationTest.php | 3 +++ tests/LumenIntegrationTest.php | 3 +++ 6 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/SoftCascade.php b/src/SoftCascade.php index 71ecf2e..9522c37 100644 --- a/src/SoftCascade.php +++ b/src/SoftCascade.php @@ -90,12 +90,15 @@ protected function relations($model, $foreignKeyIds) $foreignKeyIdsUse = $foreignKeyIds; //Many to many relations need to get related ids and related local key - if (get_class($modelRelation) == 'Illuminate\Database\Eloquent\Relations\BelongsToMany') { + $classModelRelation = get_class($modelRelation); + if ($classModelRelation == 'Illuminate\Database\Eloquent\Relations\BelongsToMany') { extract($this->getBelongsToManyData($modelRelation, $foreignKeyIds)); + } else if ($classModelRelation == 'Illuminate\Database\Eloquent\Relations\MorphMany') { + extract($this->getMorphManyData($modelRelation, $foreignKeyIds)); } $affectedRowsOnExecute = $this->affectedRowsOnExecute($modelRelation, $foreignKeyUse, $foreignKeyIdsUse); - + if ($action === 'restrict' && $affectedRowsOnExecute > 0) { DB::rollBack(); //Rollback the transaction before throw exception throw (new SoftCascadeRestrictedException)->setModel(get_class($modelRelation->getModel()), $foreignKeyUse, $foreignKeyIdsUse->toArray()); @@ -133,6 +136,30 @@ protected function getBelongsToManyData($relation, $foreignKeyIds) ]; } + /** + * Get morph many related key ids and key use + * + * @param Illuminate\Database\Eloquent\Relations\Relation $relation + * @param array $foreignKeyIds + * @return array + */ + protected function getMorphManyData($relation, $foreignKeyIds) + { + $relationConnectionName = $relation->getConnection()->getName(); + $relatedClass = $relation->getRelated(); + $foreignKeyUse = $relatedClass->getKeyName(); + $foreignKeyIdsUse = $relatedClass::where($relation->getMorphType(), $relation->getMorphClass()) + ->whereIn($relation->getQualifiedForeignKeyName(), $foreignKeyIds) + ->select($foreignKeyUse) + ->get()->toArray(); + $foreignKeyIdsUse = array_column($foreignKeyIdsUse, $foreignKeyUse); + + return [ + 'foreignKeyIdsUse' => collect($foreignKeyIdsUse), + 'foreignKeyUse' => $relation->getRelated()->getKeyName() + ]; + } + /** * Execute delete, or restore. * diff --git a/tests/App/Comment.php b/tests/App/Comment.php index 148bb0f..63a1d78 100644 --- a/tests/App/Comment.php +++ b/tests/App/Comment.php @@ -6,6 +6,8 @@ class Comment extends Model { + use \Illuminate\Database\Eloquent\SoftDeletes; + protected $table = 'comments'; protected $fillable = ['body']; diff --git a/tests/App/Post.php b/tests/App/Post.php index 14008af..3e75bbd 100644 --- a/tests/App/Post.php +++ b/tests/App/Post.php @@ -8,6 +8,7 @@ class Post extends Model { use \Illuminate\Database\Eloquent\SoftDeletes; use \Askedio\SoftCascade\Traits\SoftCascadeTrait; + use \Illuminate\Database\Eloquent\SoftDeletes; protected $table = 'posts'; diff --git a/tests/App/Video.php b/tests/App/Video.php index 382e037..6cf6b47 100644 --- a/tests/App/Video.php +++ b/tests/App/Video.php @@ -8,6 +8,7 @@ class Video extends Model { use \Illuminate\Database\Eloquent\SoftDeletes; use \Askedio\SoftCascade\Traits\SoftCascadeTrait; + use \Illuminate\Database\Eloquent\SoftDeletes; protected $table = 'videos'; diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index bb55c8a..c4a5ad3 100755 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -69,6 +69,9 @@ public function testPolymorphicRelation() $this->createCommentRaw(); Post::first()->delete(); + + $this->assertDatabaseHas('videos', ['deleted_at' => null]); + $this->assertDatabaseMissing('posts', ['deleted_at' => null]); } public function testBadRelation() diff --git a/tests/LumenIntegrationTest.php b/tests/LumenIntegrationTest.php index 9e9b354..e1a6519 100644 --- a/tests/LumenIntegrationTest.php +++ b/tests/LumenIntegrationTest.php @@ -73,6 +73,9 @@ public function testPolymorphicRelation() $this->createCommentRaw(); Post::first()->delete(); + + $this->assertDatabaseHas('videos', ['deleted_at' => null]); + $this->assertDatabaseMissing('posts', ['deleted_at' => null]); } public function testBadRelation()