CaseExpressionTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. public function testSqlOutput(): void
  29. {
  30. $expr = new QueryExpression();
  31. $expr->eq('test', 'true');
  32. $expr2 = new QueryExpression();
  33. $expr2->eq('test2', 'false');
  34. $caseExpression = new CaseExpression($expr, 'foobar');
  35. $expected = 'CASE WHEN test = :c0 THEN :param1 END';
  36. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  37. $caseExpression->add($expr2);
  38. $expected = 'CASE WHEN test = :c0 THEN :param1 WHEN test2 = :c2 THEN :param3 END';
  39. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  40. $caseExpression = new CaseExpression($expr, ['foobar', 'else']);
  41. $expected = 'CASE WHEN test = :c0 THEN :param1 ELSE :param2 END';
  42. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  43. $caseExpression = new CaseExpression([$expr], ['foobar', 'else']);
  44. $expected = 'CASE WHEN test = :c0 THEN :param1 ELSE :param2 END';
  45. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  46. $caseExpression = new CaseExpression([$expr], ['foobar' => 'literal', 'else']);
  47. $expected = 'CASE WHEN test = :c0 THEN foobar ELSE :param1 END';
  48. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  49. }
  50. /**
  51. * Test sql generation with 0 case.
  52. */
  53. public function testSqlOutputZero(): void
  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. public function testTraverse(): void
  71. {
  72. $count = 0;
  73. $visitor = function () use (&$count): void {
  74. $count++;
  75. };
  76. $expr = new QueryExpression();
  77. $expr->eq('test', 'true');
  78. $expr2 = new QueryExpression();
  79. $expr2->eq('test', 'false');
  80. $caseExpression = new CaseExpression([$expr, $expr2]);
  81. $caseExpression->traverse($visitor);
  82. $this->assertSame(4, $count);
  83. }
  84. /**
  85. * Test cloning
  86. */
  87. public function testClone(): void
  88. {
  89. $expr = new QueryExpression();
  90. $expr->eq('test', 'true');
  91. $expr2 = new QueryExpression();
  92. $expr2->eq('test2', 'false');
  93. $caseExpression = new CaseExpression([$expr, $expr2], 'foobar');
  94. $dupe = clone $caseExpression;
  95. $dupe->elseValue('nope');
  96. $this->assertNotEquals($caseExpression, $dupe);
  97. $this->assertNotSame($caseExpression, $dupe);
  98. }
  99. }