Browse Source

Add more tests for patchEntity and patchEntities.

Include an integration test with the marshaller as only having mocks
isn't quite as thorough.

Refs #11556
mark_story 8 years ago
parent
commit
c12ae0a77d
1 changed files with 41 additions and 2 deletions
  1. 41 2
      tests/TestCase/ORM/TableTest.php

+ 41 - 2
tests/TestCase/ORM/TableTest.php

@@ -5654,7 +5654,7 @@ class TableTest extends TestCase
      *
      * @return void
      */
-    public function testPatchEntity()
+    public function testPatchEntityMarshallerUsage()
     {
         $table = $this->getMockBuilder('Cake\ORM\Table')
             ->setMethods(['marshaller'])
@@ -5677,12 +5677,29 @@ class TableTest extends TestCase
     }
 
     /**
+     * Tests patchEntity in a simple scenario. The tests for Marshaller cover
+     * patch scenarios in more depth.
+     *
+     * @return void
+     */
+    public function testPatchEntity()
+    {
+        $table = TableRegistry::get('Articles');
+        $entity = new Entity(['title' => 'old title'], ['markNew' => false]);
+        $data = ['title' => 'new title'];
+        $entity = $table->patchEntity($entity, $data);
+
+        $this->assertSame($data['title'], $entity->title);
+        $this->assertFalse($entity->isNew(), 'entity should not be new.');
+    }
+
+    /**
      * Tests that patchEntities delegates the task to the marshaller and passed
      * all associations
      *
      * @return void
      */
-    public function testPatchEntities()
+    public function testPatchEntitiesMarshallerUsage()
     {
         $table = $this->getMockBuilder('Cake\ORM\Table')
             ->setMethods(['marshaller'])
@@ -5705,6 +5722,28 @@ class TableTest extends TestCase
     }
 
     /**
+     * Tests patchEntities in a simple scenario. The tests for Marshaller cover
+     * patch scenarios in more depth.
+     *
+     * @return void
+     */
+    public function testPatchEntities()
+    {
+        $table = TableRegistry::get('Articles');
+        $entities = $table->find()->limit(2)->toArray();
+
+        $data = [
+            ['id' => $entities[0]->id, 'title' => 'new title'],
+            ['id' => $entities[1]->id, 'title' => 'new title2'],
+        ];
+        $entities = $table->patchEntities($entities, $data);
+        foreach ($entities as $i => $entity) {
+            $this->assertFalse($entity->isNew(), 'entities should not be new.');
+            $this->assertSame($data[$i]['title'], $entity->title);
+        }
+    }
+
+    /**
      * Tests __debugInfo
      *
      * @return void