Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / DDC618Test.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-618
11  */
12 class DDC618Test 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__ . '\DDC618Author'),
20                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC618Book')
21             ));
22
23             // Create author 10/Joe with two books 22/JoeA and 20/JoeB
24             $author = new DDC618Author();
25             $author->id = 10;
26             $author->name = 'Joe';
27             $this->_em->persist($author);
28
29             // Create author 11/Alice with two books 21/AliceA and 23/AliceB
30             $author = new DDC618Author();
31             $author->id = 11;
32             $author->name = 'Alice';
33             $author->addBook('In Wonderland');
34             $author->addBook('Reloaded');
35             $author->addBook('Test');
36
37             $this->_em->persist($author);
38
39             $this->_em->flush();
40             $this->_em->clear();
41         } catch(\Exception $e) {
42
43         }
44     }
45
46     public function testIndexByHydrateObject()
47     {
48         $dql = 'SELECT A FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A INDEX BY A.name ORDER BY A.name ASC';
49         $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT);
50
51         $joe    = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', 10);
52         $alice  = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', 11);
53
54         $this->assertArrayHasKey('Joe', $result, "INDEX BY A.name should return an index by the name of 'Joe'.");
55         $this->assertArrayHasKey('Alice', $result, "INDEX BY A.name should return an index by the name of 'Alice'.");
56     }
57
58     public function testIndexByHydrateArray()
59     {
60         $dql = 'SELECT A FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A INDEX BY A.name ORDER BY A.name ASC';
61         $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
62
63         $joe    = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', 10);
64         $alice  = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', 11);
65
66         $this->assertArrayHasKey('Joe', $result, "INDEX BY A.name should return an index by the name of 'Joe'.");
67         $this->assertArrayHasKey('Alice', $result, "INDEX BY A.name should return an index by the name of 'Alice'.");
68     }
69
70     /**
71      * @group DDC-1018
72      */
73     public function testIndexByJoin()
74     {
75         $dql = 'SELECT A, B FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A '.
76                'INNER JOIN A.books B INDEX BY B.title ORDER BY A.name ASC';
77         $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT);
78
79         $this->assertEquals(3, count($result[0]->books)); // Alice, Joe doesnt appear because he has no books.
80         $this->assertEquals('Alice', $result[0]->name);
81         $this->assertTrue( isset($result[0]->books["In Wonderland"] ), "Indexing by title should have books by title.");
82         $this->assertTrue( isset($result[0]->books["Reloaded"] ), "Indexing by title should have books by title.");
83         $this->assertTrue( isset($result[0]->books["Test"] ), "Indexing by title should have books by title.");
84
85         $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
86
87         $this->assertEquals(3, count($result[0]['books'])); // Alice, Joe doesnt appear because he has no books.
88         $this->assertEquals('Alice', $result[0]['name']);
89         $this->assertTrue( isset($result[0]['books']["In Wonderland"] ), "Indexing by title should have books by title.");
90         $this->assertTrue( isset($result[0]['books']["Reloaded"] ), "Indexing by title should have books by title.");
91         $this->assertTrue( isset($result[0]['books']["Test"] ), "Indexing by title should have books by title.");
92     }
93
94     /**
95      * @group DDC-1018
96      */
97     public function testIndexByToOneJoinSilentlyIgnored()
98     {
99         $dql = 'SELECT B, A FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Book B '.
100                'INNER JOIN B.author A INDEX BY A.name ORDER BY A.name ASC';
101         $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT);
102
103         $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Ticket\DDC618Book', $result[0]);
104         $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', $result[0]->author);
105
106         $dql = 'SELECT B, A FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Book B '.
107                'INNER JOIN B.author A INDEX BY A.name ORDER BY A.name ASC';
108         $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
109
110         $this->assertEquals("Alice", $result[0]['author']['name']);
111     }
112
113     /**
114      * @group DDC-1018
115      */
116     public function testCombineIndexBy()
117     {
118         $dql = 'SELECT A, B FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A INDEX BY A.id '.
119                'INNER JOIN A.books B INDEX BY B.title ORDER BY A.name ASC';
120         $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT);
121
122         $this->assertArrayHasKey(11, $result); // Alice
123
124         $this->assertEquals(3, count($result[11]->books)); // Alice, Joe doesnt appear because he has no books.
125         $this->assertEquals('Alice', $result[11]->name);
126         $this->assertTrue( isset($result[11]->books["In Wonderland"] ), "Indexing by title should have books by title.");
127         $this->assertTrue( isset($result[11]->books["Reloaded"] ), "Indexing by title should have books by title.");
128         $this->assertTrue( isset($result[11]->books["Test"] ), "Indexing by title should have books by title.");
129     }
130 }
131
132 /**
133  * @Entity
134  */
135 class DDC618Author
136 {
137     /**
138      * @Id
139      * @Column(type="integer")
140      */
141     public $id;
142
143     /** @Column(type="string") */
144     public $name;
145
146     /**
147      * @OneToMany(targetEntity="DDC618Book", mappedBy="author", cascade={"persist"})
148      */
149     public $books;
150
151     public function __construct()
152     {
153         $this->books = new \Doctrine\Common\Collections\ArrayCollection;
154     }
155
156     public function addBook($title)
157     {
158         $book = new DDC618Book($title, $this);
159         $this->books[] = $book;
160     }
161 }
162
163 /**
164  * @Entity
165  */
166 class DDC618Book
167 {
168     /**
169      * @Id @GeneratedValue
170      * @Column(type="integer")
171      */
172     public $id;
173
174     /** @column(type="string") */
175     public $title;
176
177     /** @ManyToOne(targetEntity="DDC618Author", inversedBy="books") */
178     public $author;
179
180     function __construct($title, $author)
181     {
182         $this->title = $title;
183         $this->author = $author;
184     }
185 }