Passed
Push — open-2.x ( 97635c )
by Michael
03:11
created

TestObjectMetadata   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

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

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getTypeOfField() 0 4 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 4 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\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());
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Doctrine\Tests\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

58
        self::assertSame('beberlei', $this->object->/** @scrutinizer ignore-call */ getName());
Loading history...
59
    }
60
61
    public function testSetField()
62
    {
63
        $this->object->setName('test');
0 ignored issues
show
Bug introduced by
The method setName() does not exist on Doctrine\Tests\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

63
        $this->object->/** @scrutinizer ignore-call */ 
64
                       setName('test');
Loading history...
64
        self::assertSame('test', $this->object->getName());
65
    }
66
67
    public function testGetIdentifier()
68
    {
69
        self::assertSame(1, $this->object->getId());
0 ignored issues
show
Bug introduced by
The method getId() does not exist on Doctrine\Tests\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

69
        self::assertSame(1, $this->object->/** @scrutinizer ignore-call */ getId());
Loading history...
70
    }
71
72
    public function testSetIdentifier()
73
    {
74
        $this->expectException(BadMethodCallException::class);
75
        $this->object->setId(2);
0 ignored issues
show
Bug introduced by
The method setId() does not exist on Doctrine\Tests\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

75
        $this->object->/** @scrutinizer ignore-call */ 
76
                       setId(2);
Loading history...
76
    }
77
78
    public function testSetUnknownField()
79
    {
80
        $this->expectException(BadMethodCallException::class);
81
        $this->object->setUnknown('test');
0 ignored issues
show
Bug introduced by
The method setUnknown() does not exist on Doctrine\Tests\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

81
        $this->object->/** @scrutinizer ignore-call */ 
82
                       setUnknown('test');
Loading history...
82
    }
83
84
    public function testGetUnknownField()
85
    {
86
        $this->expectException(BadMethodCallException::class);
87
        $this->object->getUnknown();
0 ignored issues
show
Bug introduced by
The method getUnknown() does not exist on Doctrine\Tests\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

87
        $this->object->/** @scrutinizer ignore-call */ 
88
                       getUnknown();
Loading history...
88
    }
89
90
    public function testUndefinedMethod()
91
    {
92
        $this->expectException(BadMethodCallException::class);
93
        $this->expectExceptionMessage('There is no method');
94
        (new TestObject())->undefinedMethod();
0 ignored issues
show
Bug introduced by
The method undefinedMethod() does not exist on Doctrine\Tests\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

94
        (new TestObject())->/** @scrutinizer ignore-call */ undefinedMethod();
Loading history...
95
    }
96
97
    public function testGetToOneAssociation()
98
    {
99
        self::assertNull($this->object->getParent());
0 ignored issues
show
Bug introduced by
The method getParent() does not exist on Doctrine\Tests\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

99
        self::assertNull($this->object->/** @scrutinizer ignore-call */ getParent());
Loading history...
100
    }
101
102
    public function testSetToOneAssociation()
103
    {
104
        $parent = new TestObject();
105
        $this->object->setParent($parent);
0 ignored issues
show
Bug introduced by
The method setParent() does not exist on Doctrine\Tests\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

105
        $this->object->/** @scrutinizer ignore-call */ 
106
                       setParent($parent);
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method addChildren() does not exist on Doctrine\Tests\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

128
        $this->object->/** @scrutinizer ignore-call */ 
129
                       addChildren($child);
Loading history...
129
130
        self::assertSame($this->object, $child->getParent());
131
        self::assertSame(1, count($this->object->getChildren()));
0 ignored issues
show
Bug introduced by
The method getChildren() does not exist on Doctrine\Tests\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

131
        self::assertSame(1, count($this->object->/** @scrutinizer ignore-call */ getChildren()));
Loading history...
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();
0 ignored issues
show
Bug introduced by
The method asdf() does not exist on Doctrine\Tests\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

157
        $this->object->/** @scrutinizer ignore-call */ 
158
                       asdf();
Loading history...
158
    }
159
160
    public function testAddInvalidCollection()
161
    {
162
        $this->expectException(BadMethodCallException::class);
163
        $this->object->addAsdf(new stdClass());
0 ignored issues
show
Bug introduced by
The method addAsdf() does not exist on Doctrine\Tests\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

163
        $this->object->/** @scrutinizer ignore-call */ 
164
                       addAsdf(new stdClass());
Loading history...
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)
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

264
    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...
265
    {
266
    }
267
268
    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

268
    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...
269
    {
270
    }
271
}
272