ExpressionTypeCastingTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.3.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Database;
  15. use Cake\TestSuite\TestCase;
  16. use Cake\Database\Type\StringType;
  17. use Cake\Database\Type\ExpressionTypeInterface;
  18. use Cake\Database\Expression\FunctionExpression;
  19. use Cake\Database\ValueBinder;
  20. use Cake\Database\Type;
  21. use Cake\Database\Expression\Comparison;
  22. use Cake\Database\Expression\BetweenExpression;
  23. use Cake\Database\Expression\CaseExpression;
  24. class TestType extends StringType implements ExpressionTypeInterface
  25. {
  26. public function toExpression($value)
  27. {
  28. return new FunctionExpression('CONCAT', [$value, ' - foo']);
  29. }
  30. }
  31. /**
  32. * Tests for Expression objects casting values to other expressions
  33. * using the type classes
  34. *
  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::map('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(:c0, :c1))', $sql);
  60. $this->assertEquals('the thing', $binder->bindings()[':c0']['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(:c0, :c1),CONCAT(:c2, :c3))', $sql);
  79. $this->assertEquals('2', $binder->bindings()[':c0']['value']);
  80. $this->assertEquals('3', $binder->bindings()[':c2']['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 expresisons 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(:c0, :c1) AND CONCAT(:c2, :c3)', $sql);
  98. $this->assertEquals('from', $binder->bindings()[':c0']['value']);
  99. $this->assertEquals('to', $binder->bindings()[':c2']['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(:c1, :c2) ELSE CONCAT(:c3, :c4) END', $sql);
  122. $this->assertEquals('1', $binder->bindings()[':c0']['value']);
  123. $this->assertEquals('value1', $binder->bindings()[':c1']['value']);
  124. $this->assertEquals('value2', $binder->bindings()[':c3']['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(:c0, :c1)))', $sql);
  143. $this->assertEquals('2016-01', $binder->bindings()[':c0']['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. }