QueryExpressionTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.6
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Database\Expression;
  17. use Cake\Database\Expression\IdentifierExpression;
  18. use Cake\Database\Expression\QueryExpression;
  19. use Cake\Database\ValueBinder;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * Tests QueryExpression class
  23. */
  24. class QueryExpressionTest extends TestCase
  25. {
  26. /**
  27. * Test setConjunction()/getConjunction() works.
  28. */
  29. public function testConjunction(): void
  30. {
  31. $expr = new QueryExpression(['1', '2']);
  32. $binder = new ValueBinder();
  33. $this->assertSame($expr, $expr->setConjunction('+'));
  34. $this->assertSame('+', $expr->getConjunction());
  35. $result = $expr->sql($binder);
  36. $this->assertSame('(1 + 2)', $result);
  37. }
  38. /**
  39. * Test and() and or() calls work transparently
  40. */
  41. public function testAndOrCalls(): void
  42. {
  43. $expr = new QueryExpression();
  44. $expected = 'Cake\Database\Expression\QueryExpression';
  45. $this->assertInstanceOf($expected, $expr->and([]));
  46. $this->assertInstanceOf($expected, $expr->or([]));
  47. }
  48. /**
  49. * Test SQL generation with one element
  50. */
  51. public function testSqlGenerationOneClause(): void
  52. {
  53. $expr = new QueryExpression();
  54. $binder = new ValueBinder();
  55. $expr->add(['Users.username' => 'sally'], ['Users.username' => 'string']);
  56. $result = $expr->sql($binder);
  57. $this->assertSame('Users.username = :c0', $result);
  58. }
  59. /**
  60. * Test SQL generation with many elements
  61. */
  62. public function testSqlGenerationMultipleClauses(): void
  63. {
  64. $expr = new QueryExpression();
  65. $binder = new ValueBinder();
  66. $expr->add(
  67. [
  68. 'Users.username' => 'sally',
  69. 'Users.active' => 1,
  70. ],
  71. [
  72. 'Users.username' => 'string',
  73. 'Users.active' => 'boolean',
  74. ]
  75. );
  76. $result = $expr->sql($binder);
  77. $this->assertSame('(Users.username = :c0 AND Users.active = :c1)', $result);
  78. }
  79. /**
  80. * Test that empty expressions don't emit invalid SQL.
  81. */
  82. public function testSqlWhenEmpty(): void
  83. {
  84. $expr = new QueryExpression();
  85. $binder = new ValueBinder();
  86. $result = $expr->sql($binder);
  87. $this->assertSame('', $result);
  88. }
  89. /**
  90. * Test deep cloning of expression trees.
  91. */
  92. public function testDeepCloning(): void
  93. {
  94. $expr = new QueryExpression();
  95. $expr = $expr->add(new QueryExpression('1 + 1'))
  96. ->isNull('deleted')
  97. ->like('title', 'things%');
  98. $dupe = clone $expr;
  99. $this->assertEquals($dupe, $expr);
  100. $this->assertNotSame($dupe, $expr);
  101. $originalParts = [];
  102. $expr->iterateParts(function ($part) use (&$originalParts): void {
  103. $originalParts[] = $part;
  104. });
  105. $dupe->iterateParts(function ($part, $i) use ($originalParts): void {
  106. $this->assertNotSame($originalParts[$i], $part);
  107. });
  108. }
  109. /**
  110. * Tests the hasNestedExpression() function
  111. */
  112. public function testHasNestedExpression(): void
  113. {
  114. $expr = new QueryExpression();
  115. $this->assertFalse($expr->hasNestedExpression());
  116. $expr->add(['a' => 'b']);
  117. $this->assertTrue($expr->hasNestedExpression());
  118. $expr = new QueryExpression();
  119. $expr->add('a = b');
  120. $this->assertFalse($expr->hasNestedExpression());
  121. $expr->add(new QueryExpression('1 = 1'));
  122. $this->assertTrue($expr->hasNestedExpression());
  123. }
  124. /**
  125. * Returns the list of specific comparison methods
  126. *
  127. * @return void
  128. */
  129. public function methodsProvider(): array
  130. {
  131. return [
  132. ['eq'], ['notEq'], ['gt'], ['lt'], ['gte'], ['lte'], ['like'],
  133. ['notLike'], ['in'], ['notIn'],
  134. ];
  135. }
  136. /**
  137. * Tests that the query expression uses the type map when the
  138. * specific comparison functions are used.
  139. *
  140. * @dataProvider methodsProvider
  141. */
  142. public function testTypeMapUsage(string $method): void
  143. {
  144. $expr = new QueryExpression([], ['created' => 'date']);
  145. $expr->{$method}('created', 'foo');
  146. $binder = new ValueBinder();
  147. $expr->sql($binder);
  148. $bindings = $binder->bindings();
  149. $type = current($bindings)['type'];
  150. $this->assertSame('date', $type);
  151. }
  152. /**
  153. * Tests that creating query expressions with either the
  154. * array notation or using the combinators will produce a
  155. * zero-count expression object.
  156. *
  157. * @see https://github.com/cakephp/cakephp/issues/12081
  158. */
  159. public function testEmptyOr(): void
  160. {
  161. $expr = new QueryExpression();
  162. $expr = $expr->or([]);
  163. $expr = $expr->or([]);
  164. $this->assertCount(0, $expr);
  165. $expr = new QueryExpression(['OR' => []]);
  166. $this->assertCount(0, $expr);
  167. }
  168. /**
  169. * Tests that both conditions are generated for notInOrNull().
  170. */
  171. public function testNotInOrNull(): void
  172. {
  173. $expr = new QueryExpression();
  174. $expr->notInOrNull('test', ['one', 'two']);
  175. $this->assertEqualsSql(
  176. '(test NOT IN (:c0,:c1) OR (test) IS NULL)',
  177. $expr->sql(new ValueBinder())
  178. );
  179. }
  180. /**
  181. * Test deprecated adding of case statement.
  182. */
  183. public function testDeprecatedAddCaseStatement(): void
  184. {
  185. $this->expectDeprecation();
  186. $this->expectDeprecationMessage('QueryExpression::addCase() is deprecated, use case() instead.');
  187. (new QueryExpression())->addCase([]);
  188. }
  189. public function testCaseWithoutValue(): void
  190. {
  191. $expression = (new QueryExpression())
  192. ->case()
  193. ->when(1)
  194. ->then(2);
  195. $this->assertEqualsSql(
  196. 'CASE WHEN :c0 THEN :c1 ELSE NULL END',
  197. $expression->sql(new ValueBinder())
  198. );
  199. }
  200. public function testCaseWithValue(): void
  201. {
  202. $expression = (new QueryExpression())
  203. ->case(new IdentifierExpression('Table.column'))
  204. ->when(1)
  205. ->then('Yes');
  206. $this->assertEqualsSql(
  207. 'CASE Table.column WHEN :c0 THEN :c1 ELSE NULL END',
  208. $expression->sql(new ValueBinder())
  209. );
  210. }
  211. public function testCaseWithValueAndType(): void
  212. {
  213. $expression = (new QueryExpression())
  214. ->case('1', 'integer')
  215. ->when(1)
  216. ->then('Yes');
  217. $valueBinder = new ValueBinder();
  218. $this->assertEqualsSql(
  219. 'CASE :c0 WHEN :c1 THEN :c2 ELSE NULL END',
  220. $expression->sql($valueBinder)
  221. );
  222. $this->assertSame(
  223. [
  224. 'value' => '1',
  225. 'type' => 'integer',
  226. 'placeholder' => 'c0',
  227. ],
  228. $valueBinder->bindings()[':c0']
  229. );
  230. }
  231. }