ExpressionTypeCastingTest.php 6.7 KB

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