QueryExpressionTest.php 9.1 KB

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