TupleComparisonQueryTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 4.3.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Database\QueryTests;
  17. use Cake\Database\Driver\Mysql;
  18. use Cake\Database\Driver\Postgres;
  19. use Cake\Database\Driver\Sqlite;
  20. use Cake\Database\Driver\Sqlserver;
  21. use Cake\Database\Expression\TupleComparison;
  22. use Cake\TestSuite\TestCase;
  23. use PDOException;
  24. use RuntimeException;
  25. /**
  26. * Tuple comparison query tests.
  27. *
  28. * These tests are specifically relevant in the context of Sqlite and
  29. * Sqlserver, for which the tuple comparison will be transformed when
  30. * composite fields are used.
  31. *
  32. * @see \Cake\Database\Driver\TupleComparisonTranslatorTrait::_transformTupleComparison()
  33. */
  34. class TupleComparisonQueryTest extends TestCase
  35. {
  36. /**
  37. * @inheritDoc
  38. */
  39. protected array $fixtures = [
  40. 'core.Articles',
  41. ];
  42. public function testTransformWithInvalidOperator(): void
  43. {
  44. $articles = $this->getTableLocator()->get('Articles');
  45. $driver = $articles->getConnection()->getDriver();
  46. if (
  47. $driver instanceof Sqlite ||
  48. $driver instanceof Sqlserver
  49. ) {
  50. $this->expectException(RuntimeException::class);
  51. $this->expectExceptionMessage(
  52. 'Tuple comparison transform only supports the `IN` and `=` operators, `NOT IN` given.'
  53. );
  54. } else {
  55. $this->markTestSkipped('Tuple comparisons are only being transformed for Sqlite and Sqlserver.');
  56. }
  57. $articles
  58. ->find()
  59. ->select(['Articles.id', 'Articles.author_id'])
  60. ->where([
  61. new TupleComparison(
  62. ['Articles.id', 'Articles.author_id'],
  63. $articles
  64. ->subquery()
  65. ->select(['ArticlesAlias.id', 'ArticlesAlias.author_id'])
  66. ->from(['ArticlesAlias' => $articles->getTable()])
  67. ->where(['ArticlesAlias.author_id' => 1]),
  68. [],
  69. 'NOT IN'
  70. ),
  71. ])
  72. ->orderAsc('Articles.id')
  73. ->disableHydration()
  74. ->toArray();
  75. }
  76. public function testInWithMultiResultSubquery(): void
  77. {
  78. $articles = $this->getTableLocator()->get('Articles');
  79. $query = $articles
  80. ->find()
  81. ->select(['Articles.id', 'Articles.author_id'])
  82. ->where([
  83. new TupleComparison(
  84. ['Articles.id', 'Articles.author_id'],
  85. $articles
  86. ->subquery()
  87. ->select(['ArticlesAlias.id', 'ArticlesAlias.author_id'])
  88. ->from(['ArticlesAlias' => $articles->getTable()])
  89. ->where(['ArticlesAlias.author_id' => 1]),
  90. [],
  91. 'IN'
  92. ),
  93. ])
  94. ->orderAsc('Articles.id')
  95. ->disableHydration();
  96. $expected = [
  97. [
  98. 'id' => 1,
  99. 'author_id' => 1,
  100. ],
  101. [
  102. 'id' => 3,
  103. 'author_id' => 1,
  104. ],
  105. ];
  106. $this->assertSame($expected, $query->toArray());
  107. }
  108. public function testInWithSingleResultSubquery(): void
  109. {
  110. $articles = $this->getTableLocator()->get('Articles');
  111. $query = $articles
  112. ->find()
  113. ->select(['Articles.id', 'Articles.author_id'])
  114. ->where([
  115. new TupleComparison(
  116. ['Articles.id', 'Articles.author_id'],
  117. $articles
  118. ->subquery()
  119. ->select(['ArticlesAlias.id', 'ArticlesAlias.author_id'])
  120. ->from(['ArticlesAlias' => $articles->getTable()])
  121. ->where(['ArticlesAlias.id' => 1]),
  122. [],
  123. 'IN'
  124. ),
  125. ])
  126. ->disableHydration();
  127. $expected = [
  128. [
  129. 'id' => 1,
  130. 'author_id' => 1,
  131. ],
  132. ];
  133. $this->assertSame($expected, $query->toArray());
  134. }
  135. public function testInWithMultiArrayValues(): void
  136. {
  137. $articles = $this->getTableLocator()->get('Articles');
  138. $query = $articles
  139. ->find()
  140. ->select(['Articles.id', 'Articles.author_id'])
  141. ->where([
  142. new TupleComparison(
  143. ['Articles.id', 'Articles.author_id'],
  144. [[1, 1], [3, 1]],
  145. ['integer', 'integer'],
  146. 'IN'
  147. ),
  148. ])
  149. ->orderAsc('Articles.id')
  150. ->disableHydration();
  151. $expected = [
  152. [
  153. 'id' => 1,
  154. 'author_id' => 1,
  155. ],
  156. [
  157. 'id' => 3,
  158. 'author_id' => 1,
  159. ],
  160. ];
  161. $this->assertSame($expected, $query->toArray());
  162. }
  163. public function testEqualWithMultiResultSubquery(): void
  164. {
  165. $articles = $this->getTableLocator()->get('Articles');
  166. $driver = $articles->getConnection()->getDriver();
  167. if (
  168. $driver instanceof Mysql ||
  169. $driver instanceof Postgres
  170. ) {
  171. $this->expectException(PDOException::class);
  172. $this->expectExceptionMessageMatches('/cardinality violation/i');
  173. } else {
  174. // Due to the way tuple comparisons are being translated, the DBMS will
  175. // not run into a cardinality violation scenario.
  176. $this->markTestSkipped(
  177. 'Sqlite and Sqlserver currently do not fail with subqueries returning incompatible results.'
  178. );
  179. }
  180. $articles
  181. ->find()
  182. ->select(['Articles.id', 'Articles.author_id'])
  183. ->where([
  184. new TupleComparison(
  185. ['Articles.id', 'Articles.author_id'],
  186. $articles
  187. ->subquery()
  188. ->select(['ArticlesAlias.id', 'ArticlesAlias.author_id'])
  189. ->from(['ArticlesAlias' => $articles->getTable()])
  190. ->where(['ArticlesAlias.author_id' => 1]),
  191. [],
  192. '='
  193. ),
  194. ])
  195. ->orderAsc('Articles.id')
  196. ->disableHydration()
  197. ->toArray();
  198. }
  199. public function testEqualWithSingleResultSubquery(): void
  200. {
  201. $articles = $this->getTableLocator()->get('Articles');
  202. $query = $articles
  203. ->find()
  204. ->select(['Articles.id', 'Articles.author_id'])
  205. ->where([
  206. new TupleComparison(
  207. ['Articles.id', 'Articles.author_id'],
  208. $articles
  209. ->subquery()
  210. ->select(['ArticlesAlias.id', 'ArticlesAlias.author_id'])
  211. ->from(['ArticlesAlias' => $articles->getTable()])
  212. ->where(['ArticlesAlias.id' => 1]),
  213. [],
  214. '='
  215. ),
  216. ])
  217. ->disableHydration();
  218. $expected = [
  219. [
  220. 'id' => 1,
  221. 'author_id' => 1,
  222. ],
  223. ];
  224. $this->assertSame($expected, $query->toArray());
  225. }
  226. public function testEqualWithSingleArrayValue(): void
  227. {
  228. $articles = $this->getTableLocator()->get('Articles');
  229. $query = $articles
  230. ->find()
  231. ->select(['Articles.id', 'Articles.author_id'])
  232. ->where([
  233. new TupleComparison(
  234. ['Articles.id', 'Articles.author_id'],
  235. [1, 1],
  236. ['integer', 'integer'],
  237. '='
  238. ),
  239. ])
  240. ->orderAsc('Articles.id')
  241. ->disableHydration();
  242. $expected = [
  243. [
  244. 'id' => 1,
  245. 'author_id' => 1,
  246. ],
  247. ];
  248. $this->assertSame($expected, $query->toArray());
  249. }
  250. }