Skip to content

Commit

Permalink
Adding test for having()
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 10, 2013
1 parent 71cc1d2 commit 667ed5a
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php
Expand Up @@ -1089,4 +1089,39 @@ public function testSelectDistinct() {
$this->assertCount(2, $result);
}

/**
* Tests that having() behaves pretty much the same as the where() method
*
* @return void
**/
public function testSelectHaving() {
$statement = $this->_insertTwoRecords();
$statement->bindValue(1, 3);
$statement->bindValue(2, 'another title');
$statement->bindValue(3, 'another body');
$statement->bindValue(4, 2);
$statement->execute();

$query = new Query($this->connection);
$result = $query
->select(['total' => 'count(author_id)', 'author_id'])
->from('articles')
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => 'author_id = a.id'])
->group('author_id')
->having(['total <' => 2], ['total' => 'integer'])
->execute();
$expected = [['total' => 1, 'author_id' => 1]];
$this->assertEquals($expected, $result->fetchAll('assoc'));

$result = $query->having(['total' => 2], ['total' => 'integer'], true)
->execute();
$expected = [['total' => 2, 'author_id' => 2]];
$this->assertEquals($expected, $result->fetchAll('assoc'));

$result = $query->having(function($e) { return $e->add('total = 1 + 1'); }, [], true)
->execute();
$expected = [['total' => 2, 'author_id' => 2]];
$this->assertEquals($expected, $result->fetchAll('assoc'));
}

}

0 comments on commit 667ed5a

Please sign in to comment.