Skip to content

Commit

Permalink
Adding test for query defaults in HasMany
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 20, 2013
1 parent 323e64e commit 4bb2613
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/Cake/ORM/Association/HasMany.php
Expand Up @@ -138,7 +138,7 @@ public function eagerLoader($results, $options = []) {
*/
protected function _options(array $opts) {
if (isset($opts['sort'])) {
$this->order($opts['sort']);
$this->sort($opts['sort']);
}
}

Expand Down
50 changes: 47 additions & 3 deletions lib/Cake/Test/TestCase/ORM/Association/HasManyTest.php
Expand Up @@ -21,7 +21,7 @@
use Cake\ORM\Query;

/**
* Tests HasOne class
* Tests HasMany class
*
*/
class HasOneTest extends \Cake\TestSuite\TestCase {
Expand Down Expand Up @@ -87,8 +87,7 @@ public function testSort() {
public function testEagerLoader() {
$config = [
'sourceTable' => $this->author,
'targetTable' => $this->article,
'conditions' => ['Article.is_active' => true]
'targetTable' => $this->article
];
$association = new HasMany('Article', $config);
$keys = [1, 2, 3, 4];
Expand Down Expand Up @@ -117,4 +116,49 @@ public function testEagerLoader() {
];
$this->assertEquals($row, $result);
}

/**
* Test the eager loader method with default query clauses
*
* @return void
*/
public function testEagerLoaderWithDefaults() {
$config = [
'sourceTable' => $this->author,
'targetTable' => $this->article,
'conditions' => ['Article.is_active' => true],
'sort' => ['id' => 'ASC']
];
$association = new HasMany('Article', $config);
$keys = [1, 2, 3, 4];
$query = $this->getMock(
'Cake\ORM\Query',
['execute', 'where', 'andWhere', 'order'],
[null]
);
$this->article->expects($this->once())->method('find')->with('all')
->will($this->returnValue($query));
$results = [
['id' => 1, 'title' => 'article 1', 'author_id' => 2],
['id' => 2, 'title' => 'article 2', 'author_id' => 1]
];

$query->expects($this->once())->method('execute')
->will($this->returnValue($results));

$query->expects($this->once())->method('where')
->with(['Article.is_active' => true])
->will($this->returnValue($query));

$query->expects($this->once())->method('andWhere')
->with(['Article.author_id in' => $keys])
->will($this->returnValue($query));

$query->expects($this->once())->method('order')
->with(['id' => 'ASC'])
->will($this->returnValue($query));

$association->eagerLoader($keys);
}

}

0 comments on commit 4bb2613

Please sign in to comment.