QueryExpressionTest.php 6.5 KB

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