/
CaseExpressionTest.php
68 lines (61 loc) · 2.08 KB
/
CaseExpressionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2013, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Database\Expression;
use Cake\Database\Expression\CaseExpression;
use Cake\Database\Expression\QueryExpression;
use Cake\Database\ValueBinder;
use Cake\TestSuite\TestCase;
/**
* Tests CaseExpression class
*/
class CaseExpressionTest extends TestCase {
/**
* Test that the sql output works correctly
*
* @return void
*/
public function testSqlOutput() {
$expr = new QueryExpression();
$expr->eq('test', 'true');
$caseExpression = new CaseExpression($expr, 'foobar');
$expected = 'CASE WHEN test = :c0 THEN :c1 END';
$this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
$expr2 = new QueryExpression();
$expr2->eq('test2', 'false');
$caseExpression->add($expr2);
$expected = 'CASE WHEN test = :c0 THEN :c1 WHEN test2 = :c2 THEN :c3 END';
$this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
$caseExpression = new CaseExpression([$expr], ['foobar', 'else']);
$expected = 'CASE WHEN test = :c0 THEN :c1 ELSE :c2 END';
$this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
}
/**
* Tests that the expression is correctly traversed
*
* @return void
*/
public function testTraverse() {
$count = 0;
$visitor = function () use (&$count) {
$count++;
};
$expr = new QueryExpression();
$expr->eq('test', 'true');
$expr2 = new QueryExpression();
$expr2->eq('test', 'false');
$caseExpression = new CaseExpression([$expr, $expr2]);
$caseExpression->traverse($visitor);
$this->assertSame(4, $count);
}
}