ExpressionTypeCastingTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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\Comparison;
  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. * @return void
  36. */
  37. public function setUp(): void
  38. {
  39. parent::setUp();
  40. TypeFactory::set('test', new TestType());
  41. }
  42. /**
  43. * Tests that the Comparison expression can handle values convertible to
  44. * expressions
  45. *
  46. * @return void
  47. */
  48. public function testComparisonSimple()
  49. {
  50. $comparison = new Comparison('field', 'the thing', 'test', '=');
  51. $binder = new ValueBinder();
  52. $sql = $comparison->sql($binder);
  53. $this->assertSame('field = (CONCAT(:param0, :param1))', $sql);
  54. $this->assertSame('the thing', $binder->bindings()[':param0']['value']);
  55. $found = false;
  56. $comparison->traverse(function ($exp) use (&$found) {
  57. $found = $exp instanceof FunctionExpression;
  58. });
  59. $this->assertTrue($found, 'The expression is not included in the tree');
  60. }
  61. /**
  62. * Tests that the Comparison expression can handle values convertible to
  63. * expressions
  64. *
  65. * @return void
  66. */
  67. public function testComparisonMultiple()
  68. {
  69. $comparison = new Comparison('field', ['2', '3'], 'test[]', 'IN');
  70. $binder = new ValueBinder();
  71. $sql = $comparison->sql($binder);
  72. $this->assertSame('field IN (CONCAT(:param0, :param1),CONCAT(:param2, :param3))', $sql);
  73. $this->assertSame('2', $binder->bindings()[':param0']['value']);
  74. $this->assertSame('3', $binder->bindings()[':param2']['value']);
  75. $found = false;
  76. $comparison->traverse(function ($exp) use (&$found) {
  77. $found = $exp instanceof FunctionExpression;
  78. });
  79. $this->assertTrue($found, 'The expression is not included in the tree');
  80. }
  81. /**
  82. * Tests that the Between expression casts values to expressions correctly
  83. *
  84. * @return void
  85. */
  86. public function testBetween()
  87. {
  88. $between = new BetweenExpression('field', 'from', 'to', 'test');
  89. $binder = new ValueBinder();
  90. $sql = $between->sql($binder);
  91. $this->assertSame('field BETWEEN CONCAT(:param0, :param1) AND CONCAT(:param2, :param3)', $sql);
  92. $this->assertSame('from', $binder->bindings()[':param0']['value']);
  93. $this->assertSame('to', $binder->bindings()[':param2']['value']);
  94. $expressions = [];
  95. $between->traverse(function ($exp) use (&$expressions) {
  96. $expressions[] = $exp instanceof FunctionExpression ? 1 : 0;
  97. });
  98. $result = array_sum($expressions);
  99. $this->assertEquals(2, $result, 'Missing expressions in the tree');
  100. }
  101. /**
  102. * Tests that the Case expressions converts values to expressions correctly
  103. *
  104. * @return void
  105. */
  106. public function testCaseExpression()
  107. {
  108. $case = new CaseExpression(
  109. [new Comparison('foo', '1', 'string', '=')],
  110. ['value1', 'value2'],
  111. ['test', 'test']
  112. );
  113. $binder = new ValueBinder();
  114. $sql = $case->sql($binder);
  115. $this->assertSame('CASE WHEN foo = :c0 THEN CONCAT(:param1, :param2) ELSE CONCAT(:param3, :param4) END', $sql);
  116. $this->assertSame('1', $binder->bindings()[':c0']['value']);
  117. $this->assertSame('value1', $binder->bindings()[':param1']['value']);
  118. $this->assertSame('value2', $binder->bindings()[':param3']['value']);
  119. $expressions = [];
  120. $case->traverse(function ($exp) use (&$expressions) {
  121. $expressions[] = $exp instanceof FunctionExpression ? 1 : 0;
  122. });
  123. $result = array_sum($expressions);
  124. $this->assertEquals(2, $result, 'Missing expressions in the tree');
  125. }
  126. /**
  127. * Tests that values in FunctionExpressions are converted to expressions correctly
  128. *
  129. * @return void
  130. */
  131. public function testFunctionExpression()
  132. {
  133. $function = new FunctionExpression('DATE', ['2016-01'], ['test']);
  134. $binder = new ValueBinder();
  135. $sql = $function->sql($binder);
  136. $this->assertSame('DATE(CONCAT(:param0, :param1))', $sql);
  137. $this->assertSame('2016-01', $binder->bindings()[':param0']['value']);
  138. $expressions = [];
  139. $function->traverse(function ($exp) use (&$expressions) {
  140. $expressions[] = $exp instanceof FunctionExpression ? 1 : 0;
  141. });
  142. $result = array_sum($expressions);
  143. $this->assertEquals(1, $result, 'Missing expressions in the tree');
  144. }
  145. /**
  146. * Tests that values in ValuesExpression are converted to expressions correctly
  147. *
  148. * @return void
  149. */
  150. public function testValuesExpression()
  151. {
  152. $values = new ValuesExpression(['title'], new TypeMap(['title' => 'test']));
  153. $values->add(['title' => 'foo']);
  154. $values->add(['title' => 'bar']);
  155. $binder = new ValueBinder();
  156. $sql = $values->sql($binder);
  157. $this->assertEquals(
  158. ' VALUES ((CONCAT(:param0, :param1))), ((CONCAT(:param2, :param3)))',
  159. $sql
  160. );
  161. $this->assertSame('foo', $binder->bindings()[':param0']['value']);
  162. $this->assertSame('bar', $binder->bindings()[':param2']['value']);
  163. $expressions = [];
  164. $values->traverse(function ($exp) use (&$expressions) {
  165. $expressions[] = $exp instanceof FunctionExpression ? 1 : 0;
  166. });
  167. $result = array_sum($expressions);
  168. $this->assertEquals(2, $result, 'Missing expressions in the tree');
  169. }
  170. }