Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / tests / Doctrine / Tests / Common / Persistence / PersistentObjectTest.php
1 <?php
2
3 namespace Doctrine\Tests\Common\Persistence;
4
5 use Doctrine\Common\Persistence\PersistentObject;
6 use Doctrine\Common\Persistence\Mapping\ClassMetadata;
7 use Doctrine\Common\Persistence\Mapping\ReflectionService;
8
9 /**
10  * @group DDC-1448
11  */
12 class PersistentObjectTest extends \Doctrine\Tests\DoctrineTestCase
13 {
14     private $cm;
15     private $om;
16     private $object;
17
18     public function setUp()
19     {
20         $this->cm = new TestObjectMetadata;
21         $this->om = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
22         $this->om->expects($this->any())->method('getClassMetadata')
23                  ->will($this->returnValue($this->cm));
24         $this->object = new TestObject;
25         PersistentObject::setObjectManager($this->om);
26         $this->object->injectObjectManager($this->om, $this->cm);
27     }
28
29     public function testGetObjectManager()
30     {
31         $this->assertSame($this->om, PersistentObject::getObjectManager());
32     }
33
34     public function testNonMatchingObjectManager()
35     {
36         $this->setExpectedException('RuntimeException');
37         $om = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
38         $this->object->injectObjectManager($om, $this->cm);
39     }
40
41     public function testGetField()
42     {
43         $this->assertEquals('beberlei', $this->object->getName());
44     }
45
46     public function testSetField()
47     {
48         $this->object->setName("test");
49         $this->assertEquals("test", $this->object->getName());
50     }
51
52     public function testGetIdentifier()
53     {
54         $this->assertEquals(1, $this->object->getId());
55     }
56
57     public function testSetIdentifier()
58     {
59         $this->setExpectedException('BadMethodCallException');
60         $this->object->setId(2);
61     }
62
63     public function testSetUnknownField()
64     {
65         $this->setExpectedException('BadMethodCallException');
66         $this->object->setUnknown("test");
67     }
68
69     public function testGetUnknownField()
70     {
71         $this->setExpectedException('BadMethodCallException');
72         $this->object->getUnknown();
73     }
74
75     public function testGetToOneAssociation()
76     {
77         $this->assertNull($this->object->getParent());
78     }
79
80     public function testSetToOneAssociation()
81     {
82         $parent = new TestObject();
83         $this->object->setParent($parent);
84         $this->assertSame($parent, $this->object->getParent($parent));
85     }
86
87     public function testSetInvalidToOneAssocation()
88     {
89         $parent = new \stdClass();
90
91         $this->setExpectedException('InvalidArgumentException');
92         $this->object->setParent($parent);
93     }
94
95     public function testSetToOneAssociationNull()
96     {
97         $parent = new TestObject();
98         $this->object->setParent($parent);
99         $this->object->setParent(null);
100         $this->assertNull($this->object->getParent());
101     }
102
103     public function testAddToManyAssocation()
104     {
105         $child = new TestObject();
106         $this->object->addChildren($child);
107
108         $this->assertSame($this->object, $child->getParent());
109         $this->assertEquals(1, count($this->object->getChildren()));
110
111         $child = new TestObject();
112         $this->object->addChildren($child);
113
114         $this->assertEquals(2, count($this->object->getChildren()));
115     }
116
117     public function testAddInvalidToManyAssocation()
118     {
119         $this->setExpectedException('InvalidArgumentException');
120         $this->object->addChildren(new \stdClass());
121     }
122
123     public function testNoObjectManagerSet()
124     {
125         PersistentObject::setObjectManager(null);
126         $child = new TestObject();
127
128         $this->setExpectedException('RuntimeException');
129         $child->setName("test");
130     }
131
132     public function testInvalidMethod()
133     {
134         $this->setExpectedException('BadMethodCallException');
135         $this->object->asdf();
136     }
137
138     public function testAddInvalidCollection()
139     {
140         $this->setExpectedException('BadMethodCallException');
141         $this->object->addAsdf(new \stdClass());
142     }
143 }
144
145 class TestObject extends PersistentObject
146 {
147     protected $id = 1;
148     protected $name = 'beberlei';
149     protected $parent;
150     protected $children;
151 }
152
153 class TestObjectMetadata implements ClassMetadata
154 {
155
156     public function getAssociationMappedByTargetField($assocName)
157     {
158         $assoc = array('children' => 'parent');
159         return $assoc[$assocName];
160     }
161
162     public function getAssociationNames()
163     {
164         return array('parent', 'children');
165     }
166
167     public function getAssociationTargetClass($assocName)
168     {
169         return __NAMESPACE__ . '\TestObject';
170     }
171
172     public function getFieldNames()
173     {
174         return array('id', 'name');
175     }
176
177     public function getIdentifier()
178     {
179         return array('id');
180     }
181
182     public function getName()
183     {
184         return __NAMESPACE__ . '\TestObject';
185     }
186
187     public function getReflectionClass()
188     {
189         return new \ReflectionClass($this->getName());
190     }
191
192     public function getTypeOfField($fieldName)
193     {
194         $types = array('id' => 'integer', 'name' => 'string');
195         return $types[$fieldName];
196     }
197
198     public function hasAssociation($fieldName)
199     {
200         return in_array($fieldName, array('parent', 'children'));
201     }
202
203     public function hasField($fieldName)
204     {
205         return in_array($fieldName, array('id', 'name'));
206     }
207
208     public function isAssociationInverseSide($assocName)
209     {
210         return ($assocName === 'children');
211     }
212
213     public function isCollectionValuedAssociation($fieldName)
214     {
215         return ($fieldName === 'children');
216     }
217
218     public function isIdentifier($fieldName)
219     {
220         return $fieldName === 'id';
221     }
222
223     public function isSingleValuedAssociation($fieldName)
224     {
225         return $fieldName === 'parent';
226     }
227
228     public function getIdentifierValues($entity)
229     {
230
231     }
232
233     public function getIdentifierFieldNames()
234     {
235
236     }
237
238     public function initializeReflection(ReflectionService $reflService)
239     {
240
241     }
242
243     public function wakeupReflection(ReflectionService $reflService)
244     {
245
246     }
247 }