QueryTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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()
  73. {
  74. return $this->getMockForAbstractClass(Query::class, [$this->connection]);
  75. }
  76. /**
  77. * Tests that empty values don't set where clauses.
  78. */
  79. public function testWhereEmptyValues(): void
  80. {
  81. $this->query->from('comments')
  82. ->where('');
  83. $this->assertCount(0, $this->query->clause('where'));
  84. $this->query->where([]);
  85. $this->assertCount(0, $this->query->clause('where'));
  86. }
  87. /**
  88. * Tests that the identifier method creates an expression object.
  89. */
  90. public function testIdentifierExpression(): void
  91. {
  92. /** @var \Cake\Database\Expression\IdentifierExpression $identifier */
  93. $identifier = $this->query->identifier('foo');
  94. $this->assertInstanceOf(IdentifierExpression::class, $identifier);
  95. $this->assertSame('foo', $identifier->getIdentifier());
  96. }
  97. /**
  98. * Tests the interface contract of identifier
  99. */
  100. public function testIdentifierInterface(): void
  101. {
  102. $identifier = $this->query->identifier('description');
  103. $this->assertInstanceOf(ExpressionInterface::class, $identifier);
  104. $this->assertSame('description', $identifier->getIdentifier());
  105. $identifier->setIdentifier('title');
  106. $this->assertSame('title', $identifier->getIdentifier());
  107. }
  108. /**
  109. * Tests __debugInfo on incomplete query
  110. */
  111. public function testDebugInfoIncompleteQuery(): void
  112. {
  113. $this->query = $this->newQuery()
  114. ->from(['articles']);
  115. $result = $this->query->__debugInfo();
  116. $this->assertStringContainsString('incomplete', $result['sql']);
  117. $this->assertSame([], $result['params']);
  118. }
  119. public function testCloneWithExpression(): void
  120. {
  121. $this->query
  122. ->with(
  123. new CommonTableExpression(
  124. 'cte',
  125. $this->newQuery()
  126. )
  127. )
  128. ->with(function (CommonTableExpression $cte, Query $query) {
  129. return $cte
  130. ->name('cte2')
  131. ->query($query);
  132. });
  133. $clause = $this->query->clause('with');
  134. $clauseClone = (clone $this->query)->clause('with');
  135. $this->assertIsArray($clause);
  136. foreach ($clause as $key => $value) {
  137. $this->assertEquals($value, $clauseClone[$key]);
  138. $this->assertNotSame($value, $clauseClone[$key]);
  139. }
  140. }
  141. public function testCloneModifierExpression(): void
  142. {
  143. $this->query->modifier($this->query->newExpr('modifier'));
  144. $clause = $this->query->clause('modifier');
  145. $clauseClone = (clone $this->query)->clause('modifier');
  146. $this->assertIsArray($clause);
  147. foreach ($clause as $key => $value) {
  148. $this->assertEquals($value, $clauseClone[$key]);
  149. $this->assertNotSame($value, $clauseClone[$key]);
  150. }
  151. }
  152. public function testCloneFromExpression(): void
  153. {
  154. $this->query->from(['alias' => $this->newQuery()]);
  155. $clause = $this->query->clause('from');
  156. $clauseClone = (clone $this->query)->clause('from');
  157. $this->assertIsArray($clause);
  158. foreach ($clause as $key => $value) {
  159. $this->assertEquals($value, $clauseClone[$key]);
  160. $this->assertNotSame($value, $clauseClone[$key]);
  161. }
  162. }
  163. public function testCloneJoinExpression(): void
  164. {
  165. $this->query
  166. ->innerJoin(
  167. ['alias_inner' => $this->newQuery()],
  168. ['alias_inner.fk = parent.pk']
  169. )
  170. ->leftJoin(
  171. ['alias_left' => $this->newQuery()],
  172. ['alias_left.fk = parent.pk']
  173. )
  174. ->rightJoin(
  175. ['alias_right' => $this->newQuery()],
  176. ['alias_right.fk = parent.pk']
  177. );
  178. $clause = $this->query->clause('join');
  179. $clauseClone = (clone $this->query)->clause('join');
  180. $this->assertIsArray($clause);
  181. foreach ($clause as $key => $value) {
  182. $this->assertEquals($value['table'], $clauseClone[$key]['table']);
  183. $this->assertNotSame($value['table'], $clauseClone[$key]['table']);
  184. $this->assertEquals($value['conditions'], $clauseClone[$key]['conditions']);
  185. $this->assertNotSame($value['conditions'], $clauseClone[$key]['conditions']);
  186. }
  187. }
  188. public function testCloneWhereExpression(): void
  189. {
  190. $this->query
  191. ->where($this->query->newExpr('where'))
  192. ->where(['field' => $this->query->newExpr('where')]);
  193. $clause = $this->query->clause('where');
  194. $clauseClone = (clone $this->query)->clause('where');
  195. $this->assertInstanceOf(ExpressionInterface::class, $clause);
  196. $this->assertEquals($clause, $clauseClone);
  197. $this->assertNotSame($clause, $clauseClone);
  198. }
  199. public function testCloneOrderExpression(): void
  200. {
  201. $this->query
  202. ->orderBy($this->query->newExpr('order'))
  203. ->orderByAsc($this->query->newExpr('order_asc'))
  204. ->orderByDesc($this->query->newExpr('order_desc'));
  205. $clause = $this->query->clause('order');
  206. $clauseClone = (clone $this->query)->clause('order');
  207. $this->assertInstanceOf(ExpressionInterface::class, $clause);
  208. $this->assertEquals($clause, $clauseClone);
  209. $this->assertNotSame($clause, $clauseClone);
  210. }
  211. public function testCloneLimitExpression(): void
  212. {
  213. $this->query->limit($this->query->newExpr('1'));
  214. $clause = $this->query->clause('limit');
  215. $clauseClone = (clone $this->query)->clause('limit');
  216. $this->assertInstanceOf(ExpressionInterface::class, $clause);
  217. $this->assertEquals($clause, $clauseClone);
  218. $this->assertNotSame($clause, $clauseClone);
  219. }
  220. public function testCloneOffsetExpression(): void
  221. {
  222. $this->query->offset($this->query->newExpr('1'));
  223. $clause = $this->query->clause('offset');
  224. $clauseClone = (clone $this->query)->clause('offset');
  225. $this->assertInstanceOf(ExpressionInterface::class, $clause);
  226. $this->assertEquals($clause, $clauseClone);
  227. $this->assertNotSame($clause, $clauseClone);
  228. }
  229. public function testCloneEpilogExpression(): void
  230. {
  231. $this->query->epilog($this->query->newExpr('epilog'));
  232. $clause = $this->query->clause('epilog');
  233. $clauseClone = (clone $this->query)->clause('epilog');
  234. $this->assertInstanceOf(ExpressionInterface::class, $clause);
  235. $this->assertEquals($clause, $clauseClone);
  236. $this->assertNotSame($clause, $clauseClone);
  237. }
  238. /**
  239. * Test getValueBinder()
  240. */
  241. public function testGetValueBinder(): void
  242. {
  243. $this->assertInstanceOf('Cake\Database\ValueBinder', $this->query->getValueBinder());
  244. }
  245. /**
  246. * Test that reading an undefined clause does not emit an error.
  247. */
  248. public function testClauseUndefined(): void
  249. {
  250. $this->expectException(InvalidArgumentException::class);
  251. $this->expectExceptionMessage('The `nope` clause is not defined. Valid clauses are: `comment`, `delete`, `update`');
  252. $this->assertEmpty($this->query->clause('where'));
  253. $this->query->clause('nope');
  254. }
  255. }