Browse Source

Adding tests for bindingKey in HasMany

Jose Lorenzo Rodriguez 10 years ago
parent
commit
eea020b899
1 changed files with 36 additions and 1 deletions
  1. 36 1
      tests/TestCase/ORM/BindingKeyTest.php

+ 36 - 1
tests/TestCase/ORM/BindingKeyTest.php

@@ -18,7 +18,7 @@ use Cake\ORM\TableRegistry;
 use Cake\TestSuite\TestCase;
 
 /**
- * Integration tetss for table operations involving composite keys
+ * Integration tests for usinge the bindingKey in associations
  */
 class BindingKeyTest extends TestCase
 {
@@ -45,6 +45,16 @@ class BindingKeyTest extends TestCase
     }
 
     /**
+     * Data provider for the two types of strategies HasMany and BelongsToMany implements
+     *
+     * @return void
+     */
+    public function strategiesProviderExternal()
+    {
+        return [['subquery'], ['select']];
+    }
+
+    /**
      * Tests that bindingKey can be used in belongsTo associations
      *
      * @dataProvider strategiesProviderJoinable
@@ -98,4 +108,29 @@ class BindingKeyTest extends TestCase
 
         $this->assertEquals(3, $result->site_author->id);
     }
+
+    /**
+     * Tests that bindingKey can be used in hasOne associations
+     *
+     * @dataProvider strategiesProviderExternal
+     * @return void
+     */
+    public function testHasMany($strategy)
+    {
+        $users = TableRegistry::get('Users');
+        $authors = $users->hasMany('SiteAuthors', [
+            'bindingKey' => 'username',
+            'foreignKey' => 'name',
+            'strategy' => $strategy
+        ]);
+
+        $authors->updateAll(['name' => 'garrett'], ['id >' => 2]);
+        $result = $users->find()
+            ->contain(['SiteAuthors'])
+            ->where(['username' => 'garrett']);
+
+        $expected = [3, 4];
+        $result = $result->extract('site_authors.{*}.id')->toList();
+        $this->assertEquals($expected, $result);
+    }
 }