CaseExpressionTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://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. $expr2 = new QueryExpression();
  34. $expr2->eq('test2', 'false');
  35. $caseExpression = new CaseExpression($expr, 'foobar');
  36. $expected = 'CASE WHEN test = :c0 THEN :param1 END';
  37. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  38. $caseExpression->add($expr2);
  39. $expected = 'CASE WHEN test = :c0 THEN :param1 WHEN test2 = :c2 THEN :param3 END';
  40. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  41. $caseExpression = new CaseExpression([$expr], ['foobar', 'else']);
  42. $expected = 'CASE WHEN test = :c0 THEN :param1 ELSE :param2 END';
  43. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  44. $caseExpression = new CaseExpression([$expr], ['foobar' => 'literal', 'else']);
  45. $expected = 'CASE WHEN test = :c0 THEN foobar ELSE :param1 END';
  46. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  47. }
  48. /**
  49. * Test sql generation with 0 case.
  50. *
  51. * @return void
  52. */
  53. public function testSqlOutputZero()
  54. {
  55. $expression = new QueryExpression();
  56. $expression->add(['id' => 'test']);
  57. $caseExpression = new CaseExpression([$expression], [0], ['integer']);
  58. $expected = 'CASE WHEN id = :c0 THEN :param1 END';
  59. $binder = new ValueBinder();
  60. $this->assertSame($expected, $caseExpression->sql($binder));
  61. $expected = [
  62. ':c0' => ['value' => 'test', 'type' => null, 'placeholder' => 'c0'],
  63. ':param1' => ['value' => 0, 'type' => 'integer', 'placeholder' => 'param1'],
  64. ];
  65. $this->assertEquals($expected, $binder->bindings());
  66. }
  67. /**
  68. * Tests that the expression is correctly traversed
  69. *
  70. * @return void
  71. */
  72. public function testTraverse()
  73. {
  74. $count = 0;
  75. $visitor = function () use (&$count) {
  76. $count++;
  77. };
  78. $expr = new QueryExpression();
  79. $expr->eq('test', 'true');
  80. $expr2 = new QueryExpression();
  81. $expr2->eq('test', 'false');
  82. $caseExpression = new CaseExpression([$expr, $expr2]);
  83. $caseExpression->traverse($visitor);
  84. $this->assertSame(4, $count);
  85. }
  86. /**
  87. * Test cloning
  88. *
  89. * @return void
  90. */
  91. public function testClone()
  92. {
  93. $expr = new QueryExpression();
  94. $expr->eq('test', 'true');
  95. $expr2 = new QueryExpression();
  96. $expr2->eq('test2', 'false');
  97. $caseExpression = new CaseExpression([$expr, $expr2], 'foobar');
  98. $dupe = clone $caseExpression;
  99. $dupe->elseValue('nope');
  100. $this->assertNotEquals($caseExpression, $dupe);
  101. $this->assertNotSame($caseExpression, $dupe);
  102. }
  103. }