Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / DDC1430Test.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5 require_once __DIR__ . '/../../../TestInit.php';
6
7 /**
8  * @group DDC-1430
9  */
10 class DDC1430Test extends \Doctrine\Tests\OrmFunctionalTestCase
11 {
12
13     protected function setUp()
14     {
15         parent::setUp();
16
17         try {
18             $this->_schemaTool->createSchema(array(
19                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1430Order'),
20                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1430OrderProduct'),
21             ));
22             $this->loadFixtures();
23         } catch (\Exception $exc) {
24
25         }
26     }
27
28     public function testOrderByFields()
29     {
30         $repository = $this->_em->getRepository(__NAMESPACE__ . '\DDC1430Order');
31         $builder    = $repository->createQueryBuilder('o');
32         $query      = $builder->select('o.id, o.date, COUNT(p.id) AS p_count')
33                         ->leftJoin('o.products', 'p')
34                         ->groupBy('o.id, o.date')
35                         ->orderBy('o.id')
36                         ->getQuery();
37
38         $this->assertEquals('SELECT o.id, o.date, COUNT(p.id) AS p_count FROM Doctrine\Tests\ORM\Functional\Ticket\DDC1430Order o LEFT JOIN o.products p GROUP BY o.id, o.date ORDER BY o.id ASC', $query->getDQL());
39         $this->assertEquals('SELECT d0_.order_id AS order_id0, d0_.created_at AS created_at1, COUNT(d1_.id) AS sclr2 FROM DDC1430Order d0_ LEFT JOIN DDC1430OrderProduct d1_ ON d0_.order_id = d1_.order_id GROUP BY d0_.order_id, d0_.created_at ORDER BY d0_.order_id ASC', $query->getSQL());
40
41
42         $result = $query->getResult();
43
44         $this->assertEquals(2, sizeof($result));
45
46         $this->assertArrayHasKey('id', $result[0]);
47         $this->assertArrayHasKey('id', $result[1]);
48
49         $this->assertArrayHasKey('p_count', $result[0]);
50         $this->assertArrayHasKey('p_count', $result[1]);
51
52         $this->assertEquals(1, $result[0]['id']);
53         $this->assertEquals(2, $result[1]['id']);
54
55         $this->assertEquals(2, $result[0]['p_count']);
56         $this->assertEquals(3, $result[1]['p_count']);
57     }
58
59     public function testOrderByAllObjectFields()
60     {
61         $repository = $this->_em->getRepository(__NAMESPACE__ . '\DDC1430Order');
62         $builder    = $repository->createQueryBuilder('o');
63         $query      = $builder->select('o, COUNT(p.id) AS p_count')
64                         ->leftJoin('o.products', 'p')
65                         ->groupBy('o.id, o.date, o.status')
66                         ->orderBy('o.id')
67                         ->getQuery();
68
69
70         $this->assertEquals('SELECT o, COUNT(p.id) AS p_count FROM Doctrine\Tests\ORM\Functional\Ticket\DDC1430Order o LEFT JOIN o.products p GROUP BY o.id, o.date, o.status ORDER BY o.id ASC', $query->getDQL());
71         $this->assertEquals('SELECT d0_.order_id AS order_id0, d0_.created_at AS created_at1, d0_.order_status AS order_status2, COUNT(d1_.id) AS sclr3 FROM DDC1430Order d0_ LEFT JOIN DDC1430OrderProduct d1_ ON d0_.order_id = d1_.order_id GROUP BY d0_.order_id, d0_.created_at, d0_.order_status ORDER BY d0_.order_id ASC', $query->getSQL());
72
73         $result = $query->getResult();
74
75
76         $this->assertEquals(2, sizeof($result));
77
78         $this->assertTrue($result[0][0] instanceof DDC1430Order);
79         $this->assertTrue($result[1][0] instanceof DDC1430Order);
80
81         $this->assertEquals($result[0][0]->getId(), 1);
82         $this->assertEquals($result[1][0]->getId(), 2);
83
84         $this->assertEquals($result[0]['p_count'], 2);
85         $this->assertEquals($result[1]['p_count'], 3);
86     }
87
88     public function testTicket()
89     {
90         $repository = $this->_em->getRepository(__NAMESPACE__ . '\DDC1430Order');
91         $builder    = $repository->createQueryBuilder('o');
92         $query      = $builder->select('o, COUNT(p.id) AS p_count')
93                         ->leftJoin('o.products', 'p')
94                         ->groupBy('o')
95                         ->orderBy('o.id')
96                         ->getQuery();
97
98
99         $this->assertEquals('SELECT o, COUNT(p.id) AS p_count FROM Doctrine\Tests\ORM\Functional\Ticket\DDC1430Order o LEFT JOIN o.products p GROUP BY o ORDER BY o.id ASC', $query->getDQL());
100         $this->assertEquals('SELECT d0_.order_id AS order_id0, d0_.created_at AS created_at1, d0_.order_status AS order_status2, COUNT(d1_.id) AS sclr3 FROM DDC1430Order d0_ LEFT JOIN DDC1430OrderProduct d1_ ON d0_.order_id = d1_.order_id GROUP BY d0_.order_id, d0_.created_at, d0_.order_status ORDER BY d0_.order_id ASC', $query->getSQL());
101
102
103         $result = $query->getResult();
104
105         $this->assertEquals(2, sizeof($result));
106
107         $this->assertTrue($result[0][0] instanceof DDC1430Order);
108         $this->assertTrue($result[1][0] instanceof DDC1430Order);
109
110         $this->assertEquals($result[0][0]->getId(), 1);
111         $this->assertEquals($result[1][0]->getId(), 2);
112
113         $this->assertEquals($result[0]['p_count'], 2);
114         $this->assertEquals($result[1]['p_count'], 3);
115     }
116
117     public function loadFixtures()
118     {
119         $o1 = new DDC1430Order('NEW');
120         $o2 = new DDC1430Order('OK');
121
122         $o1->addProduct(new DDC1430OrderProduct(1.1));
123         $o1->addProduct(new DDC1430OrderProduct(1.2));
124
125         $o2->addProduct(new DDC1430OrderProduct(2.1));
126         $o2->addProduct(new DDC1430OrderProduct(2.2));
127         $o2->addProduct(new DDC1430OrderProduct(2.3));
128
129         $this->_em->persist($o1);
130         $this->_em->persist($o2);
131
132         $this->_em->flush();
133     }
134
135 }
136
137 /**
138  * @Entity
139  */
140 class DDC1430Order
141 {
142
143     /**
144      * @Id
145      * @Column(name="order_id", type="integer")
146      * @GeneratedValue()
147      */
148     protected $id;
149
150     /**
151      * @Column(name="created_at", type="datetime")
152      */
153     private $date;
154
155     /**
156      * @Column(name="order_status", type="string")
157      */
158     private $status;
159
160     /**
161      * @OneToMany(targetEntity="DDC1430OrderProduct", mappedBy="order", cascade={"persist", "remove"})
162      *
163      * @var \Doctrine\Common\Collections\ArrayCollection $products
164      */
165     private $products;
166
167     /**
168      * @return integer
169      */
170     public function getId()
171     {
172         return $this->id;
173     }
174
175     public function __construct($status)
176     {
177         $this->status   = $status;
178         $this->date     = new \DateTime();
179         $this->products = new \Doctrine\Common\Collections\ArrayCollection();
180     }
181     /**
182      * @return \DateTime
183      */
184     public function getDate()
185     {
186         return $this->date;
187     }
188
189     /**
190      * @return string
191      */
192     public function getStatus()
193     {
194         return $this->status;
195     }
196
197     /**
198      * @param string $status
199      */
200     public function setStatus($status)
201     {
202         $this->status = $status;
203     }
204
205     /**
206      * @return \Doctrine\Common\Collections\ArrayCollection
207      */
208     public function getProducts()
209     {
210         return $this->products;
211     }
212
213     /**
214      * @param DDC1430OrderProduct $product
215      */
216     public function addProduct(DDC1430OrderProduct $product)
217     {
218         $product->setOrder($this);
219         $this->products->add($product);
220     }
221 }
222
223 /**
224  * @Entity
225  */
226 class DDC1430OrderProduct
227 {
228
229      /**
230      * @Id
231      * @Column(type="integer")
232      * @GeneratedValue()
233      */
234     protected $id;
235
236     /**
237      * @var DDC1430Order $order
238      *
239      * @ManyToOne(targetEntity="DDC1430Order", inversedBy="products")
240      * @JoinColumn(name="order_id", referencedColumnName="order_id", nullable = false)
241      */
242     private $order;
243
244     /**
245      * @column(type="float")
246      */
247     private $value;
248
249     /**
250      * @param float $value
251      */
252     public function __construct($value)
253     {
254         $this->value = $value;
255     }
256
257      /**
258      * @return integer
259      */
260     public function getId()
261     {
262         return $this->id;
263     }
264
265     /**
266      * @return DDC1430Order
267      */
268     public function getOrder()
269     {
270         return $this->order;
271     }
272
273     /**
274      * @param DDC1430Order $order
275      */
276     public function setOrder(DDC1430Order $order)
277     {
278         $this->order = $order;
279     }
280
281     /**
282      * @return float
283      */
284     public function getValue()
285     {
286         return $this->value;
287     }
288
289     /**
290      * @param float $value
291      */
292     public function setValue($value)
293     {
294         $this->value = $value;
295     }
296
297 }