AdvancedDqlQueryTest.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\Models\Company\CompanyEmployee,
  4. Doctrine\Tests\Models\Company\CompanyManager,
  5. Doctrine\Tests\Models\Company\CompanyPerson,
  6. Doctrine\Tests\Models\Company\CompanyCar;
  7. require_once __DIR__ . '/../../TestInit.php';
  8. /**
  9. * Functional Query tests.
  10. *
  11. * @author Benjamin <kontakt@beberlei.de>
  12. */
  13. class AdvancedDqlQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
  14. {
  15. protected function setUp()
  16. {
  17. $this->useModelSet('company');
  18. parent::setUp();
  19. $this->generateFixture();
  20. }
  21. public function testAggregateWithHavingClause()
  22. {
  23. $dql = 'SELECT p.department, AVG(p.salary) AS avgSalary '.
  24. 'FROM Doctrine\Tests\Models\Company\CompanyEmployee p '.
  25. 'GROUP BY p.department HAVING SUM(p.salary) > 200000 ORDER BY p.department';
  26. $result = $this->_em->createQuery($dql)->getScalarResult();
  27. $this->assertEquals(2, count($result));
  28. $this->assertEquals('IT', $result[0]['department']);
  29. $this->assertEquals(150000, $result[0]['avgSalary']);
  30. $this->assertEquals('IT2', $result[1]['department']);
  31. $this->assertEquals(600000, $result[1]['avgSalary']);
  32. }
  33. public function testUnnamedScalarResultsAreOneBased()
  34. {
  35. $dql = 'SELECT p.department, AVG(p.salary) '.
  36. 'FROM Doctrine\Tests\Models\Company\CompanyEmployee p '.
  37. 'GROUP BY p.department HAVING SUM(p.salary) > 200000 ORDER BY p.department';
  38. $result = $this->_em->createQuery($dql)->getScalarResult();
  39. $this->assertEquals(2, count($result));
  40. $this->assertEquals(150000, $result[0][1]);
  41. $this->assertEquals(600000, $result[1][1]);
  42. }
  43. public function testOrderByResultVariableCollectionSize()
  44. {
  45. $dql = 'SELECT p.name, size(p.friends) AS friends ' .
  46. 'FROM Doctrine\Tests\Models\Company\CompanyPerson p ' .
  47. 'WHERE p.friends IS NOT EMPTY ' .
  48. 'ORDER BY friends DESC, p.name DESC';
  49. $result = $this->_em->createQuery($dql)->getScalarResult();
  50. $this->assertEquals(4, count($result));
  51. $this->assertEquals("Jonathan W.", $result[0]['name']);
  52. $this->assertEquals(3, $result[0]['friends']);
  53. $this->assertEquals('Guilherme B.', $result[1]['name']);
  54. $this->assertEquals(2, $result[1]['friends']);
  55. $this->assertEquals('Benjamin E.', $result[2]['name']);
  56. $this->assertEquals(2, $result[2]['friends']);
  57. $this->assertEquals('Roman B.', $result[3]['name']);
  58. $this->assertEquals(1, $result[3]['friends']);
  59. }
  60. public function testIsNullAssocation()
  61. {
  62. $dql = 'SELECT p FROM Doctrine\Tests\Models\Company\CompanyPerson p '.
  63. 'WHERE p.spouse IS NULL';
  64. $result = $this->_em->createQuery($dql)->getResult();
  65. $this->assertEquals(2, count($result));
  66. $this->assertTrue($result[0]->getId() > 0);
  67. $this->assertNull($result[0]->getSpouse());
  68. $this->assertTrue($result[1]->getId() > 0);
  69. $this->assertNull($result[1]->getSpouse());
  70. }
  71. public function testSelectSubselect()
  72. {
  73. $dql = 'SELECT p, (SELECT c.brand FROM Doctrine\Tests\Models\Company\CompanyCar c WHERE p.car = c) brandName '.
  74. 'FROM Doctrine\Tests\Models\Company\CompanyManager p';
  75. $result = $this->_em->createQuery($dql)->getArrayResult();
  76. $this->assertEquals(1, count($result));
  77. $this->assertEquals("Caramba", $result[0]['brandName']);
  78. }
  79. public function testInSubselect()
  80. {
  81. $dql = "SELECT p.name FROM Doctrine\Tests\Models\Company\CompanyPerson p ".
  82. "WHERE p.name IN (SELECT n.name FROM Doctrine\Tests\Models\Company\CompanyPerson n WHERE n.name = 'Roman B.')";
  83. $result = $this->_em->createQuery($dql)->getScalarResult();
  84. $this->assertEquals(1, count($result));
  85. $this->assertEquals('Roman B.', $result[0]['name']);
  86. }
  87. public function testGroupByMultipleFields()
  88. {
  89. $dql = 'SELECT p.department, p.name, count(p.id) FROM Doctrine\Tests\Models\Company\CompanyEmployee p '.
  90. 'GROUP BY p.department, p.name';
  91. $result = $this->_em->createQuery($dql)->getResult();
  92. $this->assertEquals(4, count($result));
  93. }
  94. public function testUpdateAs()
  95. {
  96. $dql = 'UPDATE Doctrine\Tests\Models\Company\CompanyEmployee AS p SET p.salary = 1';
  97. $this->_em->createQuery($dql)->execute();
  98. $this->assertTrue(count($this->_em->createQuery(
  99. 'SELECT count(p.id) FROM Doctrine\Tests\Models\Company\CompanyEmployee p WHERE p.salary = 1')->getResult()) > 0);
  100. }
  101. /*public function testDeleteAs()
  102. {
  103. $dql = 'DELETE Doctrine\Tests\Models\Company\CompanyEmployee AS p';
  104. $this->_em->createQuery($dql)->getResult();
  105. $this->assertEquals(0, count($this->_em->createQuery(
  106. 'SELECT count(p) FROM Doctrine\Tests\Models\Company\CompanyEmployee p')->getResult()));
  107. }*/
  108. public function generateFixture()
  109. {
  110. $car = new CompanyCar('Caramba');
  111. $manager1 = new CompanyManager();
  112. $manager1->setName('Roman B.');
  113. $manager1->setTitle('Foo');
  114. $manager1->setDepartment('IT');
  115. $manager1->setSalary(100000);
  116. $manager1->setCar($car);
  117. $person2 = new CompanyEmployee();
  118. $person2->setName('Benjamin E.');
  119. $person2->setDepartment('IT');
  120. $person2->setSalary(200000);
  121. $person3 = new CompanyEmployee();
  122. $person3->setName('Guilherme B.');
  123. $person3->setDepartment('IT2');
  124. $person3->setSalary(400000);
  125. $person4 = new CompanyEmployee();
  126. $person4->setName('Jonathan W.');
  127. $person4->setDepartment('IT2');
  128. $person4->setSalary(800000);
  129. $person2->setSpouse($person3);
  130. $manager1->addFriend($person4);
  131. $person2->addFriend($person3);
  132. $person2->addFriend($person4);
  133. $person3->addFriend($person4);
  134. $this->_em->persist($car);
  135. $this->_em->persist($manager1);
  136. $this->_em->persist($person2);
  137. $this->_em->persist($person3);
  138. $this->_em->persist($person4);
  139. $this->_em->flush();
  140. $this->_em->clear();
  141. }
  142. }