QueryTest.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Doctrine\Tests\ORM\Query;
  3. require_once __DIR__ . '/../../TestInit.php';
  4. class QueryTest extends \Doctrine\Tests\OrmTestCase
  5. {
  6. protected $_em = null;
  7. protected function setUp()
  8. {
  9. $this->_em = $this->_getTestEntityManager();
  10. }
  11. public function testGetParameters()
  12. {
  13. $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1");
  14. $this->assertEquals(array(), $query->getParameters());
  15. }
  16. public function testGetParameters_HasSomeAlready()
  17. {
  18. $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1");
  19. $query->setParameter(2, 84);
  20. $this->assertEquals(array(2 => 84), $query->getParameters());
  21. }
  22. public function testSetParameters()
  23. {
  24. $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1");
  25. $query->setParameters(array(1 => 'foo', 2 => 'bar'));
  26. $this->assertEquals(array(1 => 'foo', 2 => 'bar'), $query->getParameters());
  27. }
  28. public function testFree()
  29. {
  30. $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1");
  31. $query->setParameter(2, 84, \PDO::PARAM_INT);
  32. $query->free();
  33. $this->assertEquals(array(), $query->getParameters());
  34. }
  35. public function testClone()
  36. {
  37. $dql = "select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1";
  38. $query = $this->_em->createQuery($dql);
  39. $query->setParameter(2, 84, \PDO::PARAM_INT);
  40. $query->setHint('foo', 'bar');
  41. $cloned = clone $query;
  42. $this->assertEquals($dql, $cloned->getDql());
  43. $this->assertEquals(array(), $cloned->getParameters());
  44. $this->assertFalse($cloned->getHint('foo'));
  45. }
  46. public function testFluentQueryInterface()
  47. {
  48. $q = $this->_em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a");
  49. $q2 = $q->expireQueryCache(true)
  50. ->setQueryCacheLifetime(3600)
  51. ->setQueryCacheDriver(null)
  52. ->expireResultCache(true)
  53. ->setHint('foo', 'bar')
  54. ->setHint('bar', 'baz')
  55. ->setParameter(1, 'bar')
  56. ->setParameters(array(2 => 'baz'))
  57. ->setResultCacheDriver(null)
  58. ->setResultCacheId('foo')
  59. ->setDql('foo')
  60. ->setFirstResult(10)
  61. ->setMaxResults(10);
  62. $this->assertSame($q2, $q);
  63. }
  64. /**
  65. * @group DDC-968
  66. */
  67. public function testHints()
  68. {
  69. $q = $this->_em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a");
  70. $q->setHint('foo', 'bar')->setHint('bar', 'baz');
  71. $this->assertEquals('bar', $q->getHint('foo'));
  72. $this->assertEquals('baz', $q->getHint('bar'));
  73. $this->assertEquals(array('foo' => 'bar', 'bar' => 'baz'), $q->getHints());
  74. }
  75. /**
  76. * @expectedException Doctrine\ORM\Query\QueryException
  77. **/
  78. public function testIterateWithNoDistinctAndWrongSelectClause()
  79. {
  80. $q = $this->_em->createQuery("select u, a from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a");
  81. $q->iterate();
  82. }
  83. /**
  84. * @expectedException Doctrine\ORM\Query\QueryException
  85. **/
  86. public function testIterateWithNoDistinctAndWithValidSelectClause()
  87. {
  88. $q = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a");
  89. $q->iterate();
  90. }
  91. public function testIterateWithDistinct()
  92. {
  93. $q = $this->_em->createQuery("SELECT DISTINCT u from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a");
  94. $q->iterate();
  95. }
  96. }