ExpressionTypeCastingTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.3.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database;
  16. use Cake\Database\Expression\BetweenExpression;
  17. use Cake\Database\Expression\CaseExpression;
  18. use Cake\Database\Expression\ComparisonExpression;
  19. use Cake\Database\Expression\FunctionExpression;
  20. use Cake\Database\Expression\ValuesExpression;
  21. use Cake\Database\TypeFactory;
  22. use Cake\Database\TypeMap;
  23. use Cake\Database\ValueBinder;
  24. use Cake\TestSuite\TestCase;
  25. use TestApp\Database\Type\TestType;
  26. /**
  27. * Tests for Expression objects casting values to other expressions
  28. * using the type classes
  29. */
  30. class ExpressionTypeCastingTest extends TestCase
  31. {
  32. /**
  33. * Setups a mock for FunctionsBuilder
  34. */
  35. public function setUp(): void
  36. {
  37. parent::setUp();
  38. TypeFactory::set('test', new TestType());
  39. }
  40. /**
  41. * Tests that the Comparison expression can handle values convertible to
  42. * expressions
  43. */
  44. public function testComparisonSimple(): void
  45. {
  46. $comparison = new ComparisonExpression('field', 'the thing', 'test', '=');
  47. $binder = new ValueBinder();
  48. $sql = $comparison->sql($binder);
  49. $this->assertSame('field = (CONCAT(:param0, :param1))', $sql);
  50. $this->assertSame('the thing', $binder->bindings()[':param0']['value']);
  51. $found = false;
  52. $comparison->traverse(function ($exp) use (&$found): void {
  53. $found = $exp instanceof FunctionExpression;
  54. });
  55. $this->assertTrue($found, 'The expression is not included in the tree');
  56. }
  57. /**
  58. * Tests that the Comparison expression can handle values convertible to
  59. * expressions
  60. */
  61. public function testComparisonMultiple(): void
  62. {
  63. $comparison = new ComparisonExpression('field', ['2', '3'], 'test[]', 'IN');
  64. $binder = new ValueBinder();
  65. $sql = $comparison->sql($binder);
  66. $this->assertSame('field IN (CONCAT(:param0, :param1),CONCAT(:param2, :param3))', $sql);
  67. $this->assertSame('2', $binder->bindings()[':param0']['value']);
  68. $this->assertSame('3', $binder->bindings()[':param2']['value']);
  69. $found = false;
  70. $comparison->traverse(function ($exp) use (&$found): void {
  71. $found = $exp instanceof FunctionExpression;
  72. });
  73. $this->assertTrue($found, 'The expression is not included in the tree');
  74. }
  75. /**
  76. * Tests that the Between expression casts values to expressions correctly
  77. */
  78. public function testBetween(): void
  79. {
  80. $between = new BetweenExpression('field', 'from', 'to', 'test');
  81. $binder = new ValueBinder();
  82. $sql = $between->sql($binder);
  83. $this->assertSame('field BETWEEN CONCAT(:param0, :param1) AND CONCAT(:param2, :param3)', $sql);
  84. $this->assertSame('from', $binder->bindings()[':param0']['value']);
  85. $this->assertSame('to', $binder->bindings()[':param2']['value']);
  86. $expressions = [];
  87. $between->traverse(function ($exp) use (&$expressions): void {
  88. $expressions[] = $exp instanceof FunctionExpression ? 1 : 0;
  89. });
  90. $result = array_sum($expressions);
  91. $this->assertSame(2, $result, 'Missing expressions in the tree');
  92. }
  93. /**
  94. * Tests that the Case expressions converts values to expressions correctly
  95. */
  96. public function testCaseExpression(): void
  97. {
  98. $case = new CaseExpression(
  99. [new ComparisonExpression('foo', '1', 'string', '=')],
  100. ['value1', 'value2'],
  101. ['test', 'test']
  102. );
  103. $binder = new ValueBinder();
  104. $sql = $case->sql($binder);
  105. $this->assertSame('CASE WHEN foo = :c0 THEN CONCAT(:param1, :param2) ELSE CONCAT(:param3, :param4) END', $sql);
  106. $this->assertSame('1', $binder->bindings()[':c0']['value']);
  107. $this->assertSame('value1', $binder->bindings()[':param1']['value']);
  108. $this->assertSame('value2', $binder->bindings()[':param3']['value']);
  109. $expressions = [];
  110. $case->traverse(function ($exp) use (&$expressions): void {
  111. $expressions[] = $exp instanceof FunctionExpression ? 1 : 0;
  112. });
  113. $result = array_sum($expressions);
  114. $this->assertSame(2, $result, 'Missing expressions in the tree');
  115. }
  116. /**
  117. * Tests that values in FunctionExpressions are converted to expressions correctly
  118. */
  119. public function testFunctionExpression(): void
  120. {
  121. $function = new FunctionExpression('DATE', ['2016-01'], ['test']);
  122. $binder = new ValueBinder();
  123. $sql = $function->sql($binder);
  124. $this->assertSame('DATE(CONCAT(:param0, :param1))', $sql);
  125. $this->assertSame('2016-01', $binder->bindings()[':param0']['value']);
  126. $expressions = [];
  127. $function->traverse(function ($exp) use (&$expressions): void {
  128. $expressions[] = $exp instanceof FunctionExpression ? 1 : 0;
  129. });
  130. $result = array_sum($expressions);
  131. $this->assertSame(1, $result, 'Missing expressions in the tree');
  132. }
  133. /**
  134. * Tests that values in ValuesExpression are converted to expressions correctly
  135. */
  136. public function testValuesExpression(): void
  137. {
  138. $values = new ValuesExpression(['title'], new TypeMap(['title' => 'test']));
  139. $values->add(['title' => 'foo']);
  140. $values->add(['title' => 'bar']);
  141. $binder = new ValueBinder();
  142. $sql = $values->sql($binder);
  143. $this->assertSame(
  144. ' VALUES ((CONCAT(:param0, :param1))), ((CONCAT(:param2, :param3)))',
  145. $sql
  146. );
  147. $this->assertSame('foo', $binder->bindings()[':param0']['value']);
  148. $this->assertSame('bar', $binder->bindings()[':param2']['value']);
  149. $expressions = [];
  150. $values->traverse(function ($exp) use (&$expressions): void {
  151. $expressions[] = $exp instanceof FunctionExpression ? 1 : 0;
  152. });
  153. $result = array_sum($expressions);
  154. $this->assertSame(2, $result, 'Missing expressions in the tree');
  155. }
  156. }