ExpressionTypeCastingTest.php 6.7 KB

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