1
0

ExpressionBuilderTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\DB\QueryBuilder;
  8. use Doctrine\DBAL\Query\Expression\ExpressionBuilder as DoctrineExpressionBuilder;
  9. use OC\DB\QueryBuilder\ExpressionBuilder\ExpressionBuilder;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use Psr\Log\LoggerInterface;
  12. use Test\TestCase;
  13. /**
  14. * Class ExpressionBuilderTest
  15. *
  16. * @group DB
  17. *
  18. * @package Test\DB\QueryBuilder
  19. */
  20. class ExpressionBuilderTest extends TestCase {
  21. /** @var ExpressionBuilder */
  22. protected $expressionBuilder;
  23. /** @var DoctrineExpressionBuilder */
  24. protected $doctrineExpressionBuilder;
  25. /** @var \OCP\IDBConnection */
  26. protected $connection;
  27. /** @var \Doctrine\DBAL\Connection */
  28. protected $internalConnection;
  29. /** @var LoggerInterface */
  30. protected $logger;
  31. protected function setUp(): void {
  32. parent::setUp();
  33. $this->connection = \OC::$server->getDatabaseConnection();
  34. $this->internalConnection = \OC::$server->get(\OC\DB\Connection::class);
  35. $this->logger = $this->createMock(LoggerInterface::class);
  36. $queryBuilder = $this->createMock(IQueryBuilder::class);
  37. $this->expressionBuilder = new ExpressionBuilder($this->connection, $queryBuilder, $this->logger);
  38. $this->doctrineExpressionBuilder = new DoctrineExpressionBuilder($this->internalConnection);
  39. }
  40. public function dataComparison() {
  41. $valueSets = $this->dataComparisons();
  42. $comparisonOperators = ['=', '<>', '<', '>', '<=', '>='];
  43. $testSets = [];
  44. foreach ($comparisonOperators as $operator) {
  45. foreach ($valueSets as $values) {
  46. $testSets[] = array_merge([$operator], $values);
  47. }
  48. }
  49. return $testSets;
  50. }
  51. /**
  52. * @dataProvider dataComparison
  53. *
  54. * @param string $comparison
  55. * @param mixed $input1
  56. * @param bool $isInput1Literal
  57. * @param mixed $input2
  58. * @param bool $isInput2Literal
  59. */
  60. public function testComparison($comparison, $input1, $isInput1Literal, $input2, $isInput2Literal): void {
  61. [$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
  62. [$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
  63. $this->assertEquals(
  64. $this->doctrineExpressionBuilder->comparison($doctrineInput1, $comparison, $doctrineInput2),
  65. $this->expressionBuilder->comparison($ocInput1, $comparison, $ocInput2)
  66. );
  67. }
  68. public function dataComparisons() {
  69. return [
  70. ['value', false, 'value', false],
  71. ['value', false, 'value', true],
  72. ['value', true, 'value', false],
  73. ['value', true, 'value', true],
  74. ];
  75. }
  76. /**
  77. * @dataProvider dataComparisons
  78. *
  79. * @param mixed $input1
  80. * @param bool $isInput1Literal
  81. * @param mixed $input2
  82. * @param bool $isInput2Literal
  83. */
  84. public function testEquals($input1, $isInput1Literal, $input2, $isInput2Literal): void {
  85. [$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
  86. [$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
  87. $this->assertEquals(
  88. $this->doctrineExpressionBuilder->eq($doctrineInput1, $doctrineInput2),
  89. $this->expressionBuilder->eq($ocInput1, $ocInput2)
  90. );
  91. }
  92. /**
  93. * @dataProvider dataComparisons
  94. *
  95. * @param mixed $input1
  96. * @param bool $isInput1Literal
  97. * @param mixed $input2
  98. * @param bool $isInput2Literal
  99. */
  100. public function testNotEquals($input1, $isInput1Literal, $input2, $isInput2Literal): void {
  101. [$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
  102. [$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
  103. $this->assertEquals(
  104. $this->doctrineExpressionBuilder->neq($doctrineInput1, $doctrineInput2),
  105. $this->expressionBuilder->neq($ocInput1, $ocInput2)
  106. );
  107. }
  108. /**
  109. * @dataProvider dataComparisons
  110. *
  111. * @param mixed $input1
  112. * @param bool $isInput1Literal
  113. * @param mixed $input2
  114. * @param bool $isInput2Literal
  115. */
  116. public function testLowerThan($input1, $isInput1Literal, $input2, $isInput2Literal): void {
  117. [$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
  118. [$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
  119. $this->assertEquals(
  120. $this->doctrineExpressionBuilder->lt($doctrineInput1, $doctrineInput2),
  121. $this->expressionBuilder->lt($ocInput1, $ocInput2)
  122. );
  123. }
  124. /**
  125. * @dataProvider dataComparisons
  126. *
  127. * @param mixed $input1
  128. * @param bool $isInput1Literal
  129. * @param mixed $input2
  130. * @param bool $isInput2Literal
  131. */
  132. public function testLowerThanEquals($input1, $isInput1Literal, $input2, $isInput2Literal): void {
  133. [$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
  134. [$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
  135. $this->assertEquals(
  136. $this->doctrineExpressionBuilder->lte($doctrineInput1, $doctrineInput2),
  137. $this->expressionBuilder->lte($ocInput1, $ocInput2)
  138. );
  139. }
  140. /**
  141. * @dataProvider dataComparisons
  142. *
  143. * @param mixed $input1
  144. * @param bool $isInput1Literal
  145. * @param mixed $input2
  146. * @param bool $isInput2Literal
  147. */
  148. public function testGreaterThan($input1, $isInput1Literal, $input2, $isInput2Literal): void {
  149. [$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
  150. [$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
  151. $this->assertEquals(
  152. $this->doctrineExpressionBuilder->gt($doctrineInput1, $doctrineInput2),
  153. $this->expressionBuilder->gt($ocInput1, $ocInput2)
  154. );
  155. }
  156. /**
  157. * @dataProvider dataComparisons
  158. *
  159. * @param mixed $input1
  160. * @param bool $isInput1Literal
  161. * @param mixed $input2
  162. * @param bool $isInput2Literal
  163. */
  164. public function testGreaterThanEquals($input1, $isInput1Literal, $input2, $isInput2Literal): void {
  165. [$doctrineInput1, $ocInput1] = $this->helpWithLiteral($input1, $isInput1Literal);
  166. [$doctrineInput2, $ocInput2] = $this->helpWithLiteral($input2, $isInput2Literal);
  167. $this->assertEquals(
  168. $this->doctrineExpressionBuilder->gte($doctrineInput1, $doctrineInput2),
  169. $this->expressionBuilder->gte($ocInput1, $ocInput2)
  170. );
  171. }
  172. public function testIsNull(): void {
  173. $this->assertEquals(
  174. $this->doctrineExpressionBuilder->isNull('`test`'),
  175. $this->expressionBuilder->isNull('test')
  176. );
  177. }
  178. public function testIsNotNull(): void {
  179. $this->assertEquals(
  180. $this->doctrineExpressionBuilder->isNotNull('`test`'),
  181. $this->expressionBuilder->isNotNull('test')
  182. );
  183. }
  184. public function dataLike() {
  185. return [
  186. ['value', false],
  187. ['value', true],
  188. ];
  189. }
  190. /**
  191. * @dataProvider dataLike
  192. *
  193. * @param mixed $input
  194. * @param bool $isLiteral
  195. */
  196. public function testLike($input, $isLiteral): void {
  197. [$doctrineInput, $ocInput] = $this->helpWithLiteral($input, $isLiteral);
  198. $this->assertEquals(
  199. $this->doctrineExpressionBuilder->like('`test`', $doctrineInput),
  200. $this->expressionBuilder->like('test', $ocInput)
  201. );
  202. }
  203. /**
  204. * @dataProvider dataLike
  205. *
  206. * @param mixed $input
  207. * @param bool $isLiteral
  208. */
  209. public function testNotLike($input, $isLiteral): void {
  210. [$doctrineInput, $ocInput] = $this->helpWithLiteral($input, $isLiteral);
  211. $this->assertEquals(
  212. $this->doctrineExpressionBuilder->notLike('`test`', $doctrineInput),
  213. $this->expressionBuilder->notLike('test', $ocInput)
  214. );
  215. }
  216. public function dataIn() {
  217. return [
  218. ['value', false],
  219. ['value', true],
  220. [['value'], false],
  221. [['value'], true],
  222. ];
  223. }
  224. /**
  225. * @dataProvider dataIn
  226. *
  227. * @param mixed $input
  228. * @param bool $isLiteral
  229. */
  230. public function testIn($input, $isLiteral): void {
  231. [$doctrineInput, $ocInput] = $this->helpWithLiteral($input, $isLiteral);
  232. $this->assertEquals(
  233. $this->doctrineExpressionBuilder->in('`test`', $doctrineInput),
  234. $this->expressionBuilder->in('test', $ocInput)
  235. );
  236. }
  237. /**
  238. * @dataProvider dataIn
  239. *
  240. * @param mixed $input
  241. * @param bool $isLiteral
  242. */
  243. public function testNotIn($input, $isLiteral): void {
  244. [$doctrineInput, $ocInput] = $this->helpWithLiteral($input, $isLiteral);
  245. $this->assertEquals(
  246. $this->doctrineExpressionBuilder->notIn('`test`', $doctrineInput),
  247. $this->expressionBuilder->notIn('test', $ocInput)
  248. );
  249. }
  250. protected function helpWithLiteral($input, $isLiteral) {
  251. if ($isLiteral) {
  252. if (is_array($input)) {
  253. $doctrineInput = array_map(function ($ident) {
  254. return $this->doctrineExpressionBuilder->literal($ident);
  255. }, $input);
  256. $ocInput = array_map(function ($ident) {
  257. return $this->expressionBuilder->literal($ident);
  258. }, $input);
  259. } else {
  260. $doctrineInput = $this->doctrineExpressionBuilder->literal($input);
  261. $ocInput = $this->expressionBuilder->literal($input);
  262. }
  263. } else {
  264. if (is_array($input)) {
  265. $doctrineInput = array_map(function ($input) {
  266. return '`' . $input . '`';
  267. }, $input);
  268. $ocInput = $input;
  269. } else {
  270. $doctrineInput = '`' . $input . '`';
  271. $ocInput = $input;
  272. }
  273. }
  274. return [$doctrineInput, $ocInput];
  275. }
  276. public function dataLiteral() {
  277. return [
  278. ['value', null],
  279. ['1', null],
  280. [1, null],
  281. [1, 'string'],
  282. [1, 'integer'],
  283. [1, IQueryBuilder::PARAM_INT],
  284. ];
  285. }
  286. /**
  287. * @dataProvider dataLiteral
  288. *
  289. * @param mixed $input
  290. * @param string|null $type
  291. */
  292. public function testLiteral($input, $type): void {
  293. /** @var \OC\DB\QueryBuilder\Literal $actual */
  294. $actual = $this->expressionBuilder->literal($input, $type);
  295. $this->assertInstanceOf('\OC\DB\QueryBuilder\Literal', $actual);
  296. $this->assertEquals(
  297. $this->doctrineExpressionBuilder->literal($input, $type),
  298. $actual->__toString()
  299. );
  300. }
  301. public function dataClobComparisons() {
  302. return [
  303. ['eq', '5', IQueryBuilder::PARAM_STR, false, 3],
  304. ['eq', '5', IQueryBuilder::PARAM_STR, true, 1],
  305. ['neq', '5', IQueryBuilder::PARAM_STR, false, 8],
  306. ['neq', '5', IQueryBuilder::PARAM_STR, true, 6],
  307. ['lt', '5', IQueryBuilder::PARAM_STR, false, 3],
  308. ['lt', '5', IQueryBuilder::PARAM_STR, true, 1],
  309. ['lte', '5', IQueryBuilder::PARAM_STR, false, 6],
  310. ['lte', '5', IQueryBuilder::PARAM_STR, true, 4],
  311. ['gt', '5', IQueryBuilder::PARAM_STR, false, 5],
  312. ['gt', '5', IQueryBuilder::PARAM_STR, true, 1],
  313. ['gte', '5', IQueryBuilder::PARAM_STR, false, 8],
  314. ['gte', '5', IQueryBuilder::PARAM_STR, true, 4],
  315. ['like', '%5%', IQueryBuilder::PARAM_STR, false, 3],
  316. ['like', '%5%', IQueryBuilder::PARAM_STR, true, 1],
  317. ['like', 'under_%', IQueryBuilder::PARAM_STR, false, 2],
  318. ['like', 'under\_%', IQueryBuilder::PARAM_STR, false, 1],
  319. ['notLike', '%5%', IQueryBuilder::PARAM_STR, false, 8],
  320. ['notLike', '%5%', IQueryBuilder::PARAM_STR, true, 6],
  321. ['in', ['5'], IQueryBuilder::PARAM_STR_ARRAY, false, 3],
  322. ['in', ['5'], IQueryBuilder::PARAM_STR_ARRAY, true, 1],
  323. ['notIn', ['5'], IQueryBuilder::PARAM_STR_ARRAY, false, 8],
  324. ['notIn', ['5'], IQueryBuilder::PARAM_STR_ARRAY, true, 6],
  325. ];
  326. }
  327. /**
  328. * @dataProvider dataClobComparisons
  329. * @param string $function
  330. * @param mixed $value
  331. * @param mixed $type
  332. * @param bool $compareKeyToValue
  333. * @param int $expected
  334. */
  335. public function testClobComparisons($function, $value, $type, $compareKeyToValue, $expected): void {
  336. $appId = $this->getUniqueID('testing');
  337. $this->createConfig($appId, 1, 4);
  338. $this->createConfig($appId, 2, 5);
  339. $this->createConfig($appId, 3, 6);
  340. $this->createConfig($appId, 4, 4);
  341. $this->createConfig($appId, 5, 5);
  342. $this->createConfig($appId, 6, 6);
  343. $this->createConfig($appId, 7, 4);
  344. $this->createConfig($appId, 8, 5);
  345. $this->createConfig($appId, 9, 6);
  346. $this->createConfig($appId, 10, 'under_score');
  347. $this->createConfig($appId, 11, 'underscore');
  348. $query = $this->connection->getQueryBuilder();
  349. $query->select($query->func()->count('*', 'count'))
  350. ->from('appconfig')
  351. ->where($query->expr()->eq('appid', $query->createNamedParameter($appId)))
  352. ->andWhere(call_user_func([$query->expr(), $function], 'configvalue', $query->createNamedParameter($value, $type), IQueryBuilder::PARAM_STR));
  353. if ($compareKeyToValue) {
  354. $query->andWhere(call_user_func([$query->expr(), $function], 'configkey', 'configvalue', IQueryBuilder::PARAM_STR));
  355. }
  356. $result = $query->execute();
  357. $this->assertEquals(['count' => $expected], $result->fetch());
  358. $result->closeCursor();
  359. $query = $this->connection->getQueryBuilder();
  360. $query->delete('appconfig')
  361. ->where($query->expr()->eq('appid', $query->createNamedParameter($appId)))
  362. ->execute();
  363. }
  364. protected function createConfig($appId, $key, $value) {
  365. $query = $this->connection->getQueryBuilder();
  366. $query->insert('appconfig')
  367. ->values([
  368. 'appid' => $query->createNamedParameter($appId),
  369. 'configkey' => $query->createNamedParameter((string)$key),
  370. 'configvalue' => $query->createNamedParameter((string)$value),
  371. ])
  372. ->execute();
  373. }
  374. }