Completed
Push — master ( 8be43b...f61101 )
by Eric
9s
created

ResourceSubscriberTest::createMappingExceptionMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Bundle\ResourceBundle\Tests\EventSubscriber\Doctrine\ORM;
13
14
use Doctrine\Common\EventSubscriber;
15
use Doctrine\Common\Persistence\ObjectManager;
16
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
17
use Doctrine\ORM\Events;
18
use Doctrine\ORM\Mapping\ClassMetadata;
19
use Doctrine\ORM\Mapping\MappingException;
20
use Lug\Bundle\ResourceBundle\EventSubscriber\Doctrine\ORM\ResourceSubscriber;
21
use Lug\Component\Registry\Model\RegistryInterface;
22
use Lug\Component\Resource\Model\ResourceInterface;
23
24
/**
25
 * @author GeLo <[email protected]>
26
 */
27
class ResourceSubscriberTest extends \PHPUnit_Framework_TestCase
28
{
29
    /**
30
     * @var ResourceSubscriber
31
     */
32
    private $resourceSubscriber;
33
34
    /**
35
     * @var \PHPUnit_Framework_MockObject_MockObject|RegistryInterface
36
     */
37
    private $serviceRegistry;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function setUp()
43
    {
44
        $this->serviceRegistry = $this->createServiceRegistryMock();
45
        $this->resourceSubscriber = new ResourceSubscriber($this->serviceRegistry);
46
    }
47
48
    public function testInheritance()
49
    {
50
        $this->assertInstanceOf(EventSubscriber::class, $this->resourceSubscriber);
51
    }
52
53
    public function testSubscribedEvents()
54
    {
55
        $this->assertSame([Events::loadClassMetadata], $this->resourceSubscriber->getSubscribedEvents());
56
    }
57
58 View Code Duplication
    public function testLoadClassMetadataWithResourceAndNoParentClasses()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        $event = $this->createLoadClassMetadataEventArgsMock();
61
        $event
62
            ->expects($this->once())
63
            ->method('getClassMetadata')
64
            ->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
65
66
        $classMetadata
67
            ->expects($this->once())
68
            ->method('getName')
69
            ->will($this->returnValue($model = 'model'));
70
71
        $this->serviceRegistry
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Registry\Model\RegistryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
72
            ->expects($this->once())
73
            ->method('getIterator')
74
            ->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()])));
75
76
        $resource
77
            ->expects($this->once())
78
            ->method('getModel')
79
            ->will($this->returnValue($model));
80
81
        $resource
82
            ->expects($this->once())
83
            ->method('getRepository')
84
            ->will($this->returnValue($repository = 'repository'));
85
86
        $classMetadata
87
            ->expects($this->once())
88
            ->method('setCustomRepositoryClass')
89
            ->with($this->identicalTo($repository));
90
91
        $classMetadata->parentClasses = [];
0 ignored issues
show
Bug introduced by
Accessing parentClasses on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
92
        $classMetadata->isMappedSuperclass = false;
0 ignored issues
show
Bug introduced by
Accessing isMappedSuperclass on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
93
94
        $this->resourceSubscriber->loadClassMetadata($event);
95
96
        $this->assertFalse($classMetadata->isMappedSuperclass);
0 ignored issues
show
Bug introduced by
Accessing isMappedSuperclass on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
97
    }
98
99 View Code Duplication
    public function testLoadClassMetadataWithResourceAndParentClassesWithoutInheritance()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $event = $this->createLoadClassMetadataEventArgsMock();
102
        $event
103
            ->expects($this->once())
104
            ->method('getClassMetadata')
105
            ->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
106
107
        $classMetadata
108
            ->expects($this->once())
109
            ->method('getName')
110
            ->will($this->returnValue($model = 'model'));
111
112
        $this->serviceRegistry
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Registry\Model\RegistryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
113
            ->expects($this->once())
114
            ->method('getIterator')
115
            ->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()])));
116
117
        $resource
118
            ->expects($this->once())
119
            ->method('getModel')
120
            ->will($this->returnValue($model));
121
122
        $classMetadata->parentClasses = [$parentClass = 'ParentClass'];
0 ignored issues
show
Bug introduced by
Accessing parentClasses on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
123
124
        $event
125
            ->expects($this->once())
126
            ->method('getObjectManager')
127
            ->will($this->returnValue($objectManager = $this->createObjectManagerMock()));
128
129
        $objectManager
130
            ->expects($this->once())
131
            ->method('getClassMetadata')
132
            ->with($this->identicalTo($parentClass))
133
            ->will($this->returnValue($parentMetadata = $this->createClassMetadataMock()));
134
135
        $parentMetadata->isMappedSuperclass = false;
0 ignored issues
show
Bug introduced by
Accessing isMappedSuperclass on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
136
        $parentMetadata->inheritanceType = ClassMetadata::INHERITANCE_TYPE_NONE;
0 ignored issues
show
Bug introduced by
Accessing inheritanceType on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
137
138
        $resource
139
            ->expects($this->once())
140
            ->method('getRepository')
141
            ->will($this->returnValue($repository = 'repository'));
142
143
        $classMetadata
144
            ->expects($this->once())
145
            ->method('setCustomRepositoryClass')
146
            ->with($this->identicalTo($repository));
147
148
        $this->resourceSubscriber->loadClassMetadata($event);
149
150
        $this->assertFalse($classMetadata->isMappedSuperclass);
0 ignored issues
show
Bug introduced by
Accessing isMappedSuperclass on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
151
        $this->assertTrue($parentMetadata->isMappedSuperclass);
0 ignored issues
show
Bug introduced by
Accessing isMappedSuperclass on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
152
    }
153
154 View Code Duplication
    public function testLoadClassMetadataWithResourceAndParentClassesWithInheritance()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
    {
156
        $event = $this->createLoadClassMetadataEventArgsMock();
157
        $event
158
            ->expects($this->once())
159
            ->method('getClassMetadata')
160
            ->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
161
162
        $classMetadata
163
            ->expects($this->once())
164
            ->method('getName')
165
            ->will($this->returnValue($model = 'model'));
166
167
        $this->serviceRegistry
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Registry\Model\RegistryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
168
            ->expects($this->once())
169
            ->method('getIterator')
170
            ->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()])));
171
172
        $resource
173
            ->expects($this->once())
174
            ->method('getModel')
175
            ->will($this->returnValue($model));
176
177
        $classMetadata->parentClasses = [$parentClass = 'ParentClass'];
0 ignored issues
show
Bug introduced by
Accessing parentClasses on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
178
179
        $event
180
            ->expects($this->once())
181
            ->method('getObjectManager')
182
            ->will($this->returnValue($objectManager = $this->createObjectManagerMock()));
183
184
        $objectManager
185
            ->expects($this->once())
186
            ->method('getClassMetadata')
187
            ->with($this->identicalTo($parentClass))
188
            ->will($this->returnValue($parentMetadata = $this->createClassMetadataMock()));
189
190
        $parentMetadata->isMappedSuperclass = false;
0 ignored issues
show
Bug introduced by
Accessing isMappedSuperclass on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
191
        $parentMetadata->inheritanceType = ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE;
0 ignored issues
show
Bug introduced by
Accessing inheritanceType on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
192
193
        $resource
194
            ->expects($this->once())
195
            ->method('getRepository')
196
            ->will($this->returnValue($repository = 'repository'));
197
198
        $classMetadata
199
            ->expects($this->once())
200
            ->method('setCustomRepositoryClass')
201
            ->with($this->identicalTo($repository));
202
203
        $this->resourceSubscriber->loadClassMetadata($event);
204
205
        $this->assertFalse($classMetadata->isMappedSuperclass);
0 ignored issues
show
Bug introduced by
Accessing isMappedSuperclass on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
206
        $this->assertFalse($parentMetadata->isMappedSuperclass);
0 ignored issues
show
Bug introduced by
Accessing isMappedSuperclass on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
207
    }
208
209
    public function testLoadClassMetadataWithResourceAndParentClassesWithoutClassMetadata()
210
    {
211
        $event = $this->createLoadClassMetadataEventArgsMock();
212
        $event
213
            ->expects($this->once())
214
            ->method('getClassMetadata')
215
            ->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
216
217
        $classMetadata
218
            ->expects($this->once())
219
            ->method('getName')
220
            ->will($this->returnValue($model = 'model'));
221
222
        $this->serviceRegistry
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Registry\Model\RegistryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
223
            ->expects($this->once())
224
            ->method('getIterator')
225
            ->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()])));
226
227
        $resource
228
            ->expects($this->once())
229
            ->method('getModel')
230
            ->will($this->returnValue($model));
231
232
        $classMetadata->parentClasses = [$parentClass = 'ParentClass'];
0 ignored issues
show
Bug introduced by
Accessing parentClasses on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
233
234
        $event
235
            ->expects($this->once())
236
            ->method('getObjectManager')
237
            ->will($this->returnValue($objectManager = $this->createObjectManagerMock()));
238
239
        $objectManager
240
            ->expects($this->once())
241
            ->method('getClassMetadata')
242
            ->with($this->identicalTo($parentClass))
243
            ->will($this->throwException(new MappingException()));
244
245
        $resource
246
            ->expects($this->once())
247
            ->method('getRepository')
248
            ->will($this->returnValue($repository = 'repository'));
249
250
        $classMetadata
251
            ->expects($this->once())
252
            ->method('setCustomRepositoryClass')
253
            ->with($this->identicalTo($repository));
254
255
        $this->resourceSubscriber->loadClassMetadata($event);
256
257
        $this->assertFalse($classMetadata->isMappedSuperclass);
0 ignored issues
show
Bug introduced by
Accessing isMappedSuperclass on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
258
    }
259
260 View Code Duplication
    public function testLoadClassMetadataWithoutResource()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
261
    {
262
        $event = $this->createLoadClassMetadataEventArgsMock();
263
        $event
264
            ->expects($this->once())
265
            ->method('getClassMetadata')
266
            ->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
267
268
        $classMetadata
269
            ->expects($this->once())
270
            ->method('getName')
271
            ->will($this->returnValue($model = 'model'));
272
273
        $this->serviceRegistry
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Registry\Model\RegistryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
274
            ->expects($this->once())
275
            ->method('getIterator')
276
            ->will($this->returnValue(new \ArrayIterator([])));
277
278
        $classMetadata
279
            ->expects($this->never())
280
            ->method('setCustomRepositoryClass');
281
282
        $classMetadata->isMappedSuperclass = true;
0 ignored issues
show
Bug introduced by
Accessing isMappedSuperclass on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
283
284
        $this->resourceSubscriber->loadClassMetadata($event);
285
286
        $this->assertTrue($classMetadata->isMappedSuperclass);
0 ignored issues
show
Bug introduced by
Accessing isMappedSuperclass on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
287
    }
288
289
    /**
290
     * @return \PHPUnit_Framework_MockObject_MockObject|RegistryInterface
291
     */
292
    private function createServiceRegistryMock()
293
    {
294
        return $this->createMock(RegistryInterface::class);
295
    }
296
297
    /**
298
     * @return \PHPUnit_Framework_MockObject_MockObject|LoadClassMetadataEventArgs
299
     */
300
    private function createLoadClassMetadataEventArgsMock()
301
    {
302
        return $this->createMock(LoadClassMetadataEventArgs::class);
303
    }
304
305
    /**
306
     * @return \PHPUnit_Framework_MockObject_MockObject|ObjectManager
307
     */
308
    private function createObjectManagerMock()
309
    {
310
        return $this->createMock(ObjectManager::class);
311
    }
312
313
    /**
314
     * @return \PHPUnit_Framework_MockObject_MockObject|ClassMetadata
315
     */
316
    private function createClassMetadataMock()
317
    {
318
        return $this->createMock(ClassMetadata::class);
319
    }
320
321
    /**
322
     * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
323
     */
324
    private function createResourceMock()
325
    {
326
        return $this->createMock(ResourceInterface::class);
327
    }
328
}
329