Skip to content

Commit

Permalink
Adding unit test for generating conditions fir eager loading BelongsTo
Browse files Browse the repository at this point in the history
when the target table has multi column primary key
  • Loading branch information
lorenzo committed Jan 7, 2014
1 parent 1dc56e9 commit 16b1902
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Test/TestCase/ORM/Association/BelongsToTest.php
Expand Up @@ -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.