QueryExpressionTest.php 9.2 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. use PHPUnit\Framework\Attributes\DataProvider;
  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. * Tests conditions with multi-word operators.
  40. *
  41. * @return void
  42. */
  43. public function testMultiWordOperators(): void
  44. {
  45. $expr = new QueryExpression(['FUNC(Users.first + Users.last) is not' => 'me']);
  46. $this->assertSame('FUNC(Users.first + Users.last) != :c0', $expr->sql(new ValueBinder()));
  47. $expr = new QueryExpression(['FUNC(Users.name + Users.id) NOT SIMILAR TO' => 'pattern']);
  48. $this->assertSame('FUNC(Users.name + Users.id) NOT SIMILAR TO :c0', $expr->sql(new ValueBinder()));
  49. }
  50. /**
  51. * Tests conditions with symbol operators.
  52. *
  53. * @return void
  54. */
  55. public function testSymbolOperators(): void
  56. {
  57. $expr = new QueryExpression(['Users.name =' => 'pattern']);
  58. $this->assertSame('Users.name = :c0', $expr->sql(new ValueBinder()));
  59. $expr = new QueryExpression(['Users.name !=' => 'pattern']);
  60. $this->assertSame('Users.name != :c0', $expr->sql(new ValueBinder()));
  61. $expr = new QueryExpression(['Users.name <>' => 'pattern']);
  62. $this->assertSame('Users.name <> :c0', $expr->sql(new ValueBinder()));
  63. $expr = new QueryExpression(['Users.name >=' => 'pattern']);
  64. $this->assertSame('Users.name >= :c0', $expr->sql(new ValueBinder()));
  65. $expr = new QueryExpression(['Users.name <=' => 'pattern']);
  66. $this->assertSame('Users.name <= :c0', $expr->sql(new ValueBinder()));
  67. $expr = new QueryExpression(['Users.name !~' => 'pattern']);
  68. $this->assertSame('Users.name !~ :c0', $expr->sql(new ValueBinder()));
  69. $expr = new QueryExpression(['Users.name *' => 'pattern']);
  70. $this->assertSame('Users.name * :c0', $expr->sql(new ValueBinder()));
  71. $expr = new QueryExpression(['Users.name -' => 'pattern']);
  72. $this->assertSame('Users.name - :c0', $expr->sql(new ValueBinder()));
  73. $expr = new QueryExpression(['Users.name \\' => 'pattern']);
  74. $this->assertSame('Users.name \\ :c0', $expr->sql(new ValueBinder()));
  75. $expr = new QueryExpression(['Users.name @>' => 'pattern']);
  76. $this->assertSame('Users.name @> :c0', $expr->sql(new ValueBinder()));
  77. }
  78. /**
  79. * Test and() and or() calls work transparently
  80. */
  81. public function testAndOrCalls(): void
  82. {
  83. $expr = new QueryExpression();
  84. $expected = QueryExpression::class;
  85. $this->assertInstanceOf($expected, $expr->and([]));
  86. $this->assertInstanceOf($expected, $expr->or([]));
  87. }
  88. /**
  89. * Test SQL generation with one element
  90. */
  91. public function testSqlGenerationOneClause(): void
  92. {
  93. $expr = new QueryExpression();
  94. $binder = new ValueBinder();
  95. $expr->add(['Users.username' => 'sally'], ['Users.username' => 'string']);
  96. $result = $expr->sql($binder);
  97. $this->assertSame('Users.username = :c0', $result);
  98. }
  99. /**
  100. * Test SQL generation with many elements
  101. */
  102. public function testSqlGenerationMultipleClauses(): void
  103. {
  104. $expr = new QueryExpression();
  105. $binder = new ValueBinder();
  106. $expr->add(
  107. [
  108. 'Users.username' => 'sally',
  109. 'Users.active' => 1,
  110. ],
  111. [
  112. 'Users.username' => 'string',
  113. 'Users.active' => 'boolean',
  114. ]
  115. );
  116. $result = $expr->sql($binder);
  117. $this->assertSame('(Users.username = :c0 AND Users.active = :c1)', $result);
  118. }
  119. /**
  120. * Test that empty expressions don't emit invalid SQL.
  121. */
  122. public function testSqlWhenEmpty(): void
  123. {
  124. $expr = new QueryExpression();
  125. $binder = new ValueBinder();
  126. $result = $expr->sql($binder);
  127. $this->assertSame('', $result);
  128. }
  129. /**
  130. * Test deep cloning of expression trees.
  131. */
  132. public function testDeepCloning(): void
  133. {
  134. $expr = new QueryExpression();
  135. $expr = $expr->add(new QueryExpression('1 + 1'))
  136. ->isNull('deleted')
  137. ->like('title', 'things%');
  138. $dupe = clone $expr;
  139. $this->assertEquals($dupe, $expr);
  140. $this->assertNotSame($dupe, $expr);
  141. $originalParts = [];
  142. $expr->iterateParts(function ($part) use (&$originalParts): void {
  143. $originalParts[] = $part;
  144. });
  145. $dupe->iterateParts(function ($part, $i) use ($originalParts): void {
  146. $this->assertNotSame($originalParts[$i], $part);
  147. });
  148. }
  149. /**
  150. * Tests the hasNestedExpression() function
  151. */
  152. public function testHasNestedExpression(): void
  153. {
  154. $expr = new QueryExpression();
  155. $this->assertFalse($expr->hasNestedExpression());
  156. $expr->add(['a' => 'b']);
  157. $this->assertTrue($expr->hasNestedExpression());
  158. $expr = new QueryExpression();
  159. $expr->add('a = b');
  160. $this->assertFalse($expr->hasNestedExpression());
  161. $expr->add(new QueryExpression('1 = 1'));
  162. $this->assertTrue($expr->hasNestedExpression());
  163. }
  164. /**
  165. * Returns the list of specific comparison methods
  166. *
  167. * @return array
  168. */
  169. public static function methodsProvider(): array
  170. {
  171. return [
  172. ['eq'], ['notEq'], ['gt'], ['lt'], ['gte'], ['lte'], ['like'],
  173. ['notLike'], ['in'], ['notIn'],
  174. ];
  175. }
  176. /**
  177. * Tests that the query expression uses the type map when the
  178. * specific comparison functions are used.
  179. */
  180. #[DataProvider('methodsProvider')]
  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. }