Browse Source

Merge branch 'unlink-tests-hasmany' into master

Refs #9323
Mark Story 9 years ago
parent
commit
5f77bac34d
2 changed files with 55 additions and 1 deletions
  1. 3 0
      src/ORM/Association/HasMany.php
  2. 52 1
      tests/TestCase/ORM/Association/HasManyTest.php

+ 3 - 0
src/ORM/Association/HasMany.php

@@ -293,6 +293,9 @@ class HasMany extends Association
         } else {
             $options += ['cleanProperty' => true];
         }
+        if (count($targetEntities) === 0) {
+            return;
+        }
 
         $foreignKey = (array)$this->foreignKey();
         $target = $this->target();

+ 52 - 1
tests/TestCase/ORM/Association/HasManyTest.php

@@ -49,7 +49,7 @@ class HasManyTest extends TestCase
         $this->author = TableRegistry::get('Authors', [
             'schema' => [
                 'id' => ['type' => 'integer'],
-                'username' => ['type' => 'string'],
+                'name' => ['type' => 'string'],
                 '_constraints' => [
                     'primary' => ['type' => 'primary', 'columns' => ['id']]
                 ]
@@ -642,4 +642,55 @@ class HasManyTest extends TestCase
         }
         $this->assertEquals($expected, $query->clause('select'));
     }
+
+    /**
+     * Tests that unlinking calls the right methods
+     *
+     * @return void
+     */
+    public function testUnlinkSuccess()
+    {
+        $articles = TableRegistry::get('Articles');
+        $assoc = $this->author->hasMany('Articles', [
+            'sourceTable' => $this->author,
+            'targetTable' => $articles
+        ]);
+
+        $entity = $this->author->get(1, ['contain' => 'Articles']);
+        $initial = $entity->articles;
+        $this->assertCount(2, $initial);
+
+        $assoc->unlink($entity, $entity->articles);
+        $this->assertEmpty($entity->get('articles'), 'Property should be empty');
+
+        $new = $this->author->get(2, ['contain' => 'Articles']);
+        $this->assertCount(0, $new->articles, 'DB should be clean');
+        $this->assertSame(4, $this->author->find()->count(), 'Authors should still exist');
+        $this->assertSame(3, $articles->find()->count(), 'Articles should still exist');
+    }
+
+    /**
+     * Tests that unlink with an empty array does nothing
+     *
+     * @return void
+     */
+    public function testUnlinkWithEmptyArray()
+    {
+        $articles = TableRegistry::get('Articles');
+        $assoc = $this->author->hasMany('Articles', [
+            'sourceTable' => $this->author,
+            'targetTable' => $articles
+        ]);
+
+        $entity = $this->author->get(1, ['contain' => 'Articles']);
+        $initial = $entity->articles;
+        $this->assertCount(2, $initial);
+
+        $assoc->unlink($entity, []);
+
+        $new = $this->author->get(1, ['contain' => 'Articles']);
+        $this->assertCount(2, $new->articles, 'Articles should remain linked');
+        $this->assertSame(4, $this->author->find()->count(), 'Authors should still exist');
+        $this->assertSame(3, $articles->find()->count(), 'Articles should still exist');
+    }
 }