Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / OneToManyBidirectionalAssociationTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
6 use Doctrine\Tests\Models\ECommerce\ECommerceFeature;
7 use Doctrine\Common\Collections\Criteria;
8
9 require_once __DIR__ . '/../../TestInit.php';
10
11 /**
12  * Tests a bidirectional one-to-one association mapping (without inheritance).
13  */
14 class OneToManyBidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
15 {
16     private $product;
17     private $firstFeature;
18     private $secondFeature;
19
20     protected function setUp()
21     {
22         $this->useModelSet('ecommerce');
23         parent::setUp();
24         $this->product = new ECommerceProduct();
25         $this->product->setName('Doctrine Cookbook');
26         $this->firstFeature = new ECommerceFeature();
27         $this->firstFeature->setDescription('Model writing tutorial');
28         $this->secondFeature = new ECommerceFeature();
29         $this->secondFeature->setDescription('Annotations examples');
30     }
31
32     public function testSavesAOneToManyAssociationWithCascadeSaveSet() {
33         $this->product->addFeature($this->firstFeature);
34         $this->product->addFeature($this->secondFeature);
35         $this->_em->persist($this->product);
36         $this->_em->flush();
37
38         $this->assertFeatureForeignKeyIs($this->product->getId(), $this->firstFeature);
39         $this->assertFeatureForeignKeyIs($this->product->getId(), $this->secondFeature);
40     }
41
42     public function testSavesAnEmptyCollection()
43     {
44         $this->_em->persist($this->product);
45         $this->_em->flush();
46
47         $this->assertEquals(0, count($this->product->getFeatures()));
48     }
49
50     public function testDoesNotSaveAnInverseSideSet() {
51         $this->product->brokenAddFeature($this->firstFeature);
52         $this->_em->persist($this->product);
53         $this->_em->flush();
54
55         $this->assertFeatureForeignKeyIs(null, $this->firstFeature);
56     }
57
58     public function testRemovesOneToOneAssociation()
59     {
60         $this->product->addFeature($this->firstFeature);
61         $this->product->addFeature($this->secondFeature);
62         $this->_em->persist($this->product);
63
64         $this->product->removeFeature($this->firstFeature);
65         $this->_em->flush();
66
67         $this->assertFeatureForeignKeyIs(null, $this->firstFeature);
68         $this->assertFeatureForeignKeyIs($this->product->getId(), $this->secondFeature);
69     }
70
71     public function testEagerLoadsOneToManyAssociation()
72     {
73         $this->_createFixture();
74         $query = $this->_em->createQuery('select p, f from Doctrine\Tests\Models\ECommerce\ECommerceProduct p join p.features f');
75         $result = $query->getResult();
76         $product = $result[0];
77
78         $features = $product->getFeatures();
79
80         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceFeature', $features[0]);
81         $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $features[0]->getProduct());
82         $this->assertSame($product, $features[0]->getProduct());
83         $this->assertEquals('Model writing tutorial', $features[0]->getDescription());
84         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceFeature', $features[1]);
85         $this->assertSame($product, $features[1]->getProduct());
86         $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $features[1]->getProduct());
87         $this->assertEquals('Annotations examples', $features[1]->getDescription());
88     }
89
90     public function testLazyLoadsObjectsOnTheOwningSide()
91     {
92         $this->_createFixture();
93
94         $query = $this->_em->createQuery('select p from Doctrine\Tests\Models\ECommerce\ECommerceProduct p');
95         $result = $query->getResult();
96         $product = $result[0];
97         $features = $product->getFeatures();
98
99         $this->assertFalse($features->isInitialized());
100         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceFeature', $features[0]);
101         $this->assertTrue($features->isInitialized());
102         $this->assertSame($product, $features[0]->getProduct());
103         $this->assertEquals('Model writing tutorial', $features[0]->getDescription());
104         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceFeature', $features[1]);
105         $this->assertSame($product, $features[1]->getProduct());
106         $this->assertEquals('Annotations examples', $features[1]->getDescription());
107     }
108
109     public function testLazyLoadsObjectsOnTheInverseSide()
110     {
111         $this->_createFixture();
112
113         $query = $this->_em->createQuery('select f from Doctrine\Tests\Models\ECommerce\ECommerceFeature f');
114         $features = $query->getResult();
115
116         $product = $features[0]->getProduct();
117         $this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $product);
118         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $product);
119         $this->assertFalse($product->__isInitialized__);
120         $this->assertSame('Doctrine Cookbook', $product->getName());
121         $this->assertTrue($product->__isInitialized__);
122     }
123
124     public function testLazyLoadsObjectsOnTheInverseSide2()
125     {
126         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
127         $this->_createFixture();
128
129         $query = $this->_em->createQuery('select f,p from Doctrine\Tests\Models\ECommerce\ECommerceFeature f join f.product p');
130         $features = $query->getResult();
131
132         $product = $features[0]->getProduct();
133         $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $product);
134         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $product);
135         $this->assertSame('Doctrine Cookbook', $product->getName());
136
137         $this->assertFalse($product->getFeatures()->isInitialized());
138
139         // This would trigger lazy-load
140         //$this->assertEquals(2, $product->getFeatures()->count());
141         //$this->assertTrue($product->getFeatures()->contains($features[0]));
142         //$this->assertTrue($product->getFeatures()->contains($features[1]));
143
144         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(null);
145     }
146
147     public function testJoinFromOwningSide()
148     {
149         $query = $this->_em->createQuery('select f,p from Doctrine\Tests\Models\ECommerce\ECommerceFeature f join f.product p');
150         $features = $query->getResult();
151         $this->assertEquals(0, count($features));
152     }
153
154     /**
155      * @group DDC-1637
156      */
157     public function testMatching()
158     {
159         $this->_createFixture();
160
161         $product  = $this->_em->find('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $this->product->getId());
162         $features = $product->getFeatures();
163
164         $results = $features->matching(new Criteria(
165             Criteria::expr()->eq('description', 'Model writing tutorial')
166         ));
167
168         $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $results);
169         $this->assertEquals(1, count($results));
170
171         $results = $features->matching(new Criteria());
172
173         $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $results);
174         $this->assertEquals(2, count($results));
175     }
176         
177     public function testMatchingBis()
178     {
179         $this->_createFixture();
180
181         $product  = $this->_em->find('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $this->product->getId());
182         $features = $product->getFeatures();
183         
184         $thirdFeature = new ECommerceFeature();
185         $thirdFeature->setDescription('Third feature');
186         $product->addFeature($thirdFeature);
187
188         $results = $features->matching(new Criteria(
189             Criteria::expr()->eq('description', 'Third feature')
190         ));
191
192         $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $results);
193         $this->assertCount(1, $results);
194
195         $results = $features->matching(new Criteria());
196
197         $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $results);
198         $this->assertCount(3, $results);
199     }
200
201     private function _createFixture()
202     {
203         $this->product->addFeature($this->firstFeature);
204         $this->product->addFeature($this->secondFeature);
205         $this->_em->persist($this->product);
206
207         $this->_em->flush();
208         $this->_em->clear();
209     }
210
211     public function assertFeatureForeignKeyIs($value, ECommerceFeature $feature) {
212         $foreignKey = $this->_em->getConnection()->executeQuery(
213             'SELECT product_id FROM ecommerce_features WHERE id=?',
214             array($feature->getId())
215         )->fetchColumn();
216         $this->assertEquals($value, $foreignKey);
217     }
218 }