CaseExpressionTest.php 2.3 KB

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