Permalink
Browse files

Adding tests for HasMany::attachTo when having multi column primary keys

  • Loading branch information...
1 parent 9e1d369 commit 175fb3cf80e53b1ecd6e786cb8e5185fb8c6fd23 @lorenzo lorenzo committed Jan 8, 2014
Showing with 59 additions and 2 deletions.
  1. +3 −2 Test/TestCase/ORM/Association/BelongsToTest.php
  2. +56 −0 Test/TestCase/ORM/Association/HasManyTest.php
@@ -258,7 +258,7 @@ public function testSaveOnlyEntities() {
}
/**
- * Tests that using belongs to with a table having a multi column primary
+ * Tests that using belongsto with a table having a multi column primary
* key will work if the foreign key is passed
*
* @return void
@@ -293,7 +293,7 @@ public function testAttachToMultiPrimaryKey() {
}
/**
- * Tests that using belongs to with a table having a multi column primary
+ * Tests that using belongsto with a table having a multi column primary
* key will work if the foreign key is passed
*
* @expectedException \RuntimeException
@@ -312,4 +312,5 @@ public function testAttachToMultiPrimaryKeyMistmatch() {
$association = new BelongsTo('Companies', $config);
$association->attachTo($query);
}
+
}
@@ -511,6 +511,61 @@ public function testAttachToNoFields() {
}
/**
+ * Tests that using hasMany with a table having a multi column primary
+ * key will work if the foreign key is passed
+ *
+ * @return void
+ */
+ public function testAttachToMultiPrimaryKey() {
+ $query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
+ $this->author->primaryKey(['id', 'site_id']);
+ $config = [
+ 'sourceTable' => $this->author,
+ 'targetTable' => $this->article,
+ 'conditions' => ['Articles.is_active' => true],
+ 'foreignKey' => ['author_id', 'author_site_id']
+ ];
+ $field1 = new IdentifierExpression('Articles.author_id');
+ $field2 = new IdentifierExpression('Articles.author_site_id');
+ $association = new HasMany('Articles', $config);
+ $query->expects($this->once())->method('join')->with([
+ 'Articles' => [
+ 'conditions' => new QueryExpression([
+ 'Articles.is_active' => true,
+ ['Authors.id' => $field1, 'Authors.site_id' => $field2]
+ ]),
+ 'type' => 'INNER',
+ 'table' => 'articles'
+ ]
+ ]);
+ $query->expects($this->never())->method('select');
+ $association->attachTo($query, ['includeFields' => false]);
+ }
+
+/**
+ * Tests that using hasMany with a table having a multi column primary
+ * key will work if the foreign key is passed
+ *
+ * @expectedException \RuntimeException
+ * @expectedExceptionMessage Cannot match provided foreignKey, got 1 columns expected 2
+ * @return void
+ */
+ public function testAttachToMultiPrimaryKeyMistmatch() {
+ $query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
+ $this->author->primaryKey(['id', 'site_id']);
+ $config = [
+ 'sourceTable' => $this->author,
+ 'targetTable' => $this->article,
+ 'conditions' => ['Articles.is_active' => true],
+ 'foreignKey' => 'author_id'
+ ];
+ $field1 = new IdentifierExpression('Articles.author_id');
+ $field2 = new IdentifierExpression('Articles.author_site_id');
+ $association = new HasMany('Articles', $config);
+ $association->attachTo($query, ['includeFields' => false]);
+ }
+
+/**
* Tests that by supplying a query builder function, it is possible to add fields
* and conditions to an association
*
@@ -649,4 +704,5 @@ public function testSaveOnlyEntities() {
$association = new HasMany('Articles', $config);
$association->save($entity);
}
+
}

0 comments on commit 175fb3c

Please sign in to comment.