ExpressionTypeCastingTest.php 6.5 KB

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