CaseExpressionTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\QueryExpression;
  16. use Cake\Database\ValueBinder;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\Database\Expression\CaseExpression;
  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);
  32. $this->assertInstanceOf('Cake\Database\ExpressionInterface', $caseExpression);
  33. $expected = 'CASE WHEN test = :c0 THEN 1 ELSE 0 END';
  34. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  35. }
  36. /**
  37. * Test that we can pass in things as the isTrue/isFalse part
  38. *
  39. * @return void
  40. */
  41. public function testSetTrue() {
  42. $expr = new QueryExpression();
  43. $expr->eq('test', 'true');
  44. $caseExpression = new CaseExpression($expr);
  45. $expr2 = new QueryExpression();
  46. $caseExpression->isTrue($expr2);
  47. $this->assertSame($expr2, $caseExpression->isTrue());
  48. $caseExpression->isTrue('test_string');
  49. $this->assertSame(['value' => 'test_string', 'type' => null], $caseExpression->isTrue());
  50. $caseExpression->isTrue(['test_string' => 'literal']);
  51. $this->assertSame('test_string', $caseExpression->isTrue());
  52. }
  53. /**
  54. * Test that things are compiled correctly
  55. *
  56. * @return void
  57. */
  58. public function testSqlCompiler() {
  59. $expr = new QueryExpression();
  60. $expr->eq('test', 'true');
  61. $caseExpression = new CaseExpression($expr);
  62. $expr2 = new QueryExpression();
  63. $expr2->eq('test', 'false');
  64. $caseExpression->isTrue($expr2);
  65. $this->assertSame('CASE WHEN test = :c0 THEN test = :c1 ELSE 0 END', $caseExpression->sql(new ValueBinder()));
  66. $caseExpression->isTrue('test_string');
  67. $this->assertSame('CASE WHEN test = :c0 THEN :c1 ELSE 0 END', $caseExpression->sql(new ValueBinder()));
  68. $caseExpression->isTrue(['test_string' => 'literal']);
  69. $this->assertSame('CASE WHEN test = :c0 THEN test_string ELSE 0 END', $caseExpression->sql(new ValueBinder()));
  70. }
  71. /**
  72. * Tests that the expression is correctly traversed
  73. *
  74. * @return void
  75. */
  76. public function testTraverse() {
  77. $count = 0;
  78. $visitor = function() use (&$count) {
  79. $count++;
  80. };
  81. $expr = new QueryExpression();
  82. $expr->eq('test', 'true');
  83. $caseExpression = new CaseExpression($expr);
  84. $caseExpression->traverse($visitor);
  85. $this->assertSame(2, $count);
  86. }
  87. }