Failed Conditions
Pull Request — master (#1)
by Jonathan
13:22 queued 10:46
created

TestObjectMetadata   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 18
dl 0
loc 89
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A isCollectionValuedAssociation() 0 3 1
A getAssociationMappedByTargetField() 0 4 1
A getIdentifier() 0 3 1
A hasAssociation() 0 3 1
A getAssociationNames() 0 3 1
A isSingleValuedAssociation() 0 3 1
A isAssociationInverseSide() 0 3 1
A initializeReflection() 0 2 1
A getReflectionClass() 0 3 1
A wakeupReflection() 0 2 1
A getName() 0 3 1
A getFieldNames() 0 3 1
A getIdentifierValues() 0 2 1
A hasField() 0 3 1
A getTypeOfField() 0 4 1
A getAssociationTargetClass() 0 3 1
A isIdentifier() 0 3 1
A getIdentifierFieldNames() 0 2 1
1
<?php
2
3
namespace Doctrine\Tests\Common\Persistence;
4
5
use BadMethodCallException;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Doctrine\Common\Persistence\PersistentObject;
8
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
9
use Doctrine\Common\Persistence\Mapping\ReflectionService;
10
use InvalidArgumentException;
11
use RuntimeException;
12
13
/**
14
 * @group DDC-1448
15
 */
16
class PersistentObjectTest extends \Doctrine\Tests\DoctrineTestCase
17
{
18
    private $cm;
19
    private $om;
20
    private $object;
21
22
    public function setUp()
23
    {
24
        $this->cm = new TestObjectMetadata;
25
        $this->om = $this->createMock(ObjectManager::class);
26
        $this->om->expects($this->any())->method('getClassMetadata')
27
                 ->will($this->returnValue($this->cm));
28
        $this->object = new TestObject;
29
        PersistentObject::setObjectManager($this->om);
30
        $this->object->injectObjectManager($this->om, $this->cm);
31
    }
32
33
    public function testGetObjectManager()
34
    {
35
        self::assertSame($this->om, PersistentObject::getObjectManager());
36
    }
37
38
    public function testNonMatchingObjectManager()
39
    {
40
        $this->expectException(RuntimeException::class);
41
        $om = $this->createMock(ObjectManager::class);
42
        $this->object->injectObjectManager($om, $this->cm);
43
    }
44
45
    public function testGetField()
46
    {
47
        self::assertEquals('beberlei', $this->object->getName());
48
    }
49
50
    public function testSetField()
51
    {
52
        $this->object->setName("test");
53
        self::assertEquals("test", $this->object->getName());
54
    }
55
56
    public function testGetIdentifier()
57
    {
58
        self::assertEquals(1, $this->object->getId());
59
    }
60
61
    public function testSetIdentifier()
62
    {
63
        $this->expectException(BadMethodCallException::class);
64
        $this->object->setId(2);
65
    }
66
67
    public function testSetUnknownField()
68
    {
69
        $this->expectException(BadMethodCallException::class);
70
        $this->object->setUnknown("test");
71
    }
72
73
    public function testGetUnknownField()
74
    {
75
        $this->expectException(BadMethodCallException::class);
76
        $this->object->getUnknown();
77
    }
78
79
    public function testUndefinedMethod()
80
    {
81
        $this->expectException(BadMethodCallException::class);
82
        $this->expectExceptionMessage("There is no method");
83
        (new TestObject)->undefinedMethod();
0 ignored issues
show
Bug introduced by
The method undefinedMethod() does not exist on Doctrine\Tests\Common\Persistence\TestObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

83
        (new TestObject)->/** @scrutinizer ignore-call */ undefinedMethod();
Loading history...
84
    }
85
86
    public function testGetToOneAssociation()
87
    {
88
        self::assertNull($this->object->getParent());
89
    }
90
91
    public function testSetToOneAssociation()
92
    {
93
        $parent = new TestObject();
94
        $this->object->setParent($parent);
95
        self::assertSame($parent, $this->object->getParent($parent));
96
    }
97
98
    public function testSetInvalidToOneAssociation()
99
    {
100
        $parent = new \stdClass();
101
102
        $this->expectException(InvalidArgumentException::class);
103
        $this->object->setParent($parent);
104
    }
105
106
    public function testSetToOneAssociationNull()
107
    {
108
        $parent = new TestObject();
109
        $this->object->setParent($parent);
110
        $this->object->setParent(null);
111
        self::assertNull($this->object->getParent());
112
    }
113
114
    public function testAddToManyAssociation()
115
    {
116
        $child = new TestObject();
117
        $this->object->addChildren($child);
118
119
        self::assertSame($this->object, $child->getParent());
0 ignored issues
show
Bug introduced by
The method getParent() does not exist on Doctrine\Tests\Common\Persistence\TestObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

119
        self::assertSame($this->object, $child->/** @scrutinizer ignore-call */ getParent());
Loading history...
120
        self::assertEquals(1, count($this->object->getChildren()));
121
122
        $child = new TestObject();
123
        $this->object->addChildren($child);
124
125
        self::assertEquals(2, count($this->object->getChildren()));
126
    }
127
128
    public function testAddInvalidToManyAssociation()
129
    {
130
        $this->expectException(InvalidArgumentException::class);
131
        $this->object->addChildren(new \stdClass());
132
    }
133
134
    public function testNoObjectManagerSet()
135
    {
136
        PersistentObject::setObjectManager(null);
137
        $child = new TestObject();
138
139
        $this->expectException(RuntimeException::class);
140
        $child->setName("test");
0 ignored issues
show
Bug introduced by
The method setName() does not exist on Doctrine\Tests\Common\Persistence\TestObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

140
        $child->/** @scrutinizer ignore-call */ 
141
                setName("test");
Loading history...
141
    }
142
143
    public function testInvalidMethod()
144
    {
145
        $this->expectException(BadMethodCallException::class);
146
        $this->object->asdf();
147
    }
148
149
    public function testAddInvalidCollection()
150
    {
151
        $this->expectException(BadMethodCallException::class);
152
        $this->object->addAsdf(new \stdClass());
153
    }
154
}
155
156
class TestObject extends PersistentObject
157
{
158
    protected $id   = 1;
159
    protected $name = 'beberlei';
160
    protected $parent;
161
    protected $children;
162
}
163
164
class TestObjectMetadata implements ClassMetadata
165
{
166
167
    public function getAssociationMappedByTargetField($assocName)
168
    {
169
        $assoc = ['children' => 'parent'];
170
        return $assoc[$assocName];
171
    }
172
173
    public function getAssociationNames()
174
    {
175
        return ['parent', 'children'];
176
    }
177
178
    public function getAssociationTargetClass($assocName)
179
    {
180
        return __NAMESPACE__ . '\TestObject';
181
    }
182
183
    public function getFieldNames()
184
    {
185
        return ['id', 'name'];
186
    }
187
188
    public function getIdentifier()
189
    {
190
        return ['id'];
191
    }
192
193
    public function getName()
194
    {
195
        return __NAMESPACE__ . '\TestObject';
196
    }
197
198
    public function getReflectionClass()
199
    {
200
        return new \ReflectionClass($this->getName());
201
    }
202
203
    public function getTypeOfField($fieldName)
204
    {
205
        $types = ['id' => 'integer', 'name' => 'string'];
206
        return $types[$fieldName];
207
    }
208
209
    public function hasAssociation($fieldName)
210
    {
211
        return in_array($fieldName, ['parent', 'children']);
212
    }
213
214
    public function hasField($fieldName)
215
    {
216
        return in_array($fieldName, ['id', 'name']);
217
    }
218
219
    public function isAssociationInverseSide($assocName)
220
    {
221
        return ($assocName === 'children');
222
    }
223
224
    public function isCollectionValuedAssociation($fieldName)
225
    {
226
        return ($fieldName === 'children');
227
    }
228
229
    public function isIdentifier($fieldName)
230
    {
231
        return $fieldName === 'id';
232
    }
233
234
    public function isSingleValuedAssociation($fieldName)
235
    {
236
        return $fieldName === 'parent';
237
    }
238
239
    public function getIdentifierValues($entity)
240
    {
241
    }
242
243
    public function getIdentifierFieldNames()
244
    {
245
    }
246
247
    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

247
    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...
248
    {
249
    }
250
251
    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

251
    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...
252
    {
253
    }
254
}
255