QueryTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Database;
  15. use Cake\Database\Connection;
  16. use Cake\Database\Expression\CommonTableExpression;
  17. use Cake\Database\Expression\IdentifierExpression;
  18. use Cake\Database\ExpressionInterface;
  19. use Cake\Database\Query;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\TestSuite\TestCase;
  22. use InvalidArgumentException;
  23. /**
  24. * Tests Query class
  25. */
  26. class QueryTest extends TestCase
  27. {
  28. use QueryAssertsTrait;
  29. protected array $fixtures = [
  30. 'core.Articles',
  31. 'core.Authors',
  32. 'core.Comments',
  33. 'core.Profiles',
  34. 'core.MenuLinkTrees',
  35. ];
  36. /**
  37. * @var \Cake\Database\Connection
  38. */
  39. protected $connection;
  40. /**
  41. * @var bool
  42. */
  43. protected $autoQuote;
  44. protected Query $query;
  45. public function setUp(): void
  46. {
  47. parent::setUp();
  48. $this->connection = ConnectionManager::get('test');
  49. $this->autoQuote = $this->connection->getDriver()->isAutoQuotingEnabled();
  50. $this->query = $this->newQuery();
  51. }
  52. public function tearDown(): void
  53. {
  54. parent::tearDown();
  55. $this->connection->getDriver()->enableAutoQuoting($this->autoQuote);
  56. unset($this->query);
  57. unset($this->connection);
  58. }
  59. public function testConnectionRoles(): void
  60. {
  61. // Defaults to write role
  62. $this->assertSame(Connection::ROLE_WRITE, $this->connection->insertQuery()->getConnectionRole());
  63. $selectQuery = $this->connection->selectQuery();
  64. $this->assertSame(Connection::ROLE_WRITE, $selectQuery->getConnectionRole());
  65. // Can set read role for select queries
  66. $this->assertSame(Connection::ROLE_READ, $selectQuery->setConnectionRole(Connection::ROLE_READ)->getConnectionRole());
  67. // Can set read role for select queries
  68. $this->assertSame(Connection::ROLE_READ, $selectQuery->useReadRole()->getConnectionRole());
  69. // Can set write role for select queries
  70. $this->assertSame(Connection::ROLE_WRITE, $selectQuery->useWriteRole()->getConnectionRole());
  71. }
  72. protected function newQuery(): Query
  73. {
  74. return new class ($this->connection) extends Query
  75. {
  76. };
  77. }
  78. /**
  79. * Tests that empty values don't set where clauses.
  80. */
  81. public function testWhereEmptyValues(): void
  82. {
  83. $this->query->from('comments')
  84. ->where('');
  85. $this->assertCount(0, $this->query->clause('where'));
  86. $this->query->where([]);
  87. $this->assertCount(0, $this->query->clause('where'));
  88. }
  89. /**
  90. * Tests that the identifier method creates an expression object.
  91. */
  92. public function testIdentifierExpression(): void
  93. {
  94. /** @var \Cake\Database\Expression\IdentifierExpression $identifier */
  95. $identifier = $this->query->identifier('foo');
  96. $this->assertInstanceOf(IdentifierExpression::class, $identifier);
  97. $this->assertSame('foo', $identifier->getIdentifier());
  98. }
  99. /**
  100. * Tests the interface contract of identifier
  101. */
  102. public function testIdentifierInterface(): void
  103. {
  104. $identifier = $this->query->identifier('description');
  105. $this->assertInstanceOf(ExpressionInterface::class, $identifier);
  106. $this->assertSame('description', $identifier->getIdentifier());
  107. $identifier->setIdentifier('title');
  108. $this->assertSame('title', $identifier->getIdentifier());
  109. }
  110. /**
  111. * Tests __debugInfo on incomplete query
  112. */
  113. public function testDebugInfoIncompleteQuery(): void
  114. {
  115. $this->query = $this->newQuery()
  116. ->from(['articles']);
  117. $result = $this->query->__debugInfo();
  118. $this->assertStringContainsString('incomplete', $result['sql']);
  119. $this->assertSame([], $result['params']);
  120. }
  121. public function testCloneWithExpression(): void
  122. {
  123. $this->query
  124. ->with(
  125. new CommonTableExpression(
  126. 'cte',
  127. $this->newQuery()
  128. )
  129. )
  130. ->with(function (CommonTableExpression $cte, Query $query) {
  131. return $cte
  132. ->name('cte2')
  133. ->query($query);
  134. });
  135. $clause = $this->query->clause('with');
  136. $clauseClone = (clone $this->query)->clause('with');
  137. $this->assertIsArray($clause);
  138. foreach ($clause as $key => $value) {
  139. $this->assertEquals($value, $clauseClone[$key]);
  140. $this->assertNotSame($value, $clauseClone[$key]);
  141. }
  142. }
  143. public function testCloneModifierExpression(): void
  144. {
  145. $this->query->modifier($this->query->newExpr('modifier'));
  146. $clause = $this->query->clause('modifier');
  147. $clauseClone = (clone $this->query)->clause('modifier');
  148. $this->assertIsArray($clause);
  149. foreach ($clause as $key => $value) {
  150. $this->assertEquals($value, $clauseClone[$key]);
  151. $this->assertNotSame($value, $clauseClone[$key]);
  152. }
  153. }
  154. public function testCloneFromExpression(): void
  155. {
  156. $this->query->from(['alias' => $this->newQuery()]);
  157. $clause = $this->query->clause('from');
  158. $clauseClone = (clone $this->query)->clause('from');
  159. $this->assertIsArray($clause);
  160. foreach ($clause as $key => $value) {
  161. $this->assertEquals($value, $clauseClone[$key]);
  162. $this->assertNotSame($value, $clauseClone[$key]);
  163. }
  164. }
  165. public function testCloneJoinExpression(): void
  166. {
  167. $this->query
  168. ->innerJoin(
  169. ['alias_inner' => $this->newQuery()],
  170. ['alias_inner.fk = parent.pk']
  171. )
  172. ->leftJoin(
  173. ['alias_left' => $this->newQuery()],
  174. ['alias_left.fk = parent.pk']
  175. )
  176. ->rightJoin(
  177. ['alias_right' => $this->newQuery()],
  178. ['alias_right.fk = parent.pk']
  179. );
  180. $clause = $this->query->clause('join');
  181. $clauseClone = (clone $this->query)->clause('join');
  182. $this->assertIsArray($clause);
  183. foreach ($clause as $key => $value) {
  184. $this->assertEquals($value['table'], $clauseClone[$key]['table']);
  185. $this->assertNotSame($value['table'], $clauseClone[$key]['table']);
  186. $this->assertEquals($value['conditions'], $clauseClone[$key]['conditions']);
  187. $this->assertNotSame($value['conditions'], $clauseClone[$key]['conditions']);
  188. }
  189. }
  190. public function testCloneWhereExpression(): void
  191. {
  192. $this->query
  193. ->where($this->query->newExpr('where'))
  194. ->where(['field' => $this->query->newExpr('where')]);
  195. $clause = $this->query->clause('where');
  196. $clauseClone = (clone $this->query)->clause('where');
  197. $this->assertInstanceOf(ExpressionInterface::class, $clause);
  198. $this->assertEquals($clause, $clauseClone);
  199. $this->assertNotSame($clause, $clauseClone);
  200. }
  201. public function testCloneOrderExpression(): void
  202. {
  203. $this->query
  204. ->orderBy($this->query->newExpr('order'))
  205. ->orderByAsc($this->query->newExpr('order_asc'))
  206. ->orderByDesc($this->query->newExpr('order_desc'));
  207. $clause = $this->query->clause('order');
  208. $clauseClone = (clone $this->query)->clause('order');
  209. $this->assertInstanceOf(ExpressionInterface::class, $clause);
  210. $this->assertEquals($clause, $clauseClone);
  211. $this->assertNotSame($clause, $clauseClone);
  212. }
  213. public function testCloneLimitExpression(): void
  214. {
  215. $this->query->limit($this->query->newExpr('1'));
  216. $clause = $this->query->clause('limit');
  217. $clauseClone = (clone $this->query)->clause('limit');
  218. $this->assertInstanceOf(ExpressionInterface::class, $clause);
  219. $this->assertEquals($clause, $clauseClone);
  220. $this->assertNotSame($clause, $clauseClone);
  221. }
  222. public function testCloneOffsetExpression(): void
  223. {
  224. $this->query->offset($this->query->newExpr('1'));
  225. $clause = $this->query->clause('offset');
  226. $clauseClone = (clone $this->query)->clause('offset');
  227. $this->assertInstanceOf(ExpressionInterface::class, $clause);
  228. $this->assertEquals($clause, $clauseClone);
  229. $this->assertNotSame($clause, $clauseClone);
  230. }
  231. public function testCloneEpilogExpression(): void
  232. {
  233. $this->query->epilog($this->query->newExpr('epilog'));
  234. $clause = $this->query->clause('epilog');
  235. $clauseClone = (clone $this->query)->clause('epilog');
  236. $this->assertInstanceOf(ExpressionInterface::class, $clause);
  237. $this->assertEquals($clause, $clauseClone);
  238. $this->assertNotSame($clause, $clauseClone);
  239. }
  240. /**
  241. * Test getValueBinder()
  242. */
  243. public function testGetValueBinder(): void
  244. {
  245. $this->assertInstanceOf('Cake\Database\ValueBinder', $this->query->getValueBinder());
  246. }
  247. /**
  248. * Test that reading an undefined clause does not emit an error.
  249. */
  250. public function testClauseUndefined(): void
  251. {
  252. $this->expectException(InvalidArgumentException::class);
  253. $this->expectExceptionMessage('The `nope` clause is not defined. Valid clauses are: `comment`, `delete`, `update`');
  254. $this->assertEmpty($this->query->clause('where'));
  255. $this->query->clause('nope');
  256. }
  257. }