Skip to content

Commit

Permalink
Added missing test for attachTo
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 4, 2014
1 parent 17287a6 commit afa83e3
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Cake/Test/TestCase/ORM/Association/HasManyTest.php
Expand Up @@ -457,6 +457,43 @@ public function testAttachToNoFields() {
$association->attachTo($query, ['includeFields' => false]);
}

/**
* Tests that by supplying a query builder function, it is possible to add fields
* and conditions to an association
*
* @return void
*/
public function testAttachToWithQueryBuilder() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'sourceTable' => $this->author,
'targetTable' => $this->article,
'conditions' => ['Articles.is_active' => true]
];
$field = new IdentifierExpression('Articles.author_id');
$association = new HasMany('Articles', $config);
$query->expects($this->once())->method('join')->with([
'Articles' => [
'conditions' => new QueryExpression([
'Articles.is_active' => true,
['Authors.id' => $field],
new QueryExpression(['a' => 1])
]),
'type' => 'INNER',
'table' => 'articles'
]
]);
$query->expects($this->once())->method('select')
->with([
'Articles__a' => 'Articles.a',
'Articles__b' => 'Articles.b'
]);
$builder = function($q) {
return $q->select(['a', 'b'])->where(['a' => 1]);
};
$association->attachTo($query, ['queryBuilder' => $builder]);
}

/**
* Test cascading deletes.
*
Expand Down

0 comments on commit afa83e3

Please sign in to comment.