QueryBuilderTest.php 27KB

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