Skip to content

Commit

Permalink
Fix failing test on SqlServer
Browse files Browse the repository at this point in the history
  • Loading branch information
Walther Lalk committed Aug 11, 2014
1 parent 820f522 commit b8c00d4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/Database/Expression/CaseExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class CaseExpression implements ExpressionInterface {
* @param string|array|ExpressionInterface $trueValues Value of each condition if that condition is true
* @param string|array|ExpressionInterface $defaultValue Default value if none of the conditiosn are true
*/
public function __construct($conditions = [], $trueValues = [], $defaultValue = '0') {
public function __construct($conditions = [], $trueValues = [], $defaultValue = 0) {
if (!empty($conditions)) {
$this->add($conditions, $trueValues);
}
Expand Down Expand Up @@ -108,7 +108,7 @@ protected function _addExpressions($conditions, $trueValues) {
continue;
}

$trueValue = isset($trueValues[$k]) ? $trueValues[$k] : 1;
$trueValue = !empty($trueValues[$k]) ? $trueValues[$k] : 1;

if ($trueValue === 'literal') {
$trueValue = $k;
Expand All @@ -117,8 +117,6 @@ protected function _addExpressions($conditions, $trueValues) {
'value' => $trueValue,
'type' => null
];
} elseif (empty($trueValue)) {
$trueValue = 1;
}

$this->_conditions[] = $c;
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Expression/QueryExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public function in($field, $values, $type = null) {
*
* @return QueryExpression
*/
public function addCase($conditions, $trueValues = [], $defaultValue = '0') {
public function addCase($conditions, $trueValues = [], $defaultValue = 0) {
return $this->add(new CaseExpression($conditions, $trueValues, $defaultValue));
}

Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2722,6 +2722,8 @@ public function testDirectIsNull() {
* @return void
*/
public function testSqlCaseStatement() {
$convert = $this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver;

$query = new Query($this->connection);
$publishedCase = $query
->newExpr()
Expand All @@ -2735,6 +2737,21 @@ public function testSqlCaseStatement() {
->newExpr()
->add(['published' => 'N'])
);

//SQLServer requires the case statements to be converted to int
if ($convert) {
$publishedCase = $query->func()
->convert([
'INT' => 'literal',
$publishedCase
]);
$notPublishedCase = $query->func()
->convert([
'INT' => 'literal',
$notPublishedCase
]);
}

$results = $query
->select([
'published' => $query->func()->sum($publishedCase),
Expand Down

0 comments on commit b8c00d4

Please sign in to comment.