Browse Source

Using Collections instead of pure arrays.Fixed wrong property name in TableTest.

Luan Hospodarsky 10 years ago
parent
commit
942784984d
2 changed files with 41 additions and 51 deletions
  1. 11 9
      src/ORM/Association/HasMany.php
  2. 30 42
      tests/TestCase/ORM/TableTest.php

+ 11 - 9
src/ORM/Association/HasMany.php

@@ -15,6 +15,7 @@
  */
 namespace Cake\ORM\Association;
 
+use Cake\Collection\Collection;
 use Cake\Datasource\EntityInterface;
 use Cake\ORM\Association;
 use Cake\ORM\Table;
@@ -190,21 +191,22 @@ class HasMany extends Association
      * @param array $remainingEntities Entities that should not be deleted
      * @return void
      */
-    protected function _unlinkAssociated(array $properties, EntityInterface $entity, Table $target, array $remainingEntities)
+    protected function _unlinkAssociated(array $properties, EntityInterface $entity, Table $target, array $remainingEntities = [])
     {
         $primaryKey = (array)$target->primaryKey();
         $mustBeDependent = (!$this->_foreignKeyAcceptsNull($target, $properties) || $this->dependent());
-        $exclusions = array_filter(
-            array_map(
-                function ($ent) use ($primaryKey) {
-                    return $ent->extract($primaryKey);
-                },
-                $remainingEntities
-            ),
+        $exclusions = new Collection($remainingEntities);
+        $exclusions = $exclusions->map(
+            function ($ent) use ($primaryKey) {
+                return $ent->extract($primaryKey);
+            }
+        )
+        ->filter(
             function ($v) {
                 return !in_array(null, array_values($v), true);
             }
-        );
+        )
+        ->toArray();
 
         if (count($exclusions) > 0) {
             $conditions = [

+ 30 - 42
tests/TestCase/ORM/TableTest.php

@@ -1745,17 +1745,15 @@ class TableTest extends TestCase
      */
     public function testSaveReplaceSaveStrategy()
     {
-        $authors = $this->getMock(
-            'Cake\ORM\Table',
-            ['exists'],
+        $authors = new Table(
             [
-                [
-                    'connection' => $this->connection,
-                    'alias' => 'Authors',
-                    'table' => 'authors',
-                ]
+                'table' => 'authors',
+                'alias' => 'Authors',
+                'connection' => $this->connection,
+                'entityClass' => 'Cake\ORM\Entity',
             ]
         );
+        
         $authors->hasMany('Articles', ['saveStrategy' => 'replace']);
 
         $entity = $authors->newEntity([
@@ -1781,7 +1779,6 @@ class TableTest extends TestCase
         $this->assertTrue($authors->Articles->exists(['id' => $articleId]));
     }
 
-
     /**
      * Test that save works with append saveStrategy not deleting or setting null anything
      *
@@ -1789,17 +1786,15 @@ class TableTest extends TestCase
      */
     public function testSaveAppendSaveStrategy()
     {
-        $authors = $this->getMock(
-            'Cake\ORM\Table',
-            ['exists'],
+        $authors = new Table(
             [
-                [
-                    'connection' => $this->connection,
-                    'alias' => 'Authors',
-                    'table' => 'authors',
-                ]
+                'table' => 'authors',
+                'alias' => 'Authors',
+                'connection' => $this->connection,
+                'entityClass' => 'Cake\ORM\Entity',
             ]
         );
+
         $authors->hasMany('Articles', ['saveStrategy' => 'append']);
 
         $entity = $authors->newEntity([
@@ -1825,6 +1820,7 @@ class TableTest extends TestCase
         $this->assertEquals($sizeArticles, $authors->Articles->find('all')->where(['author_id' => $entity['id']])->count());
         $this->assertTrue($authors->Articles->exists(['id' => $articleId]));
     }
+
     /**
      * Test that save has append as the default save strategy
      *
@@ -1851,17 +1847,15 @@ class TableTest extends TestCase
      */
     public function testSaveReplaceSaveStrategyDependent()
     {
-        $authors = $this->getMock(
-            'Cake\ORM\Table',
-            ['exists'],
+        $authors = new Table(
             [
-                [
-                    'connection' => $this->connection,
-                    'alias' => 'Authors',
-                    'table' => 'authors',
-                ]
+                'table' => 'authors',
+                'alias' => 'Authors',
+                'connection' => $this->connection,
+                'entityClass' => 'Cake\ORM\Entity',
             ]
         );
+
         $authors->hasMany('Articles', ['saveStrategy' => 'replace', 'dependent' => true]);
 
         $entity = $authors->newEntity([
@@ -1894,15 +1888,12 @@ class TableTest extends TestCase
      */
     public function testSaveReplaceSaveStrategyNotNullable()
     {
-        $articles = $this->getMock(
-            'Cake\ORM\Table',
-            ['exists'],
+        $articles = new Table(
             [
-                [
-                    'connection' => $this->connection,
-                    'alias' => 'Articles',
-                    'table' => 'articles',
-                ]
+                'table' => 'articles',
+                'alias' => 'Articles',
+                'connection' => $this->connection,
+                'entityClass' => 'Cake\ORM\Entity',
             ]
         );
 
@@ -1945,15 +1936,12 @@ class TableTest extends TestCase
      */
     public function testSaveReplaceSaveStrategyAdding()
     {
-        $articles = $this->getMock(
-            'Cake\ORM\Table',
-            ['exists'],
+        $articles = new Table(
             [
-                [
-                    'connection' => $this->connection,
-                    'alias' => 'Articles',
-                    'table' => 'articles',
-                ]
+                'table' => 'articles',
+                'alias' => 'Articles',
+                'connection' => $this->connection,
+                'entityClass' => 'Cake\ORM\Entity',
             ]
         );
 
@@ -1983,7 +1971,7 @@ class TableTest extends TestCase
         $this->assertTrue($articles->Comments->exists(['id' => $commentId]));
         
         unset($article->comments[0]);
-        $article->comments[] = $articles->comments->newEntity([
+        $article->comments[] = $articles->Comments->newEntity([
             'user_id' => 1,
             'comment' => 'new comment'
         ]);