CaseExpressionTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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', 'else']);
  45. $expected = 'CASE WHEN test = :c0 THEN :param1 ELSE :param2 END';
  46. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  47. $caseExpression = new CaseExpression([$expr], ['foobar' => 'literal', 'else']);
  48. $expected = 'CASE WHEN test = :c0 THEN foobar ELSE :param1 END';
  49. $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
  50. }
  51. /**
  52. * Test sql generation with 0 case.
  53. *
  54. * @return void
  55. */
  56. public function testSqlOutputZero()
  57. {
  58. $expression = new QueryExpression();
  59. $expression->add(['id' => 'test']);
  60. $caseExpression = new CaseExpression([$expression], [0], ['integer']);
  61. $expected = 'CASE WHEN id = :c0 THEN :param1 END';
  62. $binder = new ValueBinder();
  63. $this->assertSame($expected, $caseExpression->sql($binder));
  64. $expected = [
  65. ':c0' => ['value' => 'test', 'type' => null, 'placeholder' => 'c0'],
  66. ':param1' => ['value' => 0, 'type' => 'integer', 'placeholder' => 'param1'],
  67. ];
  68. $this->assertEquals($expected, $binder->bindings());
  69. }
  70. /**
  71. * Tests that the expression is correctly traversed
  72. *
  73. * @return void
  74. */
  75. public function testTraverse()
  76. {
  77. $count = 0;
  78. $visitor = function () use (&$count) {
  79. $count++;
  80. };
  81. $expr = new QueryExpression();
  82. $expr->eq('test', 'true');
  83. $expr2 = new QueryExpression();
  84. $expr2->eq('test', 'false');
  85. $caseExpression = new CaseExpression([$expr, $expr2]);
  86. $caseExpression->traverse($visitor);
  87. $this->assertSame(4, $count);
  88. }
  89. /**
  90. * Test cloning
  91. *
  92. * @return void
  93. */
  94. public function testClone()
  95. {
  96. $expr = new QueryExpression();
  97. $expr->eq('test', 'true');
  98. $expr2 = new QueryExpression();
  99. $expr2->eq('test2', 'false');
  100. $caseExpression = new CaseExpression([$expr, $expr2], 'foobar');
  101. $dupe = clone $caseExpression;
  102. $dupe->elseValue('nope');
  103. $this->assertNotEquals($caseExpression, $dupe);
  104. $this->assertNotSame($caseExpression, $dupe);
  105. }
  106. }