QueryExpressionTest.php 7.3 KB

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