Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / DDC1545Test.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5 use Doctrine\Common\Collections\ArrayCollection;
6
7 use Doctrine\Tests\Models\CMS\CmsComment;
8 use Doctrine\Tests\Models\CMS\CmsArticle;
9 use Doctrine\Tests\Models\CMS\CmsUser;
10
11 require_once __DIR__ . '/../../../TestInit.php';
12
13 /**
14  * @group DDC-1545
15  */
16 class DDC1545Test extends \Doctrine\Tests\OrmFunctionalTestCase
17 {
18     private $articleId;
19
20     private $userId;
21
22     private $user2Id;
23
24     public function setUp()
25     {
26         $this->useModelSet('cms');
27         parent::setUp();
28     }
29
30     private function initDb($link)
31     {
32         $article = new CmsArticle();
33         $article->topic = 'foo';
34         $article->text = 'foo';
35
36         $user = new CmsUser();
37         $user->status = 'foo';
38         $user->username = 'foo';
39         $user->name = 'foo';
40
41         $user2 = new CmsUser();
42         $user2->status = 'bar';
43         $user2->username = 'bar';
44         $user2->name = 'bar';
45
46         if ($link) {
47             $article->user = $user;
48         }
49
50         $this->_em->persist($article);
51         $this->_em->persist($user);
52         $this->_em->persist($user2);
53         $this->_em->flush();
54         $this->_em->clear();
55
56         $this->articleId = $article->id;
57         $this->userId = $user->id;
58         $this->user2Id = $user2->id;
59     }
60
61     public function testLinkObjects()
62     {
63         $this->initDb(false);
64
65         // don't join association
66         $article = $this->_em->find('Doctrine\Tests\Models\Cms\CmsArticle', $this->articleId);
67
68         $user = $this->_em->find('Doctrine\Tests\Models\Cms\CmsUser', $this->userId);
69
70         $article->user = $user;
71
72         $this->_em->flush();
73         $this->_em->clear();
74
75         $article = $this->_em
76             ->createQuery('SELECT a, u FROM Doctrine\Tests\Models\Cms\CmsArticle a LEFT JOIN a.user u WHERE a.id = :id')
77             ->setParameter('id', $this->articleId)
78             ->getOneOrNullResult();
79
80         $this->assertNotNull($article->user);
81         $this->assertEquals($user->id, $article->user->id);
82     }
83
84     public function testLinkObjectsWithAssociationLoaded()
85     {
86         $this->initDb(false);
87
88         // join association
89         $article = $this->_em
90             ->createQuery('SELECT a, u FROM Doctrine\Tests\Models\Cms\CmsArticle a LEFT JOIN a.user u WHERE a.id = :id')
91             ->setParameter('id', $this->articleId)
92             ->getOneOrNullResult();
93
94         $user = $this->_em->find('Doctrine\Tests\Models\Cms\CmsUser', $this->userId);
95
96         $article->user = $user;
97
98         $this->_em->flush();
99         $this->_em->clear();
100
101         $article = $this->_em
102             ->createQuery('SELECT a, u FROM Doctrine\Tests\Models\Cms\CmsArticle a LEFT JOIN a.user u WHERE a.id = :id')
103             ->setParameter('id', $this->articleId)
104             ->getOneOrNullResult();
105
106         $this->assertNotNull($article->user);
107         $this->assertEquals($user->id, $article->user->id);
108     }
109
110     public function testUnlinkObjects()
111     {
112         $this->initDb(true);
113
114         // don't join association
115         $article = $this->_em->find('Doctrine\Tests\Models\Cms\CmsArticle', $this->articleId);
116
117         $article->user = null;
118
119         $this->_em->flush();
120         $this->_em->clear();
121
122         $article = $this->_em
123             ->createQuery('SELECT a, u FROM Doctrine\Tests\Models\Cms\CmsArticle a LEFT JOIN a.user u WHERE a.id = :id')
124             ->setParameter('id', $this->articleId)
125             ->getOneOrNullResult();
126
127         $this->assertNull($article->user);
128     }
129
130     public function testUnlinkObjectsWithAssociationLoaded()
131     {
132         $this->initDb(true);
133
134         // join association
135         $article = $this->_em
136             ->createQuery('SELECT a, u FROM Doctrine\Tests\Models\Cms\CmsArticle a LEFT JOIN a.user u WHERE a.id = :id')
137             ->setParameter('id', $this->articleId)
138             ->getOneOrNullResult();
139
140         $article->user = null;
141
142         $this->_em->flush();
143         $this->_em->clear();
144
145         $article = $this->_em
146             ->createQuery('SELECT a, u FROM Doctrine\Tests\Models\Cms\CmsArticle a LEFT JOIN a.user u WHERE a.id = :id')
147             ->setParameter('id', $this->articleId)
148             ->getOneOrNullResult();
149
150         $this->assertNull($article->user);
151     }
152
153     public function testChangeLink()
154     {
155         $this->initDb(false);
156
157         // don't join association
158         $article = $this->_em->find('Doctrine\Tests\Models\Cms\CmsArticle', $this->articleId);
159
160         $user2 = $this->_em->find('Doctrine\Tests\Models\Cms\CmsUser', $this->user2Id);
161
162         $article->user = $user2;
163
164         $this->_em->flush();
165         $this->_em->clear();
166
167         $article = $this->_em
168             ->createQuery('SELECT a, u FROM Doctrine\Tests\Models\Cms\CmsArticle a LEFT JOIN a.user u WHERE a.id = :id')
169             ->setParameter('id', $this->articleId)
170             ->getOneOrNullResult();
171
172         $this->assertNotNull($article->user);
173         $this->assertEquals($user2->id, $article->user->id);
174     }
175
176     public function testChangeLinkWithAssociationLoaded()
177     {
178         $this->initDb(false);
179
180         // join association
181         $article = $this->_em
182             ->createQuery('SELECT a, u FROM Doctrine\Tests\Models\Cms\CmsArticle a LEFT JOIN a.user u WHERE a.id = :id')
183             ->setParameter('id', $this->articleId)
184             ->getOneOrNullResult();
185
186         $user2 = $this->_em->find('Doctrine\Tests\Models\Cms\CmsUser', $this->user2Id);
187
188         $article->user = $user2;
189
190         $this->_em->flush();
191         $this->_em->clear();
192
193         $article = $this->_em
194             ->createQuery('SELECT a, u FROM Doctrine\Tests\Models\Cms\CmsArticle a LEFT JOIN a.user u WHERE a.id = :id')
195             ->setParameter('id', $this->articleId)
196             ->getOneOrNullResult();
197
198         $this->assertNotNull($article->user);
199         $this->assertEquals($user2->id, $article->user->id);
200     }
201 }