Skip to content
Permalink
Browse files

Adding unit test for generating conditions fir eager loading BelongsTo

when the target table has multi column primary key
  • Loading branch information...
lorenzo committed Jan 7, 2014
1 parent 1dc56e9 commit 16b19025c6c5a43bd0a054e2d64272f9e70a9c85
Showing with 34 additions and 0 deletions.
  1. +34 −0 Test/TestCase/ORM/Association/BelongsToTest.php
@@ -257,4 +257,38 @@ public function testSaveOnlyEntities() {
$this->assertNull($entity->author_id);
}
/**
* Tests that using belongs to with a table having a multi column primary
* key will work if the foreign key is passed
*
* @return void
*/
public function testAttachToMultiPrimaryKey() {
$this->company->primaryKey(['id', 'tenant_id']);
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'foreignKey' => ['company_id', 'company_tenant_id'],
'sourceTable' => $this->client,
'targetTable' => $this->company,
'conditions' => ['Companies.is_active' => true]
];
$association = new BelongsTo('Companies', $config);
$field1 = new IdentifierExpression('Clients.company_id');
$field2 = new IdentifierExpression('Clients.company_tenant_id');
$query->expects($this->once())->method('join')->with([
'Companies' => [
'conditions' => new QueryExpression([
'Companies.is_active' => true,
['Companies.id' => $field1, 'Companies.tenant_id' => $field2]
]),
'table' => 'companies',
'type' => 'LEFT'
]
]);
$query->expects($this->once())->method('select')->with([
'Companies__id' => 'Companies.id',
'Companies__company_name' => 'Companies.company_name'
]);
$association->attachTo($query);
}
}

0 comments on commit 16b1902

Please sign in to comment.
You can’t perform that action at this time.