ExpressionBuilderTest.php 13 KB

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