Completed
Pull Request — 1.4.x (#106)
by Grégoire
02:25
created

TestObjectMetadata   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 17
dl 0
loc 90
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getTypeOfField() 0 5 1
A wakeupReflection() 0 2 1
A getAssociationNames() 0 3 1
A hasField() 0 3 1
A getName() 0 3 1
A getIdentifier() 0 3 1
A isSingleValuedAssociation() 0 3 1
A initializeReflection() 0 2 1
A isAssociationInverseSide() 0 3 1
A hasAssociation() 0 3 1
A getIdentifierFieldNames() 0 2 1
A getFieldNames() 0 3 1
A getAssociationMappedByTargetField() 0 5 1
A getReflectionClass() 0 3 1
A isCollectionValuedAssociation() 0 3 1
A isIdentifier() 0 3 1
A getIdentifierValues() 0 2 1
A getAssociationTargetClass() 0 3 1
1
<?php
2
3
namespace Doctrine\Tests\Persistence;
4
5
use BadMethodCallException;
6
use Doctrine\Common\Persistence\PersistentObject;
7
use Doctrine\Persistence\Mapping\ClassMetadata;
8
use Doctrine\Persistence\Mapping\ReflectionService;
9
use Doctrine\Persistence\ObjectManager;
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 TestObjectMetadata implements ClassMetadata
168
{
169
    public function getAssociationMappedByTargetField($assocName)
170
    {
171
        $assoc = ['children' => 'parent'];
172
173
        return $assoc[$assocName];
174
    }
175
176
    public function getAssociationNames()
177
    {
178
        return ['parent', 'children'];
179
    }
180
181
    public function getAssociationTargetClass($assocName)
182
    {
183
        return __NAMESPACE__ . '\TestObject';
184
    }
185
186
    public function getFieldNames()
187
    {
188
        return ['id', 'name'];
189
    }
190
191
    public function getIdentifier()
192
    {
193
        return ['id'];
194
    }
195
196
    public function getName()
197
    {
198
        return __NAMESPACE__ . '\TestObject';
199
    }
200
201
    public function getReflectionClass()
202
    {
203
        return new ReflectionClass($this->getName());
204
    }
205
206
    public function getTypeOfField($fieldName)
207
    {
208
        $types = ['id' => 'integer', 'name' => 'string'];
209
210
        return $types[$fieldName];
211
    }
212
213
    public function hasAssociation($fieldName)
214
    {
215
        return in_array($fieldName, ['parent', 'children']);
216
    }
217
218
    public function hasField($fieldName)
219
    {
220
        return in_array($fieldName, ['id', 'name']);
221
    }
222
223
    public function isAssociationInverseSide($assocName)
224
    {
225
        return $assocName === 'children';
226
    }
227
228
    public function isCollectionValuedAssociation($fieldName)
229
    {
230
        return $fieldName === 'children';
231
    }
232
233
    public function isIdentifier($fieldName)
234
    {
235
        return $fieldName === 'id';
236
    }
237
238
    public function isSingleValuedAssociation($fieldName)
239
    {
240
        return $fieldName === 'parent';
241
    }
242
243
    public function getIdentifierValues($entity)
244
    {
245
    }
246
247
    public function getIdentifierFieldNames()
248
    {
249
    }
250
251
    public function initializeReflection(ReflectionService $reflService)
0 ignored issues
show
Unused Code introduced by
The parameter $reflService is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

251
    public function initializeReflection(/** @scrutinizer ignore-unused */ ReflectionService $reflService)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
252
    {
253
    }
254
255
    public function wakeupReflection(ReflectionService $reflService)
0 ignored issues
show
Unused Code introduced by
The parameter $reflService is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

255
    public function wakeupReflection(/** @scrutinizer ignore-unused */ ReflectionService $reflService)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
256
    {
257
    }
258
}
259