Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / DDC1163Test.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5 require_once __DIR__ . '/../../../TestInit.php';
6
7 /**
8  * @group DDC-1163
9  */
10 class DDC1163Test extends \Doctrine\Tests\OrmFunctionalTestCase
11 {
12     protected function setUp()
13     {
14         parent::setUp();
15         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
16         $this->_schemaTool->createSchema(array(
17             $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1163Product'),
18             $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1163SpecialProduct'),
19             $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1163ProxyHolder'),
20             $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1163Tag'),
21         ));
22     }
23
24     public function testIssue()
25     {
26         $this->createSpecialProductAndProxyHolderReferencingIt();
27         $this->_em->clear();
28
29         $this->createProxyForSpecialProduct();
30
31         $this->setPropertyAndAssignTagToSpecialProduct();
32
33         // fails
34         $this->_em->flush();
35     }
36
37     private function createSpecialProductAndProxyHolderReferencingIt()
38     {
39         $specialProduct = new DDC1163SpecialProduct();
40         $this->_em->persist($specialProduct);
41
42         $proxyHolder = new DDC1163ProxyHolder();
43         $this->_em->persist($proxyHolder);
44
45         $proxyHolder->setSpecialProduct($specialProduct);
46
47         $this->_em->flush();
48
49         $this->productId = $specialProduct->getId();
50         $this->proxyHolderId = $proxyHolder->getId();
51     }
52
53     /**
54      * We want Doctrine to instantiate a lazy-load proxy for the previously created
55      * 'SpecialProduct' and register it.
56      *
57      * When Doctrine loads the 'ProxyHolder', it will do just that because the 'ProxyHolder'
58      * references the 'SpecialProduct'.
59      */
60     private function createProxyForSpecialProduct()
61     {
62         /* @var $proxyHolder ProxyHolder */
63         $proxyHolder = $this->_em->find(__NAMESPACE__ . '\\DDC1163ProxyHolder', $this->proxyHolderId);
64
65         $this->assertInstanceOf(__NAMESPACE__.'\\DDC1163SpecialProduct', $proxyHolder->getSpecialProduct());
66     }
67
68     private function setPropertyAndAssignTagToSpecialProduct()
69     {
70         /* @var $specialProduct SpecialProduct */
71         $specialProduct = $this->_em->find(__NAMESPACE__ . '\\DDC1163SpecialProduct', $this->productId);
72
73         $this->assertInstanceOf(__NAMESPACE__.'\\DDC1163SpecialProduct', $specialProduct);
74         $this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $specialProduct);
75
76         $specialProduct->setSubclassProperty('foobar');
77
78         // this screams violation of law of demeter ;)
79         $this->assertEquals(
80             __NAMESPACE__.'\\DDC1163SpecialProduct',
81             $this->_em->getUnitOfWork()->getEntityPersister(get_class($specialProduct))->getClassMetadata()->name
82         );
83
84         $tag = new DDC1163Tag('Foo');
85         $this->_em->persist($tag);
86         $tag->setProduct($specialProduct);
87     }
88 }
89
90 /**
91  * @Entity
92  */
93 class DDC1163ProxyHolder
94 {
95
96     /**
97      * @var int
98      * @Column(name="id", type="integer")
99      * @Id
100      * @GeneratedValue(strategy="AUTO")
101      */
102     private $id;
103     /**
104      * @var SpecialProduct
105      * @OneToOne(targetEntity="DDC1163SpecialProduct")
106      */
107     private $specialProduct;
108
109     public function getId()
110     {
111         return $this->id;
112     }
113
114     public function setSpecialProduct(DDC1163SpecialProduct $specialProduct)
115     {
116         $this->specialProduct = $specialProduct;
117     }
118
119     public function getSpecialProduct()
120     {
121         return $this->specialProduct;
122     }
123
124 }
125
126 /**
127  * @Entity
128  * @InheritanceType("JOINED")
129  * @DiscriminatorColumn(name="type", type="string")
130  * @DiscriminatorMap({"special" = "DDC1163SpecialProduct"})
131  */
132 abstract class DDC1163Product
133 {
134
135     /**
136      * @var int
137      * @Column(name="id", type="integer")
138      * @Id
139      * @GeneratedValue(strategy="AUTO")
140      */
141     protected $id;
142
143     public function getId()
144     {
145         return $this->id;
146     }
147
148 }
149
150 /**
151  * @Entity
152  */
153 class DDC1163SpecialProduct extends DDC1163Product
154 {
155
156     /**
157      * @var string
158      * @Column(name="subclass_property", type="string", nullable=true)
159      */
160     private $subclassProperty;
161
162     /**
163      * @param string $value
164      */
165     public function setSubclassProperty($value)
166     {
167         $this->subclassProperty = $value;
168     }
169
170 }
171
172 /**
173  * @Entity
174  */
175 class DDC1163Tag
176 {
177
178     /**
179      * @var int
180      * @Column(name="id", type="integer")
181      * @Id
182      * @GeneratedValue(strategy="AUTO")
183      */
184     private $id;
185     /**
186      * @var string
187      * @Column(name="name", type="string")
188      */
189     private $name;
190     /**
191      * @var Product
192      * @ManyToOne(targetEntity="DDC1163Product", inversedBy="tags")
193      * @JoinColumns({
194      *   @JoinColumn(name="product_id", referencedColumnName="id")
195      * })
196      */
197     private $product;
198
199     /**
200      * @param string $name
201      */
202     public function __construct($name)
203     {
204         $this->name = $name;
205     }
206
207     /**
208      * @param Product $product
209      */
210     public function setProduct(DDC1163Product $product)
211     {
212         $this->product = $product;
213     }
214
215 }