Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / DDC1301Test.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5 use Doctrine\ORM\Mapping\ClassMetadataInfo;
6
7 require_once __DIR__ . '/../../../TestInit.php';
8
9 /**
10  * @author asm89
11  */
12 class DDC1301Test extends \Doctrine\Tests\OrmFunctionalTestCase
13 {
14     private $userId;
15
16     public function setUp()
17     {
18         $this->useModelSet('legacy');
19         parent::setUp();
20
21         $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\Legacy\LegacyUser');
22         $class->associationMappings['_articles']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
23         $class->associationMappings['_references']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
24         $class->associationMappings['_cars']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY;
25
26         $this->loadFixture();
27     }
28
29     public function tearDown()
30     {
31         parent::tearDown();
32
33         $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\Legacy\LegacyUser');
34         $class->associationMappings['_articles']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
35         $class->associationMappings['_references']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
36         $class->associationMappings['_cars']['fetch'] = ClassMetadataInfo::FETCH_LAZY;
37     }
38
39     public function testCountNotInitializesLegacyCollection()
40     {
41         $user = $this->_em->find('Doctrine\Tests\Models\Legacy\LegacyUser', $this->userId);
42         $queryCount = $this->getCurrentQueryCount();
43
44         $this->assertFalse($user->_articles->isInitialized());
45         $this->assertEquals(2, count($user->_articles));
46         $this->assertFalse($user->_articles->isInitialized());
47
48         foreach ($user->_articles AS $article) { }
49
50         $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration.");
51     }
52
53     public function testCountNotInitializesLegacyCollectionWithForeignIdentifier()
54     {
55         $user = $this->_em->find('Doctrine\Tests\Models\Legacy\LegacyUser', $this->userId);
56         $queryCount = $this->getCurrentQueryCount();
57
58         $this->assertFalse($user->_references->isInitialized());
59         $this->assertEquals(2, count($user->_references));
60         $this->assertFalse($user->_references->isInitialized());
61
62         foreach ($user->_references AS $reference) { }
63
64         $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration.");
65     }
66
67     public function testCountNotInitializesLegacyManyToManyCollection()
68     {
69         $user = $this->_em->find('Doctrine\Tests\Models\Legacy\LegacyUser', $this->userId);
70         $queryCount = $this->getCurrentQueryCount();
71
72         $this->assertFalse($user->_cars->isInitialized());
73         $this->assertEquals(3, count($user->_cars));
74         $this->assertFalse($user->_cars->isInitialized());
75
76         foreach ($user->_cars AS $reference) { }
77
78         $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration.");
79     }
80
81     public function loadFixture()
82     {
83         $user1 = new \Doctrine\Tests\Models\Legacy\LegacyUser();
84         $user1->_username = "beberlei";
85         $user1->_name = "Benjamin";
86         $user1->_status = "active";
87
88         $user2 = new \Doctrine\Tests\Models\Legacy\LegacyUser();
89         $user2->_username = "jwage";
90         $user2->_name = "Jonathan";
91         $user2->_status = "active";
92
93         $user3 = new \Doctrine\Tests\Models\Legacy\LegacyUser();
94         $user3->_username = "romanb";
95         $user3->_name = "Roman";
96         $user3->_status = "active";
97
98         $this->_em->persist($user1);
99         $this->_em->persist($user2);
100         $this->_em->persist($user3);
101
102         $article1 = new \Doctrine\Tests\Models\Legacy\LegacyArticle();
103         $article1->_topic = "Test";
104         $article1->_text = "Test";
105         $article1->setAuthor($user1);
106
107         $article2 = new \Doctrine\Tests\Models\Legacy\LegacyArticle();
108         $article2->_topic = "Test";
109         $article2->_text = "Test";
110         $article2->setAuthor($user1);
111
112         $this->_em->persist($article1);
113         $this->_em->persist($article2);
114
115         $car1 = new \Doctrine\Tests\Models\Legacy\LegacyCar();
116         $car1->_description = "Test1";
117
118         $car2 = new \Doctrine\Tests\Models\Legacy\LegacyCar();
119         $car2->_description = "Test2";
120
121         $car3 = new \Doctrine\Tests\Models\Legacy\LegacyCar();
122         $car3->_description = "Test3";
123
124         $user1->addCar($car1);
125         $user1->addCar($car2);
126         $user1->addCar($car3);
127
128         $user2->addCar($car1);
129         $user3->addCar($car1);
130
131         $this->_em->persist($car1);
132         $this->_em->persist($car2);
133         $this->_em->persist($car3);
134
135         $this->_em->flush();
136
137         $detail1 = new \Doctrine\Tests\Models\Legacy\LegacyUserReference($user1, $user2, "foo");
138         $detail2 = new \Doctrine\Tests\Models\Legacy\LegacyUserReference($user1, $user3, "bar");
139
140         $this->_em->persist($detail1);
141         $this->_em->persist($detail2);
142
143         $this->_em->flush();
144         $this->_em->clear();
145
146         $this->userId = $user1->getId();
147     }
148 }