Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / DDC1335Test.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5 use DateTime;
6
7 require_once __DIR__ . '/../../../TestInit.php';
8
9 /**
10  * @group DDC-1335
11  */
12 class DDC1335Test extends \Doctrine\Tests\OrmFunctionalTestCase
13 {
14     protected function setUp()
15     {
16         parent::setUp();
17         try {
18             $this->_schemaTool->createSchema(array(
19                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1335User'),
20                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1335Phone'),
21             ));
22             $this->loadFixture();
23         } catch(\Exception $e) {
24         }
25     }
26
27
28     public function testDql()
29     {
30         $dql      = 'SELECT u FROM ' . __NAMESPACE__ . '\DDC1335User u INDEX BY u.id';
31         $query    = $this->_em->createQuery($dql);
32         $result   = $query->getResult();
33
34         $this->assertEquals(sizeof($result), 3);
35         $this->assertArrayHasKey(1, $result);
36         $this->assertArrayHasKey(2, $result);
37         $this->assertArrayHasKey(3, $result);
38
39         $dql      = 'SELECT u, p FROM '.__NAMESPACE__ . '\DDC1335User u INDEX BY u.email INNER JOIN u.phones p INDEX BY p.id';
40         $query    = $this->_em->createQuery($dql);
41         $result   = $query->getResult();
42
43         $this->assertEquals(sizeof($result), 3);
44         $this->assertArrayHasKey('foo@foo.com', $result);
45         $this->assertArrayHasKey('bar@bar.com', $result);
46         $this->assertArrayHasKey('foobar@foobar.com', $result);
47
48         $this->assertEquals(sizeof($result['foo@foo.com']->phones), 3);
49         $this->assertEquals(sizeof($result['bar@bar.com']->phones), 3);
50         $this->assertEquals(sizeof($result['foobar@foobar.com']->phones), 3);
51
52         $foo = $result['foo@foo.com']->phones->toArray();
53         $bar = $result['bar@bar.com']->phones->toArray();
54         $foobar = $result['foobar@foobar.com']->phones->toArray();
55
56         $this->assertArrayHasKey(1, $foo);
57         $this->assertArrayHasKey(2, $foo);
58         $this->assertArrayHasKey(3, $foo);
59
60         $this->assertArrayHasKey(4, $bar);
61         $this->assertArrayHasKey(5, $bar);
62         $this->assertArrayHasKey(6, $bar);
63
64         $this->assertArrayHasKey(7, $foobar);
65         $this->assertArrayHasKey(8, $foobar);
66         $this->assertArrayHasKey(9, $foobar);
67     }
68
69     public function testTicket()
70     {
71         $builder = $this->_em->createQueryBuilder();
72         $builder->select('u')->from(__NAMESPACE__ . '\DDC1335User', 'u', 'u.id');
73
74         $dql    = $builder->getQuery()->getDQL();
75         $result = $builder->getQuery()->getResult();
76
77         $this->assertEquals(sizeof($result), 3);
78         $this->assertArrayHasKey(1, $result);
79         $this->assertArrayHasKey(2, $result);
80         $this->assertArrayHasKey(3, $result);
81         $this->assertEquals('SELECT u FROM ' . __NAMESPACE__ . '\DDC1335User u INDEX BY u.id', $dql);
82     }
83
84     public function testIndexByUnique()
85     {
86         $builder = $this->_em->createQueryBuilder();
87         $builder->select('u')->from(__NAMESPACE__ . '\DDC1335User', 'u', 'u.email');
88
89         $dql    = $builder->getQuery()->getDQL();
90         $result = $builder->getQuery()->getResult();
91
92         $this->assertEquals(sizeof($result), 3);
93         $this->assertArrayHasKey('foo@foo.com', $result);
94         $this->assertArrayHasKey('bar@bar.com', $result);
95         $this->assertArrayHasKey('foobar@foobar.com', $result);
96         $this->assertEquals('SELECT u FROM ' . __NAMESPACE__ . '\DDC1335User u INDEX BY u.email', $dql);
97     }
98
99     public function  testIndexWithJoin()
100     {
101         $builder = $this->_em->createQueryBuilder();
102         $builder->select('u','p')
103                 ->from(__NAMESPACE__ . '\DDC1335User', 'u', 'u.email')
104                 ->join('u.phones', 'p', null, null, 'p.id');
105
106         $dql    = $builder->getQuery()->getDQL();
107         $result = $builder->getQuery()->getResult();
108
109         $this->assertEquals(sizeof($result), 3);
110         $this->assertArrayHasKey('foo@foo.com', $result);
111         $this->assertArrayHasKey('bar@bar.com', $result);
112         $this->assertArrayHasKey('foobar@foobar.com', $result);
113
114         $this->assertEquals(sizeof($result['foo@foo.com']->phones), 3);
115         $this->assertEquals(sizeof($result['bar@bar.com']->phones), 3);
116         $this->assertEquals(sizeof($result['foobar@foobar.com']->phones), 3);
117
118         $this->assertArrayHasKey(1, $result['foo@foo.com']->phones->toArray());
119         $this->assertArrayHasKey(2, $result['foo@foo.com']->phones->toArray());
120         $this->assertArrayHasKey(3, $result['foo@foo.com']->phones->toArray());
121
122         $this->assertArrayHasKey(4, $result['bar@bar.com']->phones->toArray());
123         $this->assertArrayHasKey(5, $result['bar@bar.com']->phones->toArray());
124         $this->assertArrayHasKey(6, $result['bar@bar.com']->phones->toArray());
125
126         $this->assertArrayHasKey(7, $result['foobar@foobar.com']->phones->toArray());
127         $this->assertArrayHasKey(8, $result['foobar@foobar.com']->phones->toArray());
128         $this->assertArrayHasKey(9, $result['foobar@foobar.com']->phones->toArray());
129
130         $this->assertEquals('SELECT u, p FROM '.__NAMESPACE__ . '\DDC1335User u INDEX BY u.email INNER JOIN u.phones p INDEX BY p.id', $dql);
131     }
132
133     private function loadFixture()
134     {
135         $p1 = array('11 xxxx-xxxx','11 yyyy-yyyy','11 zzzz-zzzz');
136         $p2 = array('22 xxxx-xxxx','22 yyyy-yyyy','22 zzzz-zzzz');
137         $p3 = array('33 xxxx-xxxx','33 yyyy-yyyy','33 zzzz-zzzz');
138
139         $u1 = new DDC1335User("foo@foo.com", "Foo",$p1);
140         $u2 = new DDC1335User("bar@bar.com", "Bar",$p2);
141         $u3 = new DDC1335User("foobar@foobar.com", "Foo Bar",$p3);
142
143         $this->_em->persist($u1);
144         $this->_em->persist($u2);
145         $this->_em->persist($u3);
146         $this->_em->flush();
147         $this->_em->clear();
148     }
149
150 }
151
152 /**
153  * @Entity
154  */
155 class DDC1335User
156 {
157     /**
158      * @Id @Column(type="integer")
159      * @GeneratedValue
160      */
161     public $id;
162
163     /**
164      * @Column(type="string", unique=true)
165      */
166     public $email;
167
168     /**
169      * @Column(type="string")
170      */
171     public $name;
172
173     /**
174      * @OneToMany(targetEntity="DDC1335Phone", mappedBy="user", cascade={"persist", "remove"})
175      */
176     public $phones;
177
178     public function __construct($email, $name, array $numbers = array())
179     {
180         $this->name   = $name;
181         $this->email  = $email;
182         $this->phones = new \Doctrine\Common\Collections\ArrayCollection();
183
184         foreach ($numbers as $number) {
185             $this->phones->add(new DDC1335Phone($this,$number));
186         }
187     }
188 }
189
190 /**
191  * @Entity
192  */
193 class DDC1335Phone
194 {
195     /**
196      * @Id
197      * @Column(name="id", type="integer")
198      * @GeneratedValue
199      */
200     public $id;
201
202     /**
203      * @Column(name="numericalValue", type="string", nullable = false)
204      */
205     public $numericalValue;
206
207     /**
208      * @ManyToOne(targetEntity="DDC1335User", inversedBy="phones")
209      * @JoinColumn(name="user_id", referencedColumnName="id", nullable = false)
210      */
211     public $user;
212
213     public function __construct($user, $number)
214     {
215         $this->user     = $user;
216         $this->numericalValue   = $number;
217     }
218 }