CaseExpressionTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 2005-2013, 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. * Test that the sql output works correctly
  25. *
  26. * @return void
  27. */
  28. public function testSqlOutput() {
  29. $expr = new QueryExpression();
  30. $expr->eq('test', 'true');
  31. $caseExpression = new CaseExpression($expr, 'foobar');
  32. $expected = 'CASE WHEN test = :c0 THEN :c1 END';
  33. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  34. $expr2 = new QueryExpression();
  35. $expr2->eq('test2', 'false');
  36. $caseExpression->add($expr2);
  37. $expected = 'CASE WHEN test = :c0 THEN :c1 WHEN test2 = :c2 THEN :c3 END';
  38. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  39. $caseExpression = new CaseExpression([$expr], ['foobar', 'else']);
  40. $expected = 'CASE WHEN test = :c0 THEN :c1 ELSE :c2 END';
  41. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  42. }
  43. /**
  44. * Tests that the expression is correctly traversed
  45. *
  46. * @return void
  47. */
  48. public function testTraverse() {
  49. $count = 0;
  50. $visitor = function () use (&$count) {
  51. $count++;
  52. };
  53. $expr = new QueryExpression();
  54. $expr->eq('test', 'true');
  55. $expr2 = new QueryExpression();
  56. $expr2->eq('test', 'false');
  57. $caseExpression = new CaseExpression([$expr, $expr2]);
  58. $caseExpression->traverse($visitor);
  59. $this->assertSame(4, $count);
  60. }
  61. }