1
0

QueryBuilderTest.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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\CompositeExpression;
  23. use OC\DB\QueryBuilder\Literal;
  24. use OC\DB\QueryBuilder\Parameter;
  25. use OC\DB\QueryBuilder\QueryBuilder;
  26. use OC\SystemConfig;
  27. use OCP\IDBConnection;
  28. use OCP\ILogger;
  29. /**
  30. * Class QueryBuilderTest
  31. *
  32. * @group DB
  33. *
  34. * @package Test\DB\QueryBuilder
  35. */
  36. class QueryBuilderTest extends \Test\TestCase {
  37. /** @var QueryBuilder */
  38. protected $queryBuilder;
  39. /** @var IDBConnection */
  40. protected $connection;
  41. /** @var SystemConfig|\PHPUnit_Framework_MockObject_MockObject */
  42. protected $config;
  43. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  44. protected $logger;
  45. protected function setUp() {
  46. parent::setUp();
  47. $this->connection = \OC::$server->getDatabaseConnection();
  48. $this->config = $this->createMock(SystemConfig::class);
  49. $this->logger = $this->createMock(ILogger::class);
  50. $this->queryBuilder = new QueryBuilder($this->connection, $this->config, $this->logger);
  51. }
  52. protected function createTestingRows($appId = 'testFirstResult') {
  53. $qB = $this->connection->getQueryBuilder();
  54. for ($i = 1; $i < 10; $i++) {
  55. $qB->insert('*PREFIX*appconfig')
  56. ->values([
  57. 'appid' => $qB->expr()->literal($appId),
  58. 'configkey' => $qB->expr()->literal('testing' . $i),
  59. 'configvalue' => $qB->expr()->literal(100 - $i),
  60. ])
  61. ->execute();
  62. }
  63. }
  64. protected function getTestingRows(QueryBuilder $queryBuilder) {
  65. $queryBuilder->select('configvalue')
  66. ->from('*PREFIX*appconfig')
  67. ->where($queryBuilder->expr()->eq(
  68. 'appid',
  69. $queryBuilder->expr()->literal('testFirstResult')
  70. ))
  71. ->orderBy('configkey', 'ASC');
  72. $query = $queryBuilder->execute();
  73. $rows = [];
  74. while ($row = $query->fetch()) {
  75. $rows[] = $row['configvalue'];
  76. }
  77. $query->closeCursor();
  78. return $rows;
  79. }
  80. protected function deleteTestingRows($appId = 'testFirstResult') {
  81. $qB = $this->connection->getQueryBuilder();
  82. $qB->delete('*PREFIX*appconfig')
  83. ->where($qB->expr()->eq('appid', $qB->expr()->literal($appId)))
  84. ->execute();
  85. }
  86. public function dataFirstResult() {
  87. return [
  88. [null, [99, 98, 97, 96, 95, 94, 93, 92, 91]],
  89. [0, [99, 98, 97, 96, 95, 94, 93, 92, 91]],
  90. [1, [98, 97, 96, 95, 94, 93, 92, 91]],
  91. [5, [94, 93, 92, 91]],
  92. ];
  93. }
  94. /**
  95. * @dataProvider dataFirstResult
  96. *
  97. * @param int $firstResult
  98. * @param array $expectedSet
  99. */
  100. public function testFirstResult($firstResult, $expectedSet) {
  101. $this->deleteTestingRows();
  102. $this->createTestingRows();
  103. if ($firstResult !== null) {
  104. $this->queryBuilder->setFirstResult($firstResult);
  105. // FIXME Remove this once Doctrine/DBAL is >2.5.1:
  106. // FIXME See https://github.com/doctrine/dbal/pull/782
  107. $this->queryBuilder->setMaxResults(100);
  108. }
  109. $this->assertSame(
  110. $firstResult,
  111. $this->queryBuilder->getFirstResult()
  112. );
  113. $rows = $this->getTestingRows($this->queryBuilder);
  114. $this->assertCount(sizeof($expectedSet), $rows);
  115. $this->assertEquals($expectedSet, $rows);
  116. $this->deleteTestingRows();
  117. }
  118. public function dataMaxResults() {
  119. return [
  120. [null, [99, 98, 97, 96, 95, 94, 93, 92, 91]],
  121. // Limit 0 gives mixed results: either all entries or none is returned
  122. //[0, []],
  123. [1, [99]],
  124. [5, [99, 98, 97, 96, 95]],
  125. ];
  126. }
  127. /**
  128. * @dataProvider dataMaxResults
  129. *
  130. * @param int $maxResult
  131. * @param array $expectedSet
  132. */
  133. public function testMaxResults($maxResult, $expectedSet) {
  134. $this->deleteTestingRows();
  135. $this->createTestingRows();
  136. if ($maxResult !== null) {
  137. $this->queryBuilder->setMaxResults($maxResult);
  138. }
  139. $this->assertSame(
  140. $maxResult,
  141. $this->queryBuilder->getMaxResults()
  142. );
  143. $rows = $this->getTestingRows($this->queryBuilder);
  144. $this->assertCount(sizeof($expectedSet), $rows);
  145. $this->assertEquals($expectedSet, $rows);
  146. $this->deleteTestingRows();
  147. }
  148. public function dataSelect() {
  149. $config = $this->createMock(SystemConfig::class);
  150. $logger = $this->createMock(ILogger::class);
  151. $queryBuilder = new QueryBuilder(\OC::$server->getDatabaseConnection(), $config, $logger);
  152. return [
  153. // select('column1')
  154. [['configvalue'], ['configvalue' => '99']],
  155. // select('column1', 'column2')
  156. [['configvalue', 'configkey'], ['configvalue' => '99', 'configkey' => 'testing1']],
  157. // select(['column1', 'column2'])
  158. [[['configvalue', 'configkey']], ['configvalue' => '99', 'configkey' => 'testing1']],
  159. // select(new Literal('column1'))
  160. [[$queryBuilder->expr()->literal('column1')], [], 'column1'],
  161. // select('column1', 'column2')
  162. [[$queryBuilder->expr()->literal('column1'), 'configkey'], ['configkey' => 'testing1'], 'column1'],
  163. // select(['column1', 'column2'])
  164. [[[$queryBuilder->expr()->literal('column1'), 'configkey']], ['configkey' => 'testing1'], 'column1'],
  165. ];
  166. }
  167. /**
  168. * @dataProvider dataSelect
  169. *
  170. * @param array $selectArguments
  171. * @param array $expected
  172. * @param string $expectedLiteral
  173. */
  174. public function testSelect($selectArguments, $expected, $expectedLiteral = '') {
  175. $this->deleteTestingRows();
  176. $this->createTestingRows();
  177. call_user_func_array(
  178. [$this->queryBuilder, 'select'],
  179. $selectArguments
  180. );
  181. $this->queryBuilder->from('*PREFIX*appconfig')
  182. ->where($this->queryBuilder->expr()->eq(
  183. 'appid',
  184. $this->queryBuilder->expr()->literal('testFirstResult')
  185. ))
  186. ->orderBy('configkey', 'ASC')
  187. ->setMaxResults(1);
  188. $query = $this->queryBuilder->execute();
  189. $row = $query->fetch();
  190. $query->closeCursor();
  191. foreach ($expected as $key => $value) {
  192. $this->assertArrayHasKey($key, $row);
  193. $this->assertEquals($value, $row[$key]);
  194. unset($row[$key]);
  195. }
  196. if ($expectedLiteral) {
  197. $this->assertEquals([$expectedLiteral], array_values($row));
  198. } else {
  199. $this->assertEmpty($row);
  200. }
  201. $this->deleteTestingRows();
  202. }
  203. public function dataSelectAlias() {
  204. $config = $this->createMock(SystemConfig::class);
  205. $logger = $this->createMock(ILogger::class);
  206. $queryBuilder = new QueryBuilder(\OC::$server->getDatabaseConnection(), $config, $logger);
  207. return [
  208. ['configvalue', 'cv', ['cv' => '99']],
  209. [$queryBuilder->expr()->literal('column1'), 'thing', ['thing' => 'column1']],
  210. ];
  211. }
  212. /**
  213. * @dataProvider dataSelectAlias
  214. *
  215. * @param mixed $select
  216. * @param array $alias
  217. * @param array $expected
  218. */
  219. public function testSelectAlias($select, $alias, $expected) {
  220. $this->deleteTestingRows();
  221. $this->createTestingRows();
  222. $this->queryBuilder->selectAlias($select, $alias);
  223. $this->queryBuilder->from('*PREFIX*appconfig')
  224. ->where($this->queryBuilder->expr()->eq(
  225. 'appid',
  226. $this->queryBuilder->expr()->literal('testFirstResult')
  227. ))
  228. ->orderBy('configkey', 'ASC')
  229. ->setMaxResults(1);
  230. $query = $this->queryBuilder->execute();
  231. $row = $query->fetch();
  232. $query->closeCursor();
  233. $this->assertEquals(
  234. $expected,
  235. $row
  236. );
  237. $this->deleteTestingRows();
  238. }
  239. public function testSelectDistinct() {
  240. $this->deleteTestingRows('testFirstResult1');
  241. $this->deleteTestingRows('testFirstResult2');
  242. $this->createTestingRows('testFirstResult1');
  243. $this->createTestingRows('testFirstResult2');
  244. $this->queryBuilder->selectDistinct('appid');
  245. $this->queryBuilder->from('*PREFIX*appconfig')
  246. ->where($this->queryBuilder->expr()->in(
  247. 'appid',
  248. [$this->queryBuilder->expr()->literal('testFirstResult1'), $this->queryBuilder->expr()->literal('testFirstResult2')]
  249. ))
  250. ->orderBy('appid', 'DESC');
  251. $query = $this->queryBuilder->execute();
  252. $rows = $query->fetchAll();
  253. $query->closeCursor();
  254. $this->assertEquals(
  255. [['appid' => 'testFirstResult2'], ['appid' => 'testFirstResult1']],
  256. $rows
  257. );
  258. $this->deleteTestingRows('testFirstResult1');
  259. $this->deleteTestingRows('testFirstResult2');
  260. }
  261. public function dataAddSelect() {
  262. $config = $this->createMock(SystemConfig::class);
  263. $logger = $this->createMock(ILogger::class);
  264. $queryBuilder = new QueryBuilder(\OC::$server->getDatabaseConnection(), $config, $logger);
  265. return [
  266. // addSelect('column1')
  267. [['configvalue'], ['appid' => 'testFirstResult', 'configvalue' => '99']],
  268. // addSelect('column1', 'column2')
  269. [['configvalue', 'configkey'], ['appid' => 'testFirstResult', 'configvalue' => '99', 'configkey' => 'testing1']],
  270. // addSelect(['column1', 'column2'])
  271. [[['configvalue', 'configkey']], ['appid' => 'testFirstResult', 'configvalue' => '99', 'configkey' => 'testing1']],
  272. // select(new Literal('column1'))
  273. [[$queryBuilder->expr()->literal('column1')], ['appid' => 'testFirstResult'], 'column1'],
  274. // select('column1', 'column2')
  275. [[$queryBuilder->expr()->literal('column1'), 'configkey'], ['appid' => 'testFirstResult', 'configkey' => 'testing1'], 'column1'],
  276. // select(['column1', 'column2'])
  277. [[[$queryBuilder->expr()->literal('column1'), 'configkey']], ['appid' => 'testFirstResult', 'configkey' => 'testing1'], 'column1'],
  278. ];
  279. }
  280. /**
  281. * @dataProvider dataAddSelect
  282. *
  283. * @param array $selectArguments
  284. * @param array $expected
  285. * @param string $expectedLiteral
  286. */
  287. public function testAddSelect($selectArguments, $expected, $expectedLiteral = '') {
  288. $this->deleteTestingRows();
  289. $this->createTestingRows();
  290. $this->queryBuilder->select('appid');
  291. call_user_func_array(
  292. [$this->queryBuilder, 'addSelect'],
  293. $selectArguments
  294. );
  295. $this->queryBuilder->from('*PREFIX*appconfig')
  296. ->where($this->queryBuilder->expr()->eq(
  297. 'appid',
  298. $this->queryBuilder->expr()->literal('testFirstResult')
  299. ))
  300. ->orderBy('configkey', 'ASC')
  301. ->setMaxResults(1);
  302. $query = $this->queryBuilder->execute();
  303. $row = $query->fetch();
  304. $query->closeCursor();
  305. foreach ($expected as $key => $value) {
  306. $this->assertArrayHasKey($key, $row);
  307. $this->assertEquals($value, $row[$key]);
  308. unset($row[$key]);
  309. }
  310. if ($expectedLiteral) {
  311. $this->assertEquals([$expectedLiteral], array_values($row));
  312. } else {
  313. $this->assertEmpty($row);
  314. }
  315. $this->deleteTestingRows();
  316. }
  317. public function dataDelete() {
  318. return [
  319. ['data', null, ['table' => '`*PREFIX*data`', 'alias' => null], '`*PREFIX*data`'],
  320. ['data', 't', ['table' => '`*PREFIX*data`', 'alias' => 't'], '`*PREFIX*data` t'],
  321. ];
  322. }
  323. /**
  324. * @dataProvider dataDelete
  325. *
  326. * @param string $tableName
  327. * @param string $tableAlias
  328. * @param array $expectedQueryPart
  329. * @param string $expectedQuery
  330. */
  331. public function testDelete($tableName, $tableAlias, $expectedQueryPart, $expectedQuery) {
  332. $this->queryBuilder->delete($tableName, $tableAlias);
  333. $this->assertSame(
  334. $expectedQueryPart,
  335. $this->queryBuilder->getQueryPart('from')
  336. );
  337. $this->assertSame(
  338. 'DELETE FROM ' . $expectedQuery,
  339. $this->queryBuilder->getSQL()
  340. );
  341. }
  342. public function dataUpdate() {
  343. return [
  344. ['data', null, ['table' => '`*PREFIX*data`', 'alias' => null], '`*PREFIX*data`'],
  345. ['data', 't', ['table' => '`*PREFIX*data`', 'alias' => 't'], '`*PREFIX*data` t'],
  346. ];
  347. }
  348. /**
  349. * @dataProvider dataUpdate
  350. *
  351. * @param string $tableName
  352. * @param string $tableAlias
  353. * @param array $expectedQueryPart
  354. * @param string $expectedQuery
  355. */
  356. public function testUpdate($tableName, $tableAlias, $expectedQueryPart, $expectedQuery) {
  357. $this->queryBuilder->update($tableName, $tableAlias);
  358. $this->assertSame(
  359. $expectedQueryPart,
  360. $this->queryBuilder->getQueryPart('from')
  361. );
  362. $this->assertSame(
  363. 'UPDATE ' . $expectedQuery . ' SET ',
  364. $this->queryBuilder->getSQL()
  365. );
  366. }
  367. public function dataInsert() {
  368. return [
  369. ['data', ['table' => '`*PREFIX*data`'], '`*PREFIX*data`'],
  370. ];
  371. }
  372. /**
  373. * @dataProvider dataInsert
  374. *
  375. * @param string $tableName
  376. * @param array $expectedQueryPart
  377. * @param string $expectedQuery
  378. */
  379. public function testInsert($tableName, $expectedQueryPart, $expectedQuery) {
  380. $this->queryBuilder->insert($tableName);
  381. $this->assertSame(
  382. $expectedQueryPart,
  383. $this->queryBuilder->getQueryPart('from')
  384. );
  385. $this->assertSame(
  386. 'INSERT INTO ' . $expectedQuery . ' () VALUES()',
  387. $this->queryBuilder->getSQL()
  388. );
  389. }
  390. public function dataFrom() {
  391. return [
  392. ['data', null, null, null, [['table' => '`*PREFIX*data`', 'alias' => null]], '`*PREFIX*data`'],
  393. ['data', 't', null, null, [['table' => '`*PREFIX*data`', 'alias' => '`t`']], '`*PREFIX*data` `t`'],
  394. ['data1', null, 'data2', null, [
  395. ['table' => '`*PREFIX*data1`', 'alias' => null],
  396. ['table' => '`*PREFIX*data2`', 'alias' => null]
  397. ], '`*PREFIX*data1`, `*PREFIX*data2`'],
  398. ['data', 't1', 'data', 't2', [
  399. ['table' => '`*PREFIX*data`', 'alias' => '`t1`'],
  400. ['table' => '`*PREFIX*data`', 'alias' => '`t2`']
  401. ], '`*PREFIX*data` `t1`, `*PREFIX*data` `t2`'],
  402. ];
  403. }
  404. /**
  405. * @dataProvider dataFrom
  406. *
  407. * @param string $table1Name
  408. * @param string $table1Alias
  409. * @param string $table2Name
  410. * @param string $table2Alias
  411. * @param array $expectedQueryPart
  412. * @param string $expectedQuery
  413. */
  414. public function testFrom($table1Name, $table1Alias, $table2Name, $table2Alias, $expectedQueryPart, $expectedQuery) {
  415. $this->queryBuilder->from($table1Name, $table1Alias);
  416. if ($table2Name !== null) {
  417. $this->queryBuilder->from($table2Name, $table2Alias);
  418. }
  419. $this->assertSame(
  420. $expectedQueryPart,
  421. $this->queryBuilder->getQueryPart('from')
  422. );
  423. $this->assertSame(
  424. 'SELECT FROM ' . $expectedQuery,
  425. $this->queryBuilder->getSQL()
  426. );
  427. }
  428. public function dataJoin() {
  429. return [
  430. [
  431. 'd1', 'data2', null, null,
  432. ['`d1`' => [['joinType' => 'inner', 'joinTable' => '`*PREFIX*data2`', 'joinAlias' => null, 'joinCondition' => null]]],
  433. '`*PREFIX*data1` `d1` INNER JOIN `*PREFIX*data2` ON '
  434. ],
  435. [
  436. 'd1', 'data2', 'd2', null,
  437. ['`d1`' => [['joinType' => 'inner', 'joinTable' => '`*PREFIX*data2`', 'joinAlias' => '`d2`', 'joinCondition' => null]]],
  438. '`*PREFIX*data1` `d1` INNER JOIN `*PREFIX*data2` `d2` ON '
  439. ],
  440. [
  441. 'd1', 'data2', 'd2', '`d1`.`field1` = `d2`.`field2`',
  442. ['`d1`' => [['joinType' => 'inner', 'joinTable' => '`*PREFIX*data2`', 'joinAlias' => '`d2`', 'joinCondition' => '`d1`.`field1` = `d2`.`field2`']]],
  443. '`*PREFIX*data1` `d1` INNER JOIN `*PREFIX*data2` `d2` ON `d1`.`field1` = `d2`.`field2`'
  444. ],
  445. ];
  446. }
  447. /**
  448. * @dataProvider dataJoin
  449. *
  450. * @param string $fromAlias
  451. * @param string $tableName
  452. * @param string $tableAlias
  453. * @param string $condition
  454. * @param array $expectedQueryPart
  455. * @param string $expectedQuery
  456. */
  457. public function testJoin($fromAlias, $tableName, $tableAlias, $condition, $expectedQueryPart, $expectedQuery) {
  458. $this->queryBuilder->from('data1', 'd1');
  459. $this->queryBuilder->join(
  460. $fromAlias,
  461. $tableName,
  462. $tableAlias,
  463. $condition
  464. );
  465. $this->assertSame(
  466. $expectedQueryPart,
  467. $this->queryBuilder->getQueryPart('join')
  468. );
  469. $this->assertSame(
  470. 'SELECT FROM ' . $expectedQuery,
  471. $this->queryBuilder->getSQL()
  472. );
  473. }
  474. /**
  475. * @dataProvider dataJoin
  476. *
  477. * @param string $fromAlias
  478. * @param string $tableName
  479. * @param string $tableAlias
  480. * @param string $condition
  481. * @param array $expectedQueryPart
  482. * @param string $expectedQuery
  483. */
  484. public function testInnerJoin($fromAlias, $tableName, $tableAlias, $condition, $expectedQueryPart, $expectedQuery) {
  485. $this->queryBuilder->from('data1', 'd1');
  486. $this->queryBuilder->innerJoin(
  487. $fromAlias,
  488. $tableName,
  489. $tableAlias,
  490. $condition
  491. );
  492. $this->assertSame(
  493. $expectedQueryPart,
  494. $this->queryBuilder->getQueryPart('join')
  495. );
  496. $this->assertSame(
  497. 'SELECT FROM ' . $expectedQuery,
  498. $this->queryBuilder->getSQL()
  499. );
  500. }
  501. public function dataLeftJoin() {
  502. return [
  503. [
  504. 'd1', 'data2', null, null,
  505. ['`d1`' => [['joinType' => 'left', 'joinTable' => '`*PREFIX*data2`', 'joinAlias' => null, 'joinCondition' => null]]],
  506. '`*PREFIX*data1` `d1` LEFT JOIN `*PREFIX*data2` ON '
  507. ],
  508. [
  509. 'd1', 'data2', 'd2', null,
  510. ['`d1`' => [['joinType' => 'left', 'joinTable' => '`*PREFIX*data2`', 'joinAlias' => '`d2`', 'joinCondition' => null]]],
  511. '`*PREFIX*data1` `d1` LEFT JOIN `*PREFIX*data2` `d2` ON '
  512. ],
  513. [
  514. 'd1', 'data2', 'd2', '`d1`.`field1` = `d2`.`field2`',
  515. ['`d1`' => [['joinType' => 'left', 'joinTable' => '`*PREFIX*data2`', 'joinAlias' => '`d2`', 'joinCondition' => '`d1`.`field1` = `d2`.`field2`']]],
  516. '`*PREFIX*data1` `d1` LEFT JOIN `*PREFIX*data2` `d2` ON `d1`.`field1` = `d2`.`field2`'
  517. ],
  518. ];
  519. }
  520. /**
  521. * @dataProvider dataLeftJoin
  522. *
  523. * @param string $fromAlias
  524. * @param string $tableName
  525. * @param string $tableAlias
  526. * @param string $condition
  527. * @param array $expectedQueryPart
  528. * @param string $expectedQuery
  529. */
  530. public function testLeftJoin($fromAlias, $tableName, $tableAlias, $condition, $expectedQueryPart, $expectedQuery) {
  531. $this->queryBuilder->from('data1', 'd1');
  532. $this->queryBuilder->leftJoin(
  533. $fromAlias,
  534. $tableName,
  535. $tableAlias,
  536. $condition
  537. );
  538. $this->assertSame(
  539. $expectedQueryPart,
  540. $this->queryBuilder->getQueryPart('join')
  541. );
  542. $this->assertSame(
  543. 'SELECT FROM ' . $expectedQuery,
  544. $this->queryBuilder->getSQL()
  545. );
  546. }
  547. public function dataRightJoin() {
  548. return [
  549. [
  550. 'd1', 'data2', null, null,
  551. ['`d1`' => [['joinType' => 'right', 'joinTable' => '`*PREFIX*data2`', 'joinAlias' => null, 'joinCondition' => null]]],
  552. '`*PREFIX*data1` `d1` RIGHT JOIN `*PREFIX*data2` ON '
  553. ],
  554. [
  555. 'd1', 'data2', 'd2', null,
  556. ['`d1`' => [['joinType' => 'right', 'joinTable' => '`*PREFIX*data2`', 'joinAlias' => '`d2`', 'joinCondition' => null]]],
  557. '`*PREFIX*data1` `d1` RIGHT JOIN `*PREFIX*data2` `d2` ON '
  558. ],
  559. [
  560. 'd1', 'data2', 'd2', '`d1`.`field1` = `d2`.`field2`',
  561. ['`d1`' => [['joinType' => 'right', 'joinTable' => '`*PREFIX*data2`', 'joinAlias' => '`d2`', 'joinCondition' => '`d1`.`field1` = `d2`.`field2`']]],
  562. '`*PREFIX*data1` `d1` RIGHT JOIN `*PREFIX*data2` `d2` ON `d1`.`field1` = `d2`.`field2`'
  563. ],
  564. ];
  565. }
  566. /**
  567. * @dataProvider dataRightJoin
  568. *
  569. * @param string $fromAlias
  570. * @param string $tableName
  571. * @param string $tableAlias
  572. * @param string $condition
  573. * @param array $expectedQueryPart
  574. * @param string $expectedQuery
  575. */
  576. public function testRightJoin($fromAlias, $tableName, $tableAlias, $condition, $expectedQueryPart, $expectedQuery) {
  577. $this->queryBuilder->from('data1', 'd1');
  578. $this->queryBuilder->rightJoin(
  579. $fromAlias,
  580. $tableName,
  581. $tableAlias,
  582. $condition
  583. );
  584. $this->assertSame(
  585. $expectedQueryPart,
  586. $this->queryBuilder->getQueryPart('join')
  587. );
  588. $this->assertSame(
  589. 'SELECT FROM ' . $expectedQuery,
  590. $this->queryBuilder->getSQL()
  591. );
  592. }
  593. public function dataSet() {
  594. return [
  595. ['column1', new Literal('value'), null, null, ['`column1` = value'], '`column1` = value'],
  596. ['column1', new Parameter(':param'), null, null, ['`column1` = :param'], '`column1` = :param'],
  597. ['column1', 'column2', null, null, ['`column1` = `column2`'], '`column1` = `column2`'],
  598. ['column1', 'column2', 'column3', new Literal('value'), ['`column1` = `column2`', '`column3` = value'], '`column1` = `column2`, `column3` = value'],
  599. ];
  600. }
  601. /**
  602. * @dataProvider dataSet
  603. *
  604. * @param string $partOne1
  605. * @param string $partOne2
  606. * @param string $partTwo1
  607. * @param string $partTwo2
  608. * @param array $expectedQueryPart
  609. * @param string $expectedQuery
  610. */
  611. public function testSet($partOne1, $partOne2, $partTwo1, $partTwo2, $expectedQueryPart, $expectedQuery) {
  612. $this->queryBuilder->update('data');
  613. $this->queryBuilder->set($partOne1, $partOne2);
  614. if ($partTwo1 !== null) {
  615. $this->queryBuilder->set($partTwo1, $partTwo2);
  616. }
  617. $this->assertSame(
  618. $expectedQueryPart,
  619. $this->queryBuilder->getQueryPart('set')
  620. );
  621. $this->assertSame(
  622. 'UPDATE `*PREFIX*data` SET ' . $expectedQuery,
  623. $this->queryBuilder->getSQL()
  624. );
  625. }
  626. public function dataWhere() {
  627. return [
  628. [['where1'], new CompositeExpression('AND', ['where1']), 'where1'],
  629. [['where1', 'where2'], new CompositeExpression('AND', ['where1', 'where2']), '(where1) AND (where2)'],
  630. ];
  631. }
  632. /**
  633. * @dataProvider dataWhere
  634. *
  635. * @param array $whereArguments
  636. * @param array $expectedQueryPart
  637. * @param string $expectedQuery
  638. */
  639. public function testWhere($whereArguments, $expectedQueryPart, $expectedQuery) {
  640. $this->queryBuilder->select('column');
  641. call_user_func_array(
  642. [$this->queryBuilder, 'where'],
  643. $whereArguments
  644. );
  645. $this->assertEquals(
  646. $expectedQueryPart,
  647. $this->queryBuilder->getQueryPart('where')
  648. );
  649. $this->assertSame(
  650. 'SELECT `column` WHERE ' . $expectedQuery,
  651. $this->queryBuilder->getSQL()
  652. );
  653. }
  654. /**
  655. * @dataProvider dataWhere
  656. *
  657. * @param array $whereArguments
  658. * @param array $expectedQueryPart
  659. * @param string $expectedQuery
  660. */
  661. public function testAndWhere($whereArguments, $expectedQueryPart, $expectedQuery) {
  662. $this->queryBuilder->select('column');
  663. call_user_func_array(
  664. [$this->queryBuilder, 'andWhere'],
  665. $whereArguments
  666. );
  667. $this->assertEquals(
  668. $expectedQueryPart,
  669. $this->queryBuilder->getQueryPart('where')
  670. );
  671. $this->assertSame(
  672. 'SELECT `column` WHERE ' . $expectedQuery,
  673. $this->queryBuilder->getSQL()
  674. );
  675. }
  676. public function dataOrWhere() {
  677. return [
  678. [['where1'], new CompositeExpression('OR', ['where1']), 'where1'],
  679. [['where1', 'where2'], new CompositeExpression('OR', ['where1', 'where2']), '(where1) OR (where2)'],
  680. ];
  681. }
  682. /**
  683. * @dataProvider dataOrWhere
  684. *
  685. * @param array $whereArguments
  686. * @param array $expectedQueryPart
  687. * @param string $expectedQuery
  688. */
  689. public function testOrWhere($whereArguments, $expectedQueryPart, $expectedQuery) {
  690. $this->queryBuilder->select('column');
  691. call_user_func_array(
  692. [$this->queryBuilder, 'orWhere'],
  693. $whereArguments
  694. );
  695. $this->assertEquals(
  696. $expectedQueryPart,
  697. $this->queryBuilder->getQueryPart('where')
  698. );
  699. $this->assertSame(
  700. 'SELECT `column` WHERE ' . $expectedQuery,
  701. $this->queryBuilder->getSQL()
  702. );
  703. }
  704. public function dataGroupBy() {
  705. return [
  706. [['column1'], ['`column1`'], '`column1`'],
  707. [['column1', 'column2'], ['`column1`', '`column2`'], '`column1`, `column2`'],
  708. ];
  709. }
  710. /**
  711. * @dataProvider dataGroupBy
  712. *
  713. * @param array $groupByArguments
  714. * @param array $expectedQueryPart
  715. * @param string $expectedQuery
  716. */
  717. public function testGroupBy($groupByArguments, $expectedQueryPart, $expectedQuery) {
  718. $this->queryBuilder->select('column');
  719. call_user_func_array(
  720. [$this->queryBuilder, 'groupBy'],
  721. $groupByArguments
  722. );
  723. $this->assertEquals(
  724. $expectedQueryPart,
  725. $this->queryBuilder->getQueryPart('groupBy')
  726. );
  727. $this->assertSame(
  728. 'SELECT `column` GROUP BY ' . $expectedQuery,
  729. $this->queryBuilder->getSQL()
  730. );
  731. }
  732. public function dataAddGroupBy() {
  733. return [
  734. [['column2'], ['`column1`', '`column2`'], '`column1`, `column2`'],
  735. [['column2', 'column3'], ['`column1`', '`column2`', '`column3`'], '`column1`, `column2`, `column3`'],
  736. ];
  737. }
  738. /**
  739. * @dataProvider dataAddGroupBy
  740. *
  741. * @param array $groupByArguments
  742. * @param array $expectedQueryPart
  743. * @param string $expectedQuery
  744. */
  745. public function testAddGroupBy($groupByArguments, $expectedQueryPart, $expectedQuery) {
  746. $this->queryBuilder->select('column');
  747. $this->queryBuilder->groupBy('column1');
  748. call_user_func_array(
  749. [$this->queryBuilder, 'addGroupBy'],
  750. $groupByArguments
  751. );
  752. $this->assertEquals(
  753. $expectedQueryPart,
  754. $this->queryBuilder->getQueryPart('groupBy')
  755. );
  756. $this->assertSame(
  757. 'SELECT `column` GROUP BY ' . $expectedQuery,
  758. $this->queryBuilder->getSQL()
  759. );
  760. }
  761. public function dataSetValue() {
  762. return [
  763. ['column', 'value', ['`column`' => 'value'], '(`column`) VALUES(value)'],
  764. ];
  765. }
  766. /**
  767. * @dataProvider dataSetValue
  768. *
  769. * @param string $column
  770. * @param string $value
  771. * @param array $expectedQueryPart
  772. * @param string $expectedQuery
  773. */
  774. public function testSetValue($column, $value, $expectedQueryPart, $expectedQuery) {
  775. $this->queryBuilder->insert('data');
  776. $this->queryBuilder->setValue($column, $value);
  777. $this->assertEquals(
  778. $expectedQueryPart,
  779. $this->queryBuilder->getQueryPart('values')
  780. );
  781. $this->assertSame(
  782. 'INSERT INTO `*PREFIX*data` ' . $expectedQuery,
  783. $this->queryBuilder->getSQL()
  784. );
  785. }
  786. /**
  787. * @dataProvider dataSetValue
  788. *
  789. * @param string $column
  790. * @param string $value
  791. * @param array $expectedQueryPart
  792. * @param string $expectedQuery
  793. */
  794. public function testValues($column, $value, $expectedQueryPart, $expectedQuery) {
  795. $this->queryBuilder->insert('data');
  796. $this->queryBuilder->values([
  797. $column => $value,
  798. ]);
  799. $this->assertEquals(
  800. $expectedQueryPart,
  801. $this->queryBuilder->getQueryPart('values')
  802. );
  803. $this->assertSame(
  804. 'INSERT INTO `*PREFIX*data` ' . $expectedQuery,
  805. $this->queryBuilder->getSQL()
  806. );
  807. }
  808. public function dataHaving() {
  809. return [
  810. [['condition1'], new CompositeExpression('AND', ['condition1']), 'HAVING condition1'],
  811. [['condition1', 'condition2'], new CompositeExpression('AND', ['condition1', 'condition2']), 'HAVING (condition1) AND (condition2)'],
  812. [
  813. [new CompositeExpression('OR', ['condition1', 'condition2'])],
  814. new CompositeExpression('OR', ['condition1', 'condition2']),
  815. 'HAVING (condition1) OR (condition2)'
  816. ],
  817. [
  818. [new CompositeExpression('AND', ['condition1', 'condition2'])],
  819. new CompositeExpression('AND', ['condition1', 'condition2']),
  820. 'HAVING (condition1) AND (condition2)'
  821. ],
  822. ];
  823. }
  824. /**
  825. * @dataProvider dataHaving
  826. *
  827. * @param array $havingArguments
  828. * @param array $expectedQueryPart
  829. * @param string $expectedQuery
  830. */
  831. public function testHaving($havingArguments, $expectedQueryPart, $expectedQuery) {
  832. call_user_func_array(
  833. [$this->queryBuilder, 'having'],
  834. $havingArguments
  835. );
  836. $this->assertEquals(
  837. $expectedQueryPart,
  838. $this->queryBuilder->getQueryPart('having')
  839. );
  840. $this->assertSame(
  841. 'SELECT ' . $expectedQuery,
  842. $this->queryBuilder->getSQL()
  843. );
  844. }
  845. public function dataAndHaving() {
  846. return [
  847. [['condition2'], new CompositeExpression('AND', ['condition1', 'condition2']), 'HAVING (condition1) AND (condition2)'],
  848. [['condition2', 'condition3'], new CompositeExpression('AND', ['condition1', 'condition2', 'condition3']), 'HAVING (condition1) AND (condition2) AND (condition3)'],
  849. [
  850. [new CompositeExpression('OR', ['condition2', 'condition3'])],
  851. new CompositeExpression('AND', ['condition1', new CompositeExpression('OR', ['condition2', 'condition3'])]),
  852. 'HAVING (condition1) AND ((condition2) OR (condition3))'
  853. ],
  854. [
  855. [new CompositeExpression('AND', ['condition2', 'condition3'])],
  856. new CompositeExpression('AND', ['condition1', new CompositeExpression('AND', ['condition2', 'condition3'])]),
  857. 'HAVING (condition1) AND ((condition2) AND (condition3))'
  858. ],
  859. ];
  860. }
  861. /**
  862. * @dataProvider dataAndHaving
  863. *
  864. * @param array $havingArguments
  865. * @param array $expectedQueryPart
  866. * @param string $expectedQuery
  867. */
  868. public function testAndHaving($havingArguments, $expectedQueryPart, $expectedQuery) {
  869. $this->queryBuilder->having('condition1');
  870. call_user_func_array(
  871. [$this->queryBuilder, 'andHaving'],
  872. $havingArguments
  873. );
  874. $this->assertEquals(
  875. $expectedQueryPart,
  876. $this->queryBuilder->getQueryPart('having')
  877. );
  878. $this->assertSame(
  879. 'SELECT ' . $expectedQuery,
  880. $this->queryBuilder->getSQL()
  881. );
  882. }
  883. public function dataOrHaving() {
  884. return [
  885. [['condition2'], new CompositeExpression('OR', ['condition1', 'condition2']), 'HAVING (condition1) OR (condition2)'],
  886. [['condition2', 'condition3'], new CompositeExpression('OR', ['condition1', 'condition2', 'condition3']), 'HAVING (condition1) OR (condition2) OR (condition3)'],
  887. [
  888. [new CompositeExpression('OR', ['condition2', 'condition3'])],
  889. new CompositeExpression('OR', ['condition1', new CompositeExpression('OR', ['condition2', 'condition3'])]),
  890. 'HAVING (condition1) OR ((condition2) OR (condition3))'
  891. ],
  892. [
  893. [new CompositeExpression('AND', ['condition2', 'condition3'])],
  894. new CompositeExpression('OR', ['condition1', new CompositeExpression('AND', ['condition2', 'condition3'])]),
  895. 'HAVING (condition1) OR ((condition2) AND (condition3))'
  896. ],
  897. ];
  898. }
  899. /**
  900. * @dataProvider dataOrHaving
  901. *
  902. * @param array $havingArguments
  903. * @param array $expectedQueryPart
  904. * @param string $expectedQuery
  905. */
  906. public function testOrHaving($havingArguments, $expectedQueryPart, $expectedQuery) {
  907. $this->queryBuilder->having('condition1');
  908. call_user_func_array(
  909. [$this->queryBuilder, 'orHaving'],
  910. $havingArguments
  911. );
  912. $this->assertEquals(
  913. $expectedQueryPart,
  914. $this->queryBuilder->getQueryPart('having')
  915. );
  916. $this->assertSame(
  917. 'SELECT ' . $expectedQuery,
  918. $this->queryBuilder->getSQL()
  919. );
  920. }
  921. public function dataOrderBy() {
  922. return [
  923. ['column', null, ['`column` ASC'], 'ORDER BY `column` ASC'],
  924. ['column', 'ASC', ['`column` ASC'], 'ORDER BY `column` ASC'],
  925. ['column', 'DESC', ['`column` DESC'], 'ORDER BY `column` DESC'],
  926. ];
  927. }
  928. /**
  929. * @dataProvider dataOrderBy
  930. *
  931. * @param string $sort
  932. * @param string $order
  933. * @param array $expectedQueryPart
  934. * @param string $expectedQuery
  935. */
  936. public function testOrderBy($sort, $order, $expectedQueryPart, $expectedQuery) {
  937. $this->queryBuilder->orderBy($sort, $order);
  938. $this->assertEquals(
  939. $expectedQueryPart,
  940. $this->queryBuilder->getQueryPart('orderBy')
  941. );
  942. $this->assertSame(
  943. 'SELECT ' . $expectedQuery,
  944. $this->queryBuilder->getSQL()
  945. );
  946. }
  947. public function dataAddOrderBy() {
  948. return [
  949. ['column2', null, null, ['`column1` ASC', '`column2` ASC'], 'ORDER BY `column1` ASC, `column2` ASC'],
  950. ['column2', null, 'ASC', ['`column1` ASC', '`column2` ASC'], 'ORDER BY `column1` ASC, `column2` ASC'],
  951. ['column2', null, 'DESC', ['`column1` DESC', '`column2` ASC'], 'ORDER BY `column1` DESC, `column2` ASC'],
  952. ['column2', 'ASC', null, ['`column1` ASC', '`column2` ASC'], 'ORDER BY `column1` ASC, `column2` ASC'],
  953. ['column2', 'ASC', 'ASC', ['`column1` ASC', '`column2` ASC'], 'ORDER BY `column1` ASC, `column2` ASC'],
  954. ['column2', 'ASC', 'DESC', ['`column1` DESC', '`column2` ASC'], 'ORDER BY `column1` DESC, `column2` ASC'],
  955. ['column2', 'DESC', null, ['`column1` ASC', '`column2` DESC'], 'ORDER BY `column1` ASC, `column2` DESC'],
  956. ['column2', 'DESC', 'ASC', ['`column1` ASC', '`column2` DESC'], 'ORDER BY `column1` ASC, `column2` DESC'],
  957. ['column2', 'DESC', 'DESC', ['`column1` DESC', '`column2` DESC'], 'ORDER BY `column1` DESC, `column2` DESC'],
  958. ];
  959. }
  960. /**
  961. * @dataProvider dataAddOrderBy
  962. *
  963. * @param string $sort2
  964. * @param string $order2
  965. * @param string $order1
  966. * @param array $expectedQueryPart
  967. * @param string $expectedQuery
  968. */
  969. public function testAddOrderBy($sort2, $order2, $order1, $expectedQueryPart, $expectedQuery) {
  970. $this->queryBuilder->orderBy('column1', $order1);
  971. $this->queryBuilder->addOrderBy($sort2, $order2);
  972. $this->assertEquals(
  973. $expectedQueryPart,
  974. $this->queryBuilder->getQueryPart('orderBy')
  975. );
  976. $this->assertSame(
  977. 'SELECT ' . $expectedQuery,
  978. $this->queryBuilder->getSQL()
  979. );
  980. }
  981. public function testGetLastInsertId() {
  982. $qB = $this->connection->getQueryBuilder();
  983. try {
  984. $qB->getLastInsertId();
  985. $this->fail('getLastInsertId() should throw an exception, when being called before insert()');
  986. } catch (\BadMethodCallException $e) {
  987. $this->addToAssertionCount(1);
  988. }
  989. $qB->insert('properties')
  990. ->values([
  991. 'userid' => $qB->expr()->literal('testFirstResult'),
  992. 'propertypath' => $qB->expr()->literal('testing'),
  993. 'propertyname' => $qB->expr()->literal('testing'),
  994. 'propertyvalue' => $qB->expr()->literal('testing'),
  995. ])
  996. ->execute();
  997. $actual = $qB->getLastInsertId();
  998. $this->assertNotNull($actual);
  999. $this->assertInternalType('int', $actual);
  1000. $this->assertEquals($this->connection->lastInsertId('*PREFIX*properties'), $actual);
  1001. $qB->delete('properties')
  1002. ->where($qB->expr()->eq('userid', $qB->expr()->literal('testFirstResult')))
  1003. ->execute();
  1004. try {
  1005. $qB->getLastInsertId();
  1006. $this->fail('getLastInsertId() should throw an exception, when being called after delete()');
  1007. } catch (\BadMethodCallException $e) {
  1008. $this->addToAssertionCount(1);
  1009. }
  1010. }
  1011. public function dataGetTableName() {
  1012. return [
  1013. ['*PREFIX*table', null, '`*PREFIX*table`'],
  1014. ['*PREFIX*table', true, '`*PREFIX*table`'],
  1015. ['*PREFIX*table', false, '`*PREFIX*table`'],
  1016. ['table', null, '`*PREFIX*table`'],
  1017. ['table', true, '`*PREFIX*table`'],
  1018. ['table', false, '`table`'],
  1019. ];
  1020. }
  1021. /**
  1022. * @dataProvider dataGetTableName
  1023. *
  1024. * @param string $tableName
  1025. * @param bool $automatic
  1026. * @param string $expected
  1027. */
  1028. public function testGetTableName($tableName, $automatic, $expected) {
  1029. if ($automatic !== null) {
  1030. $this->queryBuilder->automaticTablePrefix($automatic);
  1031. }
  1032. $this->assertSame(
  1033. $expected,
  1034. $this->queryBuilder->getTableName($tableName)
  1035. );
  1036. }
  1037. public function dataGetColumnName() {
  1038. return [
  1039. ['column', '', '`column`'],
  1040. ['column', 'a', '`a`.`column`'],
  1041. ];
  1042. }
  1043. /**
  1044. * @dataProvider dataGetColumnName
  1045. * @param string $column
  1046. * @param string $prefix
  1047. * @param string $expected
  1048. */
  1049. public function testGetColumnName($column, $prefix, $expected) {
  1050. $this->assertSame(
  1051. $expected,
  1052. $this->queryBuilder->getColumnName($column, $prefix)
  1053. );
  1054. }
  1055. public function testExecuteWithoutLogger() {
  1056. $queryBuilder = $this->createMock(\Doctrine\DBAL\Query\QueryBuilder::class);
  1057. $queryBuilder
  1058. ->expects($this->once())
  1059. ->method('execute')
  1060. ->willReturn(3);
  1061. $this->logger
  1062. ->expects($this->never())
  1063. ->method('debug');
  1064. $this->config
  1065. ->expects($this->once())
  1066. ->method('getValue')
  1067. ->with('log_query', false)
  1068. ->willReturn(false);
  1069. $this->invokePrivate($this->queryBuilder, 'queryBuilder', [$queryBuilder]);
  1070. $this->assertEquals(3, $this->queryBuilder->execute());
  1071. }
  1072. public function testExecuteWithLoggerAndNamedArray() {
  1073. $queryBuilder = $this->createMock(\Doctrine\DBAL\Query\QueryBuilder::class);
  1074. $queryBuilder
  1075. ->expects($this->at(0))
  1076. ->method('getParameters')
  1077. ->willReturn([
  1078. 'foo' => 'bar',
  1079. 'key' => 'value',
  1080. ]);
  1081. $queryBuilder
  1082. ->expects($this->at(1))
  1083. ->method('getSQL')
  1084. ->willReturn('SELECT * FROM FOO WHERE BAR = ?');
  1085. $queryBuilder
  1086. ->expects($this->once())
  1087. ->method('execute')
  1088. ->willReturn(3);
  1089. $this->logger
  1090. ->expects($this->once())
  1091. ->method('debug')
  1092. ->with(
  1093. 'DB QueryBuilder: \'{query}\' with parameters: {params}',
  1094. [
  1095. 'query' => 'SELECT * FROM FOO WHERE BAR = ?',
  1096. 'params' => 'foo => \'bar\', key => \'value\'',
  1097. 'app' => 'core',
  1098. ]
  1099. );
  1100. $this->config
  1101. ->expects($this->once())
  1102. ->method('getValue')
  1103. ->with('log_query', false)
  1104. ->willReturn(true);
  1105. $this->invokePrivate($this->queryBuilder, 'queryBuilder', [$queryBuilder]);
  1106. $this->assertEquals(3, $this->queryBuilder->execute());
  1107. }
  1108. public function testExecuteWithLoggerAndUnnamedArray() {
  1109. $queryBuilder = $this->createMock(\Doctrine\DBAL\Query\QueryBuilder::class);
  1110. $queryBuilder
  1111. ->expects($this->at(0))
  1112. ->method('getParameters')
  1113. ->willReturn(['Bar']);
  1114. $queryBuilder
  1115. ->expects($this->at(1))
  1116. ->method('getSQL')
  1117. ->willReturn('SELECT * FROM FOO WHERE BAR = ?');
  1118. $queryBuilder
  1119. ->expects($this->once())
  1120. ->method('execute')
  1121. ->willReturn(3);
  1122. $this->logger
  1123. ->expects($this->once())
  1124. ->method('debug')
  1125. ->with(
  1126. 'DB QueryBuilder: \'{query}\' with parameters: {params}',
  1127. [
  1128. 'query' => 'SELECT * FROM FOO WHERE BAR = ?',
  1129. 'params' => '0 => \'Bar\'',
  1130. 'app' => 'core',
  1131. ]
  1132. );
  1133. $this->config
  1134. ->expects($this->once())
  1135. ->method('getValue')
  1136. ->with('log_query', false)
  1137. ->willReturn(true);
  1138. $this->invokePrivate($this->queryBuilder, 'queryBuilder', [$queryBuilder]);
  1139. $this->assertEquals(3, $this->queryBuilder->execute());
  1140. }
  1141. public function testExecuteWithLoggerAndNoParams() {
  1142. $queryBuilder = $this->createMock(\Doctrine\DBAL\Query\QueryBuilder::class);
  1143. $queryBuilder
  1144. ->expects($this->at(0))
  1145. ->method('getParameters')
  1146. ->willReturn([]);
  1147. $queryBuilder
  1148. ->expects($this->at(1))
  1149. ->method('getSQL')
  1150. ->willReturn('SELECT * FROM FOO WHERE BAR = ?');
  1151. $queryBuilder
  1152. ->expects($this->once())
  1153. ->method('execute')
  1154. ->willReturn(3);
  1155. $this->logger
  1156. ->expects($this->once())
  1157. ->method('debug')
  1158. ->with(
  1159. 'DB QueryBuilder: \'{query}\'',
  1160. [
  1161. 'query' => 'SELECT * FROM FOO WHERE BAR = ?',
  1162. 'app' => 'core',
  1163. ]
  1164. );
  1165. $this->config
  1166. ->expects($this->once())
  1167. ->method('getValue')
  1168. ->with('log_query', false)
  1169. ->willReturn(true);
  1170. $this->invokePrivate($this->queryBuilder, 'queryBuilder', [$queryBuilder]);
  1171. $this->assertEquals(3, $this->queryBuilder->execute());
  1172. }
  1173. }