QueryExpressionTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. * @return
  42. */
  43. public function testTieWith()
  44. {
  45. $expr = new QueryExpression(['1', '2']);
  46. $binder = new ValueBinder();
  47. $this->assertSame($expr, $expr->tieWith('+'));
  48. $this->assertSame('+', $expr->tieWith());
  49. $result = $expr->sql($binder);
  50. $this->assertEquals('(1 + 2)', $result);
  51. }
  52. /**
  53. * Test type() works.
  54. *
  55. * @return
  56. */
  57. public function testType()
  58. {
  59. $expr = new QueryExpression(['1', '2']);
  60. $binder = new ValueBinder();
  61. $this->assertSame($expr, $expr->type('+'));
  62. $this->assertSame('+', $expr->type());
  63. $result = $expr->sql($binder);
  64. $this->assertEquals('(1 + 2)', $result);
  65. }
  66. /**
  67. * Test and() and or() calls work transparently
  68. *
  69. * @return void
  70. */
  71. public function testAndOrCalls()
  72. {
  73. $expr = new QueryExpression();
  74. $expected = '\Cake\Database\Expression\QueryExpression';
  75. $this->assertInstanceOf($expected, $expr->and([]));
  76. $this->assertInstanceOf($expected, $expr->or([]));
  77. }
  78. /**
  79. * Test SQL generation with one element
  80. *
  81. * @return void
  82. */
  83. public function testSqlGenerationOneClause()
  84. {
  85. $expr = new QueryExpression();
  86. $binder = new ValueBinder();
  87. $expr->add(['Users.username' => 'sally'], ['Users.username' => 'string']);
  88. $result = $expr->sql($binder);
  89. $this->assertEquals('Users.username = :c0', $result);
  90. }
  91. /**
  92. * Test SQL generation with many elements
  93. *
  94. * @return void
  95. */
  96. public function testSqlGenerationMultipleClauses()
  97. {
  98. $expr = new QueryExpression();
  99. $binder = new ValueBinder();
  100. $expr->add(
  101. [
  102. 'Users.username' => 'sally',
  103. 'Users.active' => 1,
  104. ],
  105. [
  106. 'Users.username' => 'string',
  107. 'Users.active' => 'boolean'
  108. ]
  109. );
  110. $result = $expr->sql($binder);
  111. $this->assertEquals('(Users.username = :c0 AND Users.active = :c1)', $result);
  112. }
  113. /**
  114. * Test that empty expressions don't emit invalid SQL.
  115. *
  116. * @return void
  117. */
  118. public function testSqlWhenEmpty()
  119. {
  120. $expr = new QueryExpression();
  121. $binder = new ValueBinder();
  122. $result = $expr->sql($binder);
  123. $this->assertEquals('', $result);
  124. }
  125. /**
  126. * Test deep cloning of expression trees.
  127. *
  128. * @return void
  129. */
  130. public function testDeepCloning()
  131. {
  132. $expr = new QueryExpression();
  133. $expr = $expr->add(new QueryExpression('1 + 1'))
  134. ->isNull('deleted')
  135. ->like('title', 'things%');
  136. $dupe = clone $expr;
  137. $this->assertEquals($dupe, $expr);
  138. $this->assertNotSame($dupe, $expr);
  139. $originalParts = [];
  140. $expr->iterateParts(function ($part) use (&$originalParts) {
  141. $originalParts[] = $part;
  142. });
  143. $dupe->iterateParts(function ($part, $i) use ($originalParts) {
  144. $this->assertNotSame($originalParts[$i], $part);
  145. });
  146. }
  147. /**
  148. * Tests the hasNestedExpression() function
  149. *
  150. * @return void
  151. */
  152. public function testHasNestedExpression()
  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 void
  168. */
  169. public function methodsProvider()
  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. * @return void
  182. */
  183. public function testTypeMapUsage($method)
  184. {
  185. $expr = new QueryExpression([], ['created' => 'date']);
  186. $expr->{$method}('created', 'foo');
  187. $binder = new ValueBinder();
  188. $expr->sql($binder);
  189. $bindings = $binder->bindings();
  190. $type = current($bindings)['type'];
  191. $this->assertEquals('date', $type);
  192. }
  193. }