QueryBuilderTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Tests\ORM;
  20. use Doctrine\Common\Collections\ArrayCollection;
  21. use Doctrine\ORM\QueryBuilder,
  22. Doctrine\ORM\Query\Expr,
  23. Doctrine\ORM\Query\Parameter,
  24. Doctrine\ORM\Query\ParameterTypeInferer;
  25. require_once __DIR__ . '/../TestInit.php';
  26. /**
  27. * Test case for the QueryBuilder class used to build DQL query string in a
  28. * object oriented way.
  29. *
  30. * @author Jonathan H. Wage <jonwage@gmail.com>
  31. * @author Roman Borschel <roman@code-factory.org
  32. * @since 2.0
  33. */
  34. class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase
  35. {
  36. private $_em;
  37. protected function setUp()
  38. {
  39. $this->_em = $this->_getTestEntityManager();
  40. }
  41. protected function assertValidQueryBuilder(QueryBuilder $qb, $expectedDql)
  42. {
  43. $dql = $qb->getDql();
  44. $q = $qb->getQuery();
  45. $this->assertEquals($expectedDql, $dql);
  46. }
  47. public function testSelectSetsType()
  48. {
  49. $qb = $this->_em->createQueryBuilder()
  50. ->delete('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  51. ->select('u.id', 'u.username');
  52. $this->assertEquals($qb->getType(), QueryBuilder::SELECT);
  53. }
  54. public function testEmptySelectSetsType()
  55. {
  56. $qb = $this->_em->createQueryBuilder()
  57. ->delete('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  58. ->select();
  59. $this->assertEquals($qb->getType(), QueryBuilder::SELECT);
  60. }
  61. public function testDeleteSetsType()
  62. {
  63. $qb = $this->_em->createQueryBuilder()
  64. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  65. ->delete();
  66. $this->assertEquals($qb->getType(), QueryBuilder::DELETE);
  67. }
  68. public function testUpdateSetsType()
  69. {
  70. $qb = $this->_em->createQueryBuilder()
  71. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  72. ->update();
  73. $this->assertEquals($qb->getType(), QueryBuilder::UPDATE);
  74. }
  75. public function testSimpleSelect()
  76. {
  77. $qb = $this->_em->createQueryBuilder()
  78. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  79. ->select('u.id', 'u.username');
  80. $this->assertValidQueryBuilder($qb, 'SELECT u.id, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u');
  81. }
  82. public function testSimpleDelete()
  83. {
  84. $qb = $this->_em->createQueryBuilder()
  85. ->delete('Doctrine\Tests\Models\CMS\CmsUser', 'u');
  86. $this->assertValidQueryBuilder($qb, 'DELETE Doctrine\Tests\Models\CMS\CmsUser u');
  87. }
  88. public function testSimpleUpdate()
  89. {
  90. $qb = $this->_em->createQueryBuilder()
  91. ->update('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  92. ->set('u.username', ':username');
  93. $this->assertValidQueryBuilder($qb, 'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.username = :username');
  94. }
  95. public function testInnerJoin()
  96. {
  97. $qb = $this->_em->createQueryBuilder()
  98. ->select('u', 'a')
  99. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  100. ->innerJoin('u.articles', 'a');
  101. $this->assertValidQueryBuilder($qb, 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a');
  102. }
  103. public function testComplexInnerJoin()
  104. {
  105. $qb = $this->_em->createQueryBuilder()
  106. ->select('u', 'a')
  107. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  108. ->innerJoin('u.articles', 'a', 'ON', 'u.id = a.author_id');
  109. $this->assertValidQueryBuilder(
  110. $qb,
  111. 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a ON u.id = a.author_id'
  112. );
  113. }
  114. public function testComplexInnerJoinWithIndexBy()
  115. {
  116. $qb = $this->_em->createQueryBuilder()
  117. ->select('u', 'a')
  118. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  119. ->innerJoin('u.articles', 'a', 'ON', 'u.id = a.author_id', 'a.name');
  120. $this->assertValidQueryBuilder(
  121. $qb,
  122. 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a INDEX BY a.name ON u.id = a.author_id'
  123. );
  124. }
  125. public function testLeftJoin()
  126. {
  127. $qb = $this->_em->createQueryBuilder()
  128. ->select('u', 'a')
  129. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  130. ->leftJoin('u.articles', 'a');
  131. $this->assertValidQueryBuilder($qb, 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a');
  132. }
  133. public function testLeftJoinWithIndexBy()
  134. {
  135. $qb = $this->_em->createQueryBuilder()
  136. ->select('u', 'a')
  137. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  138. ->leftJoin('u.articles', 'a', null, null, 'a.name');
  139. $this->assertValidQueryBuilder($qb, 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a INDEX BY a.name');
  140. }
  141. public function testMultipleFrom()
  142. {
  143. $qb = $this->_em->createQueryBuilder()
  144. ->select('u', 'g')
  145. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  146. ->from('Doctrine\Tests\Models\CMS\CmsGroup', 'g');
  147. $this->assertValidQueryBuilder($qb, 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsGroup g');
  148. }
  149. public function testMultipleFromWithJoin()
  150. {
  151. $qb = $this->_em->createQueryBuilder()
  152. ->select('u', 'g')
  153. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  154. ->from('Doctrine\Tests\Models\CMS\CmsGroup', 'g')
  155. ->innerJoin('u.articles', 'a', 'ON', 'u.id = a.author_id');
  156. $this->assertValidQueryBuilder($qb, 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a ON u.id = a.author_id, Doctrine\Tests\Models\CMS\CmsGroup g');
  157. }
  158. public function testMultipleFromWithMultipleJoin()
  159. {
  160. $qb = $this->_em->createQueryBuilder()
  161. ->select('u', 'g')
  162. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  163. ->from('Doctrine\Tests\Models\CMS\CmsArticle', 'a')
  164. ->innerJoin('u.groups', 'g')
  165. ->leftJoin('u.address', 'ad')
  166. ->innerJoin('a.comments', 'c');
  167. $this->assertValidQueryBuilder($qb, 'SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.groups g LEFT JOIN u.address ad, Doctrine\Tests\Models\CMS\CmsArticle a INNER JOIN a.comments c');
  168. }
  169. public function testWhere()
  170. {
  171. $qb = $this->_em->createQueryBuilder()
  172. ->select('u')
  173. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  174. ->where('u.id = :uid');
  175. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid');
  176. }
  177. public function testComplexAndWhere()
  178. {
  179. $qb = $this->_em->createQueryBuilder()
  180. ->select('u')
  181. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  182. ->where('u.id = :uid OR u.id = :uid2 OR u.id = :uid3')
  183. ->andWhere('u.name = :name');
  184. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (u.id = :uid OR u.id = :uid2 OR u.id = :uid3) AND u.name = :name');
  185. }
  186. public function testAndWhere()
  187. {
  188. $qb = $this->_em->createQueryBuilder()
  189. ->select('u')
  190. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  191. ->where('u.id = :uid')
  192. ->andWhere('u.id = :uid2');
  193. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id = :uid2');
  194. }
  195. public function testOrWhere()
  196. {
  197. $qb = $this->_em->createQueryBuilder()
  198. ->select('u')
  199. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  200. ->where('u.id = :uid')
  201. ->orWhere('u.id = :uid2');
  202. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id = :uid2');
  203. }
  204. public function testComplexAndWhereOrWhereNesting()
  205. {
  206. $qb = $this->_em->createQueryBuilder();
  207. $qb->select('u')
  208. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  209. ->where('u.id = :uid')
  210. ->orWhere('u.id = :uid2')
  211. ->andWhere('u.id = :uid3')
  212. ->orWhere('u.name = :name1', 'u.name = :name2')
  213. ->andWhere('u.name <> :noname');
  214. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (((u.id = :uid OR u.id = :uid2) AND u.id = :uid3) OR u.name = :name1 OR u.name = :name2) AND u.name <> :noname');
  215. }
  216. public function testAndWhereIn()
  217. {
  218. $qb = $this->_em->createQueryBuilder();
  219. $qb->select('u')
  220. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  221. ->where('u.id = :uid')
  222. ->andWhere($qb->expr()->in('u.id', array(1, 2, 3)));
  223. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id IN(1, 2, 3)');
  224. }
  225. public function testOrWhereIn()
  226. {
  227. $qb = $this->_em->createQueryBuilder();
  228. $qb->select('u')
  229. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  230. ->where('u.id = :uid')
  231. ->orWhere($qb->expr()->in('u.id', array(1, 2, 3)));
  232. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id IN(1, 2, 3)');
  233. }
  234. public function testAndWhereNotIn()
  235. {
  236. $qb = $this->_em->createQueryBuilder();
  237. $qb->select('u')
  238. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  239. ->where('u.id = :uid')
  240. ->andWhere($qb->expr()->notIn('u.id', array(1, 2, 3)));
  241. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id NOT IN(1, 2, 3)');
  242. }
  243. public function testOrWhereNotIn()
  244. {
  245. $qb = $this->_em->createQueryBuilder();
  246. $qb->select('u')
  247. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  248. ->where('u.id = :uid')
  249. ->orWhere($qb->expr()->notIn('u.id', array(1, 2, 3)));
  250. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id NOT IN(1, 2, 3)');
  251. }
  252. public function testGroupBy()
  253. {
  254. $qb = $this->_em->createQueryBuilder()
  255. ->select('u')
  256. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  257. ->groupBy('u.id')
  258. ->addGroupBy('u.username');
  259. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id, u.username');
  260. }
  261. public function testHaving()
  262. {
  263. $qb = $this->_em->createQueryBuilder()
  264. ->select('u')
  265. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  266. ->groupBy('u.id')
  267. ->having('COUNT(u.id) > 1');
  268. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id HAVING COUNT(u.id) > 1');
  269. }
  270. public function testAndHaving()
  271. {
  272. $qb = $this->_em->createQueryBuilder()
  273. ->select('u')
  274. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  275. ->groupBy('u.id')
  276. ->having('COUNT(u.id) > 1')
  277. ->andHaving('COUNT(u.id) < 1');
  278. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id HAVING COUNT(u.id) > 1 AND COUNT(u.id) < 1');
  279. }
  280. public function testOrHaving()
  281. {
  282. $qb = $this->_em->createQueryBuilder()
  283. ->select('u')
  284. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  285. ->groupBy('u.id')
  286. ->having('COUNT(u.id) > 1')
  287. ->andHaving('COUNT(u.id) < 1')
  288. ->orHaving('COUNT(u.id) > 1');
  289. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id HAVING (COUNT(u.id) > 1 AND COUNT(u.id) < 1) OR COUNT(u.id) > 1');
  290. }
  291. public function testOrderBy()
  292. {
  293. $qb = $this->_em->createQueryBuilder()
  294. ->select('u')
  295. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  296. ->orderBy('u.username', 'ASC');
  297. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC');
  298. }
  299. public function testOrderByWithExpression()
  300. {
  301. $qb = $this->_em->createQueryBuilder();
  302. $qb->select('u')
  303. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  304. ->orderBy($qb->expr()->asc('u.username'));
  305. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC');
  306. }
  307. public function testAddOrderBy()
  308. {
  309. $qb = $this->_em->createQueryBuilder()
  310. ->select('u')
  311. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  312. ->orderBy('u.username', 'ASC')
  313. ->addOrderBy('u.username', 'DESC');
  314. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username ASC, u.username DESC');
  315. }
  316. public function testGetQuery()
  317. {
  318. $qb = $this->_em->createQueryBuilder()
  319. ->select('u')
  320. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u');
  321. $q = $qb->getQuery();
  322. $this->assertEquals('Doctrine\ORM\Query', get_class($q));
  323. }
  324. public function testSetParameter()
  325. {
  326. $qb = $this->_em->createQueryBuilder()
  327. ->select('u')
  328. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  329. ->where('u.id = :id')
  330. ->setParameter('id', 1);
  331. $parameter = new Parameter('id', 1, ParameterTypeInferer::inferType(1));
  332. $this->assertEquals($parameter, $qb->getParameter('id'));
  333. }
  334. public function testSetParameters()
  335. {
  336. $qb = $this->_em->createQueryBuilder();
  337. $qb->select('u')
  338. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  339. ->where($qb->expr()->orx('u.username = :username', 'u.username = :username2'));
  340. $parameters = new ArrayCollection();
  341. $parameters->add(new Parameter('username', 'jwage'));
  342. $parameters->add(new Parameter('username2', 'jonwage'));
  343. $qb->setParameters($parameters);
  344. $this->assertEquals($parameters, $qb->getQuery()->getParameters());
  345. }
  346. public function testGetParameters()
  347. {
  348. $qb = $this->_em->createQueryBuilder();
  349. $qb->select('u')
  350. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  351. ->where('u.id = :id');
  352. $parameters = new ArrayCollection();
  353. $parameters->add(new Parameter('id', 1));
  354. $qb->setParameters($parameters);
  355. $this->assertEquals($parameters, $qb->getParameters());
  356. }
  357. public function testGetParameter()
  358. {
  359. $qb = $this->_em->createQueryBuilder()
  360. ->select('u')
  361. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  362. ->where('u.id = :id');
  363. $parameters = new ArrayCollection();
  364. $parameters->add(new Parameter('id', 1));
  365. $qb->setParameters($parameters);
  366. $this->assertEquals($parameters->first(), $qb->getParameter('id'));
  367. }
  368. public function testMultipleWhere()
  369. {
  370. $qb = $this->_em->createQueryBuilder()
  371. ->select('u')
  372. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  373. ->where('u.id = :uid', 'u.id = :uid2');
  374. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id = :uid2');
  375. }
  376. public function testMultipleAndWhere()
  377. {
  378. $qb = $this->_em->createQueryBuilder()
  379. ->select('u')
  380. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  381. ->andWhere('u.id = :uid', 'u.id = :uid2');
  382. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid AND u.id = :uid2');
  383. }
  384. public function testMultipleOrWhere()
  385. {
  386. $qb = $this->_em->createQueryBuilder();
  387. $qb->select('u')
  388. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  389. ->orWhere('u.id = :uid', $qb->expr()->eq('u.id', ':uid2'));
  390. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid OR u.id = :uid2');
  391. }
  392. public function testComplexWhere()
  393. {
  394. $qb = $this->_em->createQueryBuilder();
  395. $orExpr = $qb->expr()->orX();
  396. $orExpr->add($qb->expr()->eq('u.id', ':uid3'));
  397. $orExpr->add($qb->expr()->in('u.id', array(1)));
  398. $qb->select('u')
  399. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  400. ->where($orExpr);
  401. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid3 OR u.id IN(1)');
  402. }
  403. public function testWhereInWithStringLiterals()
  404. {
  405. $qb = $this->_em->createQueryBuilder();
  406. $qb->select('u')
  407. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  408. ->where($qb->expr()->in('u.name', array('one', 'two', 'three')));
  409. $this->assertValidQueryBuilder($qb, "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IN('one', 'two', 'three')");
  410. $qb->where($qb->expr()->in('u.name', array("O'Reilly", "O'Neil", 'Smith')));
  411. $this->assertValidQueryBuilder($qb, "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IN('O''Reilly', 'O''Neil', 'Smith')");
  412. }
  413. public function testWhereInWithObjectLiterals()
  414. {
  415. $qb = $this->_em->createQueryBuilder();
  416. $expr = $this->_em->getExpressionBuilder();
  417. $qb->select('u')
  418. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  419. ->where($expr->in('u.name', array($expr->literal('one'), $expr->literal('two'), $expr->literal('three'))));
  420. $this->assertValidQueryBuilder($qb, "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IN('one', 'two', 'three')");
  421. $qb->where($expr->in('u.name', array($expr->literal("O'Reilly"), $expr->literal("O'Neil"), $expr->literal('Smith'))));
  422. $this->assertValidQueryBuilder($qb, "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IN('O''Reilly', 'O''Neil', 'Smith')");
  423. }
  424. public function testNegation()
  425. {
  426. $expr = $this->_em->getExpressionBuilder();
  427. $orExpr = $expr->orX();
  428. $orExpr->add($expr->eq('u.id', ':uid3'));
  429. $orExpr->add($expr->not($expr->in('u.id', array(1))));
  430. $qb = $this->_em->createQueryBuilder();
  431. $qb->select('u')
  432. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  433. ->where($orExpr);
  434. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid3 OR NOT(u.id IN(1))');
  435. }
  436. public function testSomeAllAny()
  437. {
  438. $qb = $this->_em->createQueryBuilder();
  439. $expr = $this->_em->getExpressionBuilder();
  440. //$subquery = $qb->subquery('Doctrine\Tests\Models\CMS\CmsArticle', 'a')->select('a.id');
  441. $qb->select('u')
  442. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  443. ->where($expr->gt('u.id', $expr->all('select a.id from Doctrine\Tests\Models\CMS\CmsArticle a')));
  444. $this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id > ALL(select a.id from Doctrine\Tests\Models\CMS\CmsArticle a)');
  445. }
  446. public function testMultipleIsolatedQueryConstruction()
  447. {
  448. $qb = $this->_em->createQueryBuilder();
  449. $expr = $this->_em->getExpressionBuilder();
  450. $qb->select('u')->from('Doctrine\Tests\Models\CMS\CmsUser', 'u');
  451. $qb->where($expr->eq('u.name', ':name'));
  452. $qb->setParameter('name', 'romanb');
  453. $q1 = $qb->getQuery();
  454. $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = :name', $q1->getDql());
  455. $this->assertEquals(1, count($q1->getParameters()));
  456. // add another condition and construct a second query
  457. $qb->andWhere($expr->eq('u.id', ':id'));
  458. $qb->setParameter('id', 42);
  459. $q2 = $qb->getQuery();
  460. $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = :name AND u.id = :id', $q2->getDql());
  461. $this->assertTrue($q1 !== $q2); // two different, independent queries
  462. $this->assertEquals(2, count($q2->getParameters()));
  463. $this->assertEquals(1, count($q1->getParameters())); // $q1 unaffected
  464. }
  465. public function testGetEntityManager()
  466. {
  467. $qb = $this->_em->createQueryBuilder();
  468. $this->assertEquals($this->_em, $qb->getEntityManager());
  469. }
  470. public function testInitialStateIsClean()
  471. {
  472. $qb = $this->_em->createQueryBuilder();
  473. $this->assertEquals(QueryBuilder::STATE_CLEAN, $qb->getState());
  474. }
  475. public function testAlteringQueryChangesStateToDirty()
  476. {
  477. $qb = $this->_em->createQueryBuilder()
  478. ->select('u')
  479. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u');
  480. $this->assertEquals(QueryBuilder::STATE_DIRTY, $qb->getState());
  481. }
  482. public function testSelectWithFuncExpression()
  483. {
  484. $qb = $this->_em->createQueryBuilder();
  485. $expr = $qb->expr();
  486. $qb->select($expr->count('e.id'));
  487. $this->assertValidQueryBuilder($qb, 'SELECT COUNT(e.id)');
  488. }
  489. public function testResetDQLPart()
  490. {
  491. $qb = $this->_em->createQueryBuilder()
  492. ->select('u')
  493. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  494. ->where('u.username = ?1')->orderBy('u.username');
  495. $this->assertEquals('u.username = ?1', (string)$qb->getDQLPart('where'));
  496. $this->assertEquals(1, count($qb->getDQLPart('orderBy')));
  497. $qb->resetDqlPart('where')->resetDqlPart('orderBy');
  498. $this->assertNull($qb->getDQLPart('where'));
  499. $this->assertEquals(0, count($qb->getDQLPart('orderBy')));
  500. }
  501. public function testResetDQLParts()
  502. {
  503. $qb = $this->_em->createQueryBuilder()
  504. ->select('u')
  505. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  506. ->where('u.username = ?1')->orderBy('u.username');
  507. $qb->resetDQLParts(array('where', 'orderBy'));
  508. $this->assertEquals(1, count($qb->getDQLPart('select')));
  509. $this->assertNull($qb->getDQLPart('where'));
  510. $this->assertEquals(0, count($qb->getDQLPart('orderBy')));
  511. }
  512. public function testResetAllDQLParts()
  513. {
  514. $qb = $this->_em->createQueryBuilder()
  515. ->select('u')
  516. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  517. ->where('u.username = ?1')->orderBy('u.username');
  518. $qb->resetDQLParts();
  519. $this->assertEquals(0, count($qb->getDQLPart('select')));
  520. $this->assertNull($qb->getDQLPart('where'));
  521. $this->assertEquals(0, count($qb->getDQLPart('orderBy')));
  522. }
  523. /**
  524. * @group DDC-867
  525. */
  526. public function testDeepClone()
  527. {
  528. $qb = $this->_em->createQueryBuilder()
  529. ->select('u')
  530. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  531. ->andWhere('u.username = ?1')
  532. ->andWhere('u.status = ?2');
  533. $expr = $qb->getDQLPart('where');
  534. $this->assertEquals(2, $expr->count(), "Modifying the second query should affect the first one.");
  535. $qb2 = clone $qb;
  536. $qb2->andWhere('u.name = ?3');
  537. $this->assertEquals(2, $expr->count(), "Modifying the second query should affect the first one.");
  538. }
  539. /**
  540. * @group DDC-1933
  541. */
  542. public function testParametersAreCloned()
  543. {
  544. $originalQb = new QueryBuilder($this->_em);
  545. $originalQb->setParameter('parameter1', 'value1');
  546. $copy = clone $originalQb;
  547. $copy->setParameter('parameter2', 'value2');
  548. $this->assertCount(1, $originalQb->getParameters());
  549. $this->assertSame('value1', $copy->getParameter('parameter1')->getValue());
  550. $this->assertSame('value2', $copy->getParameter('parameter2')->getValue());
  551. }
  552. public function testGetRootAlias()
  553. {
  554. $qb = $this->_em->createQueryBuilder()
  555. ->select('u')
  556. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u');
  557. $this->assertEquals('u', $qb->getRootAlias());
  558. }
  559. public function testGetRootAliases()
  560. {
  561. $qb = $this->_em->createQueryBuilder()
  562. ->select('u')
  563. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u');
  564. $this->assertEquals(array('u'), $qb->getRootAliases());
  565. }
  566. public function testGetRootEntities()
  567. {
  568. $qb = $this->_em->createQueryBuilder()
  569. ->select('u')
  570. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u');
  571. $this->assertEquals(array('Doctrine\Tests\Models\CMS\CmsUser'), $qb->getRootEntities());
  572. }
  573. public function testGetSeveralRootAliases()
  574. {
  575. $qb = $this->_em->createQueryBuilder()
  576. ->select('u')
  577. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  578. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u2');
  579. $this->assertEquals(array('u', 'u2'), $qb->getRootAliases());
  580. $this->assertEquals('u', $qb->getRootAlias());
  581. }
  582. public function testBCAddJoinWithoutRootAlias()
  583. {
  584. $qb = $this->_em->createQueryBuilder()
  585. ->select('u')
  586. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  587. ->add('join', array('INNER JOIN u.groups g'), true);
  588. $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.groups g', $qb->getDQL());
  589. }
  590. /**
  591. * @group DDC-1211
  592. */
  593. public function testEmptyStringLiteral()
  594. {
  595. $expr = $this->_em->getExpressionBuilder();
  596. $qb = $this->_em->createQueryBuilder()
  597. ->select('u')
  598. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  599. ->where($expr->eq('u.username', $expr->literal("")));
  600. $this->assertEquals("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = ''", $qb->getDQL());
  601. }
  602. /**
  603. * @group DDC-1211
  604. */
  605. public function testEmptyNumericLiteral()
  606. {
  607. $expr = $this->_em->getExpressionBuilder();
  608. $qb = $this->_em->createQueryBuilder()
  609. ->select('u')
  610. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
  611. ->where($expr->eq('u.username', $expr->literal(0)));
  612. $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 0', $qb->getDQL());
  613. }
  614. /**
  615. * @group DDC-1227
  616. */
  617. public function testAddFromString()
  618. {
  619. $qb = $this->_em->createQueryBuilder()
  620. ->add('select', 'u')
  621. ->add('from', 'Doctrine\Tests\Models\CMS\CmsUser u');
  622. $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $qb->getDQL());
  623. }
  624. /**
  625. * @group DDC-1619
  626. */
  627. public function testAddDistinct()
  628. {
  629. $qb = $this->_em->createQueryBuilder()
  630. ->select('u')
  631. ->distinct()
  632. ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u');
  633. $this->assertEquals('SELECT DISTINCT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $qb->getDQL());
  634. }
  635. }