Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / OneToOneUnidirectionalAssociationTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
6 use Doctrine\Tests\Models\ECommerce\ECommerceShipping;
7 use Doctrine\ORM\Mapping\AssociationMapping;
8 use Doctrine\ORM\Mapping\ClassMetadata;
9 use Doctrine\ORM\Query;
10
11 require_once __DIR__ . '/../../TestInit.php';
12
13 /**
14  * Tests a unidirectional one-to-one association mapping (without inheritance).
15  * Inverse side is not present.
16  */
17 class OneToOneUnidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
18 {
19     private $product;
20     private $shipping;
21
22     protected function setUp()
23     {
24         $this->useModelSet('ecommerce');
25         parent::setUp();
26         $this->product = new ECommerceProduct();
27         $this->product->setName('Doctrine 2 Manual');
28         $this->shipping = new ECommerceShipping();
29         $this->shipping->setDays('5');
30     }
31
32     public function testSavesAOneToOneAssociationWithCascadeSaveSet() {
33         $this->product->setShipping($this->shipping);
34         $this->_em->persist($this->product);
35         $this->_em->flush();
36
37         $this->assertForeignKeyIs($this->shipping->getId());
38     }
39
40     public function testRemovesOneToOneAssociation()
41     {
42         $this->product->setShipping($this->shipping);
43         $this->_em->persist($this->product);
44         $this->product->removeShipping();
45
46         $this->_em->flush();
47
48         $this->assertForeignKeyIs(null);
49     }
50
51     public function _testEagerLoad()
52     {
53         $this->_createFixture();
54
55         $query = $this->_em->createQuery('select p, s from Doctrine\Tests\Models\ECommerce\ECommerceProduct p left join p.shipping s');
56         $result = $query->getResult();
57         $product = $result[0];
58
59         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceShipping', $product->getShipping());
60         $this->assertEquals(1, $product->getShipping()->getDays());
61     }
62
63     public function testLazyLoadsObjects() {
64         $this->_createFixture();
65         $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceProduct');
66         $metadata->associationMappings['shipping']['fetch'] = ClassMetadata::FETCH_LAZY;
67
68         $query = $this->_em->createQuery('select p from Doctrine\Tests\Models\ECommerce\ECommerceProduct p');
69         $result = $query->getResult();
70         $product = $result[0];
71
72         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceShipping', $product->getShipping());
73         $this->assertEquals(1, $product->getShipping()->getDays());
74     }
75
76     public function testDoesNotLazyLoadObjectsIfConfigurationDoesNotAllowIt() {
77         $this->_createFixture();
78
79         $query = $this->_em->createQuery('select p from Doctrine\Tests\Models\ECommerce\ECommerceProduct p');
80         $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
81
82         $result = $query->getResult();
83         $product = $result[0];
84
85         $this->assertNull($product->getShipping());
86     }
87
88     protected function _createFixture()
89     {
90         $product = new ECommerceProduct;
91         $product->setName('Php manual');
92         $shipping = new ECommerceShipping;
93         $shipping->setDays('1');
94         $product->setShipping($shipping);
95
96         $this->_em->persist($product);
97
98         $this->_em->flush();
99         $this->_em->clear();
100     }
101
102     public function assertForeignKeyIs($value) {
103         $foreignKey = $this->_em->getConnection()->executeQuery(
104             'SELECT shipping_id FROM ecommerce_products WHERE id=?',
105             array($this->product->getId())
106         )->fetchColumn();
107         $this->assertEquals($value, $foreignKey);
108     }
109
110     /**
111      * @group DDC-762
112      */
113     public function testNullForeignKey()
114     {
115         $product = new ECommerceProduct();
116         $product->setName('Doctrine 2 Manual');
117
118         $this->_em->persist($product);
119         $this->_em->flush();
120
121         $product = $this->_em->find(get_class($product), $product->getId());
122
123         $this->assertNull($product->getShipping());
124     }
125 }