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