3 namespace Doctrine\Tests\ORM\Functional;
5 use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
6 use Doctrine\Tests\Models\ECommerce\ECommerceFeature;
7 use Doctrine\Common\Collections\Criteria;
9 require_once __DIR__ . '/../../TestInit.php';
12 * Tests a bidirectional one-to-one association mapping (without inheritance).
14 class OneToManyBidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
17 private $firstFeature;
18 private $secondFeature;
20 protected function setUp()
22 $this->useModelSet('ecommerce');
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');
32 public function testSavesAOneToManyAssociationWithCascadeSaveSet() {
33 $this->product->addFeature($this->firstFeature);
34 $this->product->addFeature($this->secondFeature);
35 $this->_em->persist($this->product);
38 $this->assertFeatureForeignKeyIs($this->product->getId(), $this->firstFeature);
39 $this->assertFeatureForeignKeyIs($this->product->getId(), $this->secondFeature);
42 public function testSavesAnEmptyCollection()
44 $this->_em->persist($this->product);
47 $this->assertEquals(0, count($this->product->getFeatures()));
50 public function testDoesNotSaveAnInverseSideSet() {
51 $this->product->brokenAddFeature($this->firstFeature);
52 $this->_em->persist($this->product);
55 $this->assertFeatureForeignKeyIs(null, $this->firstFeature);
58 public function testRemovesOneToOneAssociation()
60 $this->product->addFeature($this->firstFeature);
61 $this->product->addFeature($this->secondFeature);
62 $this->_em->persist($this->product);
64 $this->product->removeFeature($this->firstFeature);
67 $this->assertFeatureForeignKeyIs(null, $this->firstFeature);
68 $this->assertFeatureForeignKeyIs($this->product->getId(), $this->secondFeature);
71 public function testEagerLoadsOneToManyAssociation()
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];
78 $features = $product->getFeatures();
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());
90 public function testLazyLoadsObjectsOnTheOwningSide()
92 $this->_createFixture();
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();
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());
109 public function testLazyLoadsObjectsOnTheInverseSide()
111 $this->_createFixture();
113 $query = $this->_em->createQuery('select f from Doctrine\Tests\Models\ECommerce\ECommerceFeature f');
114 $features = $query->getResult();
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__);
124 public function testLazyLoadsObjectsOnTheInverseSide2()
126 //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
127 $this->_createFixture();
129 $query = $this->_em->createQuery('select f,p from Doctrine\Tests\Models\ECommerce\ECommerceFeature f join f.product p');
130 $features = $query->getResult();
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());
137 $this->assertFalse($product->getFeatures()->isInitialized());
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]));
144 //$this->_em->getConnection()->getConfiguration()->setSQLLogger(null);
147 public function testJoinFromOwningSide()
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));
157 public function testMatching()
159 $this->_createFixture();
161 $product = $this->_em->find('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $this->product->getId());
162 $features = $product->getFeatures();
164 $results = $features->matching(new Criteria(
165 Criteria::expr()->eq('description', 'Model writing tutorial')
168 $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $results);
169 $this->assertEquals(1, count($results));
171 $results = $features->matching(new Criteria());
173 $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $results);
174 $this->assertEquals(2, count($results));
177 public function testMatchingBis()
179 $this->_createFixture();
181 $product = $this->_em->find('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $this->product->getId());
182 $features = $product->getFeatures();
184 $thirdFeature = new ECommerceFeature();
185 $thirdFeature->setDescription('Third feature');
186 $product->addFeature($thirdFeature);
188 $results = $features->matching(new Criteria(
189 Criteria::expr()->eq('description', 'Third feature')
192 $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $results);
193 $this->assertCount(1, $results);
195 $results = $features->matching(new Criteria());
197 $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $results);
198 $this->assertCount(3, $results);
201 private function _createFixture()
203 $this->product->addFeature($this->firstFeature);
204 $this->product->addFeature($this->secondFeature);
205 $this->_em->persist($this->product);
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())
216 $this->assertEquals($value, $foreignKey);