CaseExpressionTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The Open Group Test Suite License
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database\Expression;
  16. use Cake\Database\Expression\CaseExpression;
  17. use Cake\Database\Expression\QueryExpression;
  18. use Cake\Database\ValueBinder;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Tests CaseExpression class
  22. */
  23. class CaseExpressionTest extends TestCase
  24. {
  25. /**
  26. * Test that the sql output works correctly
  27. *
  28. * @return void
  29. */
  30. public function testSqlOutput()
  31. {
  32. $expr = new QueryExpression();
  33. $expr->eq('test', 'true');
  34. $expr2 = new QueryExpression();
  35. $expr2->eq('test2', 'false');
  36. $caseExpression = new CaseExpression($expr, 'foobar');
  37. $expected = 'CASE WHEN test = :c0 THEN :param1 END';
  38. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  39. $caseExpression->add($expr2);
  40. $expected = 'CASE WHEN test = :c0 THEN :param1 WHEN test2 = :c2 THEN :param3 END';
  41. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  42. $caseExpression = new CaseExpression([$expr], ['foobar', 'else']);
  43. $expected = 'CASE WHEN test = :c0 THEN :param1 ELSE :param2 END';
  44. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  45. $caseExpression = new CaseExpression([$expr], ['foobar' => 'literal', 'else']);
  46. $expected = 'CASE WHEN test = :c0 THEN foobar ELSE :param1 END';
  47. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  48. }
  49. /**
  50. * Test sql generation with 0 case.
  51. *
  52. * @return void
  53. */
  54. public function testSqlOutputZero()
  55. {
  56. $expression = new QueryExpression();
  57. $expression->add(['id' => 'test']);
  58. $caseExpression = new CaseExpression([$expression], [0], ['integer']);
  59. $expected = 'CASE WHEN id = :c0 THEN :param1 END';
  60. $binder = new ValueBinder();
  61. $this->assertSame($expected, $caseExpression->sql($binder));
  62. $expected = [
  63. ':c0' => ['value' => 'test', 'type' => null, 'placeholder' => 'c0'],
  64. ':param1' => ['value' => 0, 'type' => 'integer', 'placeholder' => 'param1'],
  65. ];
  66. $this->assertEquals($expected, $binder->bindings());
  67. }
  68. /**
  69. * Tests that the expression is correctly traversed
  70. *
  71. * @return void
  72. */
  73. public function testTraverse()
  74. {
  75. $count = 0;
  76. $visitor = function () use (&$count) {
  77. $count++;
  78. };
  79. $expr = new QueryExpression();
  80. $expr->eq('test', 'true');
  81. $expr2 = new QueryExpression();
  82. $expr2->eq('test', 'false');
  83. $caseExpression = new CaseExpression([$expr, $expr2]);
  84. $caseExpression->traverse($visitor);
  85. $this->assertSame(4, $count);
  86. }
  87. /**
  88. * Test cloning
  89. *
  90. * @return void
  91. */
  92. public function testClone()
  93. {
  94. $expr = new QueryExpression();
  95. $expr->eq('test', 'true');
  96. $expr2 = new QueryExpression();
  97. $expr2->eq('test2', 'false');
  98. $caseExpression = new CaseExpression([$expr, $expr2], 'foobar');
  99. $dupe = clone $caseExpression;
  100. $dupe->elseValue('nope');
  101. $this->assertNotEquals($caseExpression, $dupe);
  102. $this->assertNotSame($caseExpression, $dupe);
  103. }
  104. }