Skip to content

Commit

Permalink
Added test for eagerLoader + queryBuilder in BelongsToMany
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 4, 2014
1 parent 368a15e commit fdab611
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
12 changes: 9 additions & 3 deletions Cake/ORM/Association/BelongsToMany.php
Expand Up @@ -247,12 +247,18 @@ public function eagerLoader(array $options) {
'foreignKey' => $this->foreignKey(),
'conditions' => [],
'sort' => $this->sort(),
'strategy' => $this->strategy()
'strategy' => $this->strategy(),
];
$fetchQuery = $this->_buildQuery($options);

$queryBuilder = false;
if (!empty($options['queryBuilder'])) {
$fetchQuery = $options['queryBuilder']($fetchQuery);
$queryBuilder = $options['queryBuilder'];
unset($options['queryBuilder']);
}

$fetchQuery = $this->_buildQuery($options);
if ($queryBuilder) {
$fetchQuery = $queryBuilder($fetchQuery);
}

$resultMap = [];
Expand Down
65 changes: 65 additions & 0 deletions Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -683,6 +683,71 @@ public function testEagerLoaderSubquery() {
$this->assertEquals($row, $result);
}

/**
* Tests eagerLoader with queryBuilder
*
* @return void
*/
public function testEagerLoaderWithQueryBuilder() {
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
];
TableRegistry::get('ArticlesTags', [
'table' => 'articles_tags',
'schema' => [
'article_id' => ['type' => 'integer'],
'tag_id' => ['type' => 'integer']
]
]);
$association = new BelongsToMany('Tags', $config);
$keys = [1, 2, 3, 4];
$query = $this->getMock(
'Cake\ORM\Query',
['all', 'contain', 'andWhere', 'limit'],
[null, null]
);

$this->tag->expects($this->once())
->method('find')
->with('all')
->will($this->returnValue($query));

$results = [
['id' => 1, 'name' => 'foo', 'articles_tags' => ['article_id' => 1]],
['id' => 2, 'name' => 'bar', 'articles_tags' => ['article_id' => 2]]
];
$query->expects($this->once())
->method('all')
->will($this->returnValue($results));

$query->expects($this->once())->method('contain')
->with([
'ArticlesTags' => [
'conditions' => ['ArticlesTags.article_id in' => $keys],
'matching' => true
]
])
->will($this->returnSelf());

$query->hydrate(false);

$query->expects($this->once())
->method('andWhere')
->with(['foo' => 1])
->will($this->returnSelf());

$query->expects($this->once())
->method('limit')
->with(1)
->will($this->returnSelf());

$queryBuilder = function($q) {
return $q->andWhere(['foo' => 1])->limit(1);
};
$association->eagerLoader(compact('keys', 'query', 'queryBuilder'));
}

/**
* Test cascading deletes.
*
Expand Down
2 changes: 1 addition & 1 deletion Cake/Test/TestCase/ORM/Association/HasManyTest.php
Expand Up @@ -411,7 +411,7 @@ public function testEagerLoaderWithQueryBuilder() {
return $query->select(['a', 'b'])->join('foo')->where(['a' => 1]);
};

$callable = $association->eagerLoader(compact('keys', 'query', 'queryBuilder'));
$association->eagerLoader(compact('keys', 'query', 'queryBuilder'));
}

/**
Expand Down

0 comments on commit fdab611

Please sign in to comment.