QueryExpressionTest.php 5.9 KB

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