ExpressionTypeCastingTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\ComparisonExpression;
  18. use Cake\Database\Expression\FunctionExpression;
  19. use Cake\Database\Expression\ValuesExpression;
  20. use Cake\Database\TypeFactory;
  21. use Cake\Database\TypeMap;
  22. use Cake\Database\ValueBinder;
  23. use Cake\TestSuite\TestCase;
  24. use TestApp\Database\Type\TestType;
  25. /**
  26. * Tests for Expression objects casting values to other expressions
  27. * using the type classes
  28. */
  29. class ExpressionTypeCastingTest extends TestCase
  30. {
  31. /**
  32. * Setups a mock for FunctionsBuilder
  33. */
  34. public function setUp(): void
  35. {
  36. parent::setUp();
  37. TypeFactory::set('test', new TestType());
  38. }
  39. /**
  40. * Tests that the Comparison expression can handle values convertible to
  41. * expressions
  42. */
  43. public function testComparisonSimple(): void
  44. {
  45. $comparison = new ComparisonExpression('field', 'the thing', 'test', '=');
  46. $binder = new ValueBinder();
  47. $sql = $comparison->sql($binder);
  48. $this->assertSame('field = (CONCAT(:param0, :param1))', $sql);
  49. $this->assertSame('the thing', $binder->bindings()[':param0']['value']);
  50. $found = false;
  51. $comparison->traverse(function ($exp) use (&$found): void {
  52. $found = $exp instanceof FunctionExpression;
  53. });
  54. $this->assertTrue($found, 'The expression is not included in the tree');
  55. }
  56. /**
  57. * Tests that the Comparison expression can handle values convertible to
  58. * expressions
  59. */
  60. public function testComparisonMultiple(): void
  61. {
  62. $comparison = new ComparisonExpression('field', ['2', '3'], 'test[]', 'IN');
  63. $binder = new ValueBinder();
  64. $sql = $comparison->sql($binder);
  65. $this->assertSame('field IN (CONCAT(:param0, :param1),CONCAT(:param2, :param3))', $sql);
  66. $this->assertSame('2', $binder->bindings()[':param0']['value']);
  67. $this->assertSame('3', $binder->bindings()[':param2']['value']);
  68. $found = false;
  69. $comparison->traverse(function ($exp) use (&$found): void {
  70. $found = $exp instanceof FunctionExpression;
  71. });
  72. $this->assertTrue($found, 'The expression is not included in the tree');
  73. }
  74. /**
  75. * Tests that the Between expression casts values to expressions correctly
  76. */
  77. public function testBetween(): void
  78. {
  79. $between = new BetweenExpression('field', 'from', 'to', 'test');
  80. $binder = new ValueBinder();
  81. $sql = $between->sql($binder);
  82. $this->assertSame('field BETWEEN CONCAT(:param0, :param1) AND CONCAT(:param2, :param3)', $sql);
  83. $this->assertSame('from', $binder->bindings()[':param0']['value']);
  84. $this->assertSame('to', $binder->bindings()[':param2']['value']);
  85. $expressions = [];
  86. $between->traverse(function ($exp) use (&$expressions): void {
  87. $expressions[] = $exp instanceof FunctionExpression ? 1 : 0;
  88. });
  89. $result = array_sum($expressions);
  90. $this->assertSame(2, $result, 'Missing expressions in the tree');
  91. }
  92. /**
  93. * Tests that values in FunctionExpressions are converted to expressions correctly
  94. */
  95. public function testFunctionExpression(): void
  96. {
  97. $function = new FunctionExpression('DATE', ['2016-01'], ['test']);
  98. $binder = new ValueBinder();
  99. $sql = $function->sql($binder);
  100. $this->assertSame('DATE(CONCAT(:param0, :param1))', $sql);
  101. $this->assertSame('2016-01', $binder->bindings()[':param0']['value']);
  102. $expressions = [];
  103. $function->traverse(function ($exp) use (&$expressions): void {
  104. $expressions[] = $exp instanceof FunctionExpression ? 1 : 0;
  105. });
  106. $result = array_sum($expressions);
  107. $this->assertSame(1, $result, 'Missing expressions in the tree');
  108. }
  109. /**
  110. * Tests that values in ValuesExpression are converted to expressions correctly
  111. */
  112. public function testValuesExpression(): void
  113. {
  114. $values = new ValuesExpression(['title'], new TypeMap(['title' => 'test']));
  115. $values->add(['title' => 'foo']);
  116. $values->add(['title' => 'bar']);
  117. $binder = new ValueBinder();
  118. $sql = $values->sql($binder);
  119. $this->assertSame(
  120. ' VALUES ((CONCAT(:param0, :param1))), ((CONCAT(:param2, :param3)))',
  121. $sql
  122. );
  123. $this->assertSame('foo', $binder->bindings()[':param0']['value']);
  124. $this->assertSame('bar', $binder->bindings()[':param2']['value']);
  125. $expressions = [];
  126. $values->traverse(function ($exp) use (&$expressions): void {
  127. $expressions[] = $exp instanceof FunctionExpression ? 1 : 0;
  128. });
  129. $result = array_sum($expressions);
  130. $this->assertSame(2, $result, 'Missing expressions in the tree');
  131. }
  132. }