3 namespace Doctrine\Tests\ORM\Functional;
5 use Doctrine\Tests\Models\Company\CompanyEmployee,
6 Doctrine\Tests\Models\Company\CompanyManager,
7 Doctrine\Tests\Models\Company\CompanyPerson,
8 Doctrine\Tests\Models\Company\CompanyCar;
10 require_once __DIR__ . '/../../TestInit.php';
13 * Functional Query tests.
15 * @author Benjamin <kontakt@beberlei.de>
17 class AdvancedDqlQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
19 protected function setUp()
21 $this->useModelSet('company');
24 $this->generateFixture();
27 public function testAggregateWithHavingClause()
29 $dql = 'SELECT p.department, AVG(p.salary) AS avgSalary '.
30 'FROM Doctrine\Tests\Models\Company\CompanyEmployee p '.
31 'GROUP BY p.department HAVING SUM(p.salary) > 200000 ORDER BY p.department';
33 $result = $this->_em->createQuery($dql)->getScalarResult();
35 $this->assertEquals(2, count($result));
36 $this->assertEquals('IT', $result[0]['department']);
37 $this->assertEquals(150000, $result[0]['avgSalary']);
38 $this->assertEquals('IT2', $result[1]['department']);
39 $this->assertEquals(600000, $result[1]['avgSalary']);
42 public function testUnnamedScalarResultsAreOneBased()
44 $dql = 'SELECT p.department, AVG(p.salary) '.
45 'FROM Doctrine\Tests\Models\Company\CompanyEmployee p '.
46 'GROUP BY p.department HAVING SUM(p.salary) > 200000 ORDER BY p.department';
48 $result = $this->_em->createQuery($dql)->getScalarResult();
50 $this->assertEquals(2, count($result));
51 $this->assertEquals(150000, $result[0][1]);
52 $this->assertEquals(600000, $result[1][1]);
55 public function testOrderByResultVariableCollectionSize()
57 $dql = 'SELECT p.name, size(p.friends) AS friends ' .
58 'FROM Doctrine\Tests\Models\Company\CompanyPerson p ' .
59 'WHERE p.friends IS NOT EMPTY ' .
60 'ORDER BY friends DESC, p.name DESC';
62 $result = $this->_em->createQuery($dql)->getScalarResult();
64 $this->assertEquals(4, count($result));
66 $this->assertEquals("Jonathan W.", $result[0]['name']);
67 $this->assertEquals(3, $result[0]['friends']);
69 $this->assertEquals('Guilherme B.', $result[1]['name']);
70 $this->assertEquals(2, $result[1]['friends']);
72 $this->assertEquals('Benjamin E.', $result[2]['name']);
73 $this->assertEquals(2, $result[2]['friends']);
75 $this->assertEquals('Roman B.', $result[3]['name']);
76 $this->assertEquals(1, $result[3]['friends']);
79 public function testIsNullAssocation()
81 $dql = 'SELECT p FROM Doctrine\Tests\Models\Company\CompanyPerson p '.
82 'WHERE p.spouse IS NULL';
83 $result = $this->_em->createQuery($dql)->getResult();
85 $this->assertEquals(2, count($result));
86 $this->assertTrue($result[0]->getId() > 0);
87 $this->assertNull($result[0]->getSpouse());
89 $this->assertTrue($result[1]->getId() > 0);
90 $this->assertNull($result[1]->getSpouse());
93 public function testSelectSubselect()
95 $dql = 'SELECT p, (SELECT c.brand FROM Doctrine\Tests\Models\Company\CompanyCar c WHERE p.car = c) brandName '.
96 'FROM Doctrine\Tests\Models\Company\CompanyManager p';
97 $result = $this->_em->createQuery($dql)->getArrayResult();
99 $this->assertEquals(1, count($result));
100 $this->assertEquals("Caramba", $result[0]['brandName']);
103 public function testInSubselect()
105 $dql = "SELECT p.name FROM Doctrine\Tests\Models\Company\CompanyPerson p ".
106 "WHERE p.name IN (SELECT n.name FROM Doctrine\Tests\Models\Company\CompanyPerson n WHERE n.name = 'Roman B.')";
107 $result = $this->_em->createQuery($dql)->getScalarResult();
109 $this->assertEquals(1, count($result));
110 $this->assertEquals('Roman B.', $result[0]['name']);
113 public function testGroupByMultipleFields()
115 $dql = 'SELECT p.department, p.name, count(p.id) FROM Doctrine\Tests\Models\Company\CompanyEmployee p '.
116 'GROUP BY p.department, p.name';
117 $result = $this->_em->createQuery($dql)->getResult();
119 $this->assertEquals(4, count($result));
122 public function testUpdateAs()
124 $dql = 'UPDATE Doctrine\Tests\Models\Company\CompanyEmployee AS p SET p.salary = 1';
125 $this->_em->createQuery($dql)->execute();
127 $this->assertTrue(count($this->_em->createQuery(
128 'SELECT count(p.id) FROM Doctrine\Tests\Models\Company\CompanyEmployee p WHERE p.salary = 1')->getResult()) > 0);
131 public function testDeleteAs()
133 $dql = 'DELETE Doctrine\Tests\Models\Company\CompanyEmployee AS p';
134 $this->_em->createQuery($dql)->getResult();
136 $dql = 'SELECT count(p) FROM Doctrine\Tests\Models\Company\CompanyEmployee p';
137 $result = $this->_em->createQuery($dql)->getSingleScalarResult();
139 $this->assertEquals(0, $result);
142 public function generateFixture()
144 $car = new CompanyCar('Caramba');
146 $manager1 = new CompanyManager();
147 $manager1->setName('Roman B.');
148 $manager1->setTitle('Foo');
149 $manager1->setDepartment('IT');
150 $manager1->setSalary(100000);
151 $manager1->setCar($car);
153 $person2 = new CompanyEmployee();
154 $person2->setName('Benjamin E.');
155 $person2->setDepartment('IT');
156 $person2->setSalary(200000);
158 $person3 = new CompanyEmployee();
159 $person3->setName('Guilherme B.');
160 $person3->setDepartment('IT2');
161 $person3->setSalary(400000);
163 $person4 = new CompanyEmployee();
164 $person4->setName('Jonathan W.');
165 $person4->setDepartment('IT2');
166 $person4->setSalary(800000);
168 $person2->setSpouse($person3);
170 $manager1->addFriend($person4);
171 $person2->addFriend($person3);
172 $person2->addFriend($person4);
173 $person3->addFriend($person4);
175 $this->_em->persist($car);
176 $this->_em->persist($manager1);
177 $this->_em->persist($person2);
178 $this->_em->persist($person3);
179 $this->_em->persist($person4);