Skip to content

Commit

Permalink
Add integration test for the CaseExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
Walther Lalk committed Aug 11, 2014
1 parent c053eb4 commit 820f522
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -2716,6 +2716,77 @@ public function testDirectIsNull() {
$this->assertEquals(['name' => 'larry'], $results->fetch('assoc'));
}

/**
* Tests that case statements work correctly for various use-cases.
*
* @return void
*/
public function testSqlCaseStatement() {
$query = new Query($this->connection);
$publishedCase = $query
->newExpr()
->addCase($query
->newExpr()
->add(['published' => 'Y'])
);
$notPublishedCase = $query
->newExpr()
->addCase($query
->newExpr()
->add(['published' => 'N'])
);
$results = $query
->select([
'published' => $query->func()->sum($publishedCase),
'not_published' => $query->func()->sum($notPublishedCase)
])
->from(['comments'])
->execute()
->fetchAll('assoc');

$this->assertEquals(5, $results[0]['published']);
$this->assertEquals(1, $results[0]['not_published']);

$query = new Query($this->connection);
$query
->insert(['article_id', 'user_id', 'comment', 'published'])
->into('comments')
->values([
'article_id' => 2,
'user_id' => 1,
'comment' => 'In limbo',
'published' => 'L'
])
->execute();

$query = new Query($this->connection);
$conditions = [
$query
->newExpr()
->add(['published' => 'Y']),
$query
->newExpr()
->add(['published' => 'N'])
];
$trueValues = [
'Published',
'Not published'
];
$results = $query
->select([
'id',
'comment',
'status' => $query->newExpr()->addCase($conditions, $trueValues, 'None')
])
->from(['comments'])
->execute()
->fetchAll('assoc');

$this->assertEquals('Published', $results[2]['status']);
$this->assertEquals('Not published', $results[3]['status']);
$this->assertEquals('None', $results[6]['status']);
}

/**
* Assertion for comparing a table's contents with what is in it.
*
Expand Down

0 comments on commit 820f522

Please sign in to comment.