1
0

ExpressionBuilderTest.php 13 KB

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