Skip to content

Commit

Permalink
Adding a couple extra tests to HasMany
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 20, 2013
1 parent 4bb2613 commit 9485413
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 22 deletions.
85 changes: 85 additions & 0 deletions lib/Cake/Test/TestCase/ORM/Association/HasManyTest.php
Expand Up @@ -161,4 +161,89 @@ public function testEagerLoaderWithDefaults() {
$association->eagerLoader($keys);
}

/**
* Test the eager loader method with overridden query clauses
*
* @return void
*/
public function testEagerLoaderWithOverrides() {
$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', 'select'],
[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, 'Article.id !=' => 3])
->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(['title' => 'DESC'])
->will($this->returnValue($query));

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

$query->expects($this->once())->method('select')
->with([
'Article__title' => 'Article.title',
'Article__author_id' => 'Article.author_id'
])
->will($this->returnValue($query));

$association->eagerLoader($keys, [
'conditions' => ['Article.id !=' => 3],
'sort' => ['title' => 'DESC'],
'fields' => ['title', 'author_id']
]);
}

/**
* Test that failing to add the foreignKey to the list of fields will throw an
* exception
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage You are required to select the "Article.author_id"
* @return void
*/
public function testEagerLoaderFieldsException() {
$config = [
'sourceTable' => $this->author,
'targetTable' => $this->article,
];
$association = new HasMany('Article', $config);
$keys = [1, 2, 3, 4];
$query = $this->getMock(
'Cake\ORM\Query',
['execute'],
[null]
);
$this->article->expects($this->once())->method('find')->with('all')
->will($this->returnValue($query));

$association->eagerLoader($keys, ['fields' => ['id', 'title']]);
}

}
22 changes: 0 additions & 22 deletions lib/Cake/Test/TestCase/ORM/QueryTest.php
Expand Up @@ -393,28 +393,6 @@ public function testHasManyEagerLoadingFields() {
$this->assertEquals($expected, $results);
}

/**
* Tests that not selecting the foreignKey for a hasMany association will
* throw an exception
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage You are required to select the "article.author_id"
* @return void
**/
public function testHasManyEagerLoadingFieldsError() {
$this->_insertTwoRecords();

$query = new Query($this->connection);
$table = Table::build('author', ['connection' => $this->connection]);
Table::build('article', ['connection' => $this->connection]);
$table->hasMany('article', ['property' => 'articles']);

$results = $query->repository($table)
->select()
->contain(['article' => ['fields' => ['title']]])
->toArray();
}

/**
* Tests that it is possible to set an order in a hasMany result set
*
Expand Down

0 comments on commit 9485413

Please sign in to comment.