Browse Source

Adding PHPDoc for testReplaceHasMany method in test case. Renaming method replaceLinks to replace on HasMany.php. Grouping together HasMany tests.

Luan Hospodarsky 10 years ago
parent
commit
c1fa2bf05a
2 changed files with 61 additions and 55 deletions
  1. 1 1
      src/ORM/Association/HasMany.php
  2. 60 54
      tests/TestCase/ORM/TableTest.php

+ 1 - 1
src/ORM/Association/HasMany.php

@@ -343,7 +343,7 @@ class HasMany extends Association
      * any of them is lacking a primary key value
      * @return bool success
      */
-    public function replaceLinks(EntityInterface $sourceEntity, array $targetEntities, array $options = [])
+    public function replace(EntityInterface $sourceEntity, array $targetEntities, array $options = [])
     {
         $property = $this->property();
         $sourceEntity->set($property, $targetEntities);

+ 60 - 54
tests/TestCase/ORM/TableTest.php

@@ -4125,6 +4125,66 @@ class TableTest extends TestCase
     }
 
     /**
+     * Integration test for replacing entities with HasMany.
+     *
+     * @return void
+     */
+    public function testReplaceHasMany()
+    {
+        $authors = TableRegistry::get('Authors');
+        $articles = TableRegistry::get('Articles');
+
+        $authors->hasMany('Articles', [
+            'foreignKey' => 'author_id'
+        ]);
+
+        $author = $authors->newEntity(['name' => 'mylux']);
+        $author = $authors->save($author);
+
+        $newArticles = $articles->newEntities(
+            [
+                [
+                    'title' => 'New bakery next corner',
+                    'body' => 'They sell tastefull cakes'
+                ],
+                [
+                    'title' => 'Spicy cake recipe',
+                    'body' => 'chocolate and peppers'
+                ]
+            ]
+        );
+
+        $sizeArticles = count($newArticles);
+
+        $this->assertTrue($authors->Articles->link($author, $newArticles));
+
+        $this->assertEquals($authors->Articles->findAllByAuthorId($author->id)->count(), $sizeArticles);
+        $this->assertEquals(count($author->articles), $sizeArticles);
+
+        $newArticles = array_merge(
+            $author->articles,
+            $articles->newEntities(
+                [
+                    [
+                        'title' => 'Cheese cake recipe',
+                        'body' => 'The secrets of mixing salt and sugar'
+                    ],
+                    [
+                        'title' => 'Not another piece of cake',
+                        'body' => 'This is the best'
+                    ]
+                ]
+            )
+        );
+        unset($newArticles[0]);
+
+        $this->assertTrue($authors->Articles->replace($author, $newArticles));
+        $this->assertEquals(count($newArticles), count($author->articles));
+        $this->assertEquals((new Collection($newArticles))->extract('title'), (new Collection($author->articles))->extract('title'));
+    }
+
+
+    /**
      * Integration test to show how to unlink a single record from a belongsToMany
      *
      * @return void
@@ -4276,60 +4336,6 @@ class TableTest extends TestCase
         $this->assertEquals(3, $article->tags[1]->id);
     }
 
-    public function testReplaceLinksHasMany()
-    {
-        $authors = TableRegistry::get('Authors');
-        $articles = TableRegistry::get('Articles');
-
-        $authors->hasMany('Articles', [
-            'foreignKey' => 'author_id'
-        ]);
-
-        $author = $authors->newEntity(['name' => 'mylux']);
-        $author = $authors->save($author);
-
-        $newArticles = $articles->newEntities(
-            [
-                [
-                    'title' => 'New bakery next corner',
-                    'body' => 'They sell tastefull cakes'
-                ],
-                [
-                    'title' => 'Spicy cake recipe',
-                    'body' => 'chocolate and peppers'
-                ]
-            ]
-        );
-
-        $sizeArticles = count($newArticles);
-
-        $this->assertTrue($authors->Articles->link($author, $newArticles));
-
-        $this->assertEquals($authors->Articles->findAllByAuthorId($author->id)->count(), $sizeArticles);
-        $this->assertEquals(count($author->articles), $sizeArticles);
-
-        $newArticles = array_merge(
-            $author->articles,
-            $articles->newEntities(
-                [
-                    [
-                        'title' => 'Cheese cake recipe',
-                        'body' => 'The secrets of mixing salt and sugar'
-                    ],
-                    [
-                        'title' => 'Not another piece of cake',
-                        'body' => 'This is the best'
-                    ]
-                ]
-            )
-        );
-        unset($newArticles[0]);
-
-        $this->assertTrue($authors->Articles->replaceLinks($author, $newArticles));
-        $this->assertEquals(count($newArticles), count($author->articles));
-        $this->assertEquals((new Collection($newArticles))->extract('title'), (new Collection($author->articles))->extract('title'));
-    }
-
     /**
      * Tests that it is possible to call find with no arguments
      *