Completed
Pull Request — master (#951)
by David
05:33 queued 02:50
created

testRegisterMissingServiceAndResolve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\Tests\Mapping;
4
5
use Doctrine\Bundle\DoctrineBundle\Mapping\ContainerEntityListenerResolver;
6
use PHPUnit\Framework\TestCase;
7
use PHPUnit_Framework_MockObject_MockObject;
8
use Psr\Container\ContainerInterface;
9
10
class ContainerEntityListenerResolverTest extends TestCase
11
{
12
    /** @var ContainerEntityListenerResolver */
13
    private $resolver;
14
15
    /** @var ContainerInterface|PHPUnit_Framework_MockObject_MockObject */
16
    private $container;
17
18
    protected function setUp()
19
    {
20
        parent::setUp();
21
22
        $this->container = $this->createMock(ContainerInterface::class);
23
        $this->resolver  = new ContainerEntityListenerResolver($this->container);
0 ignored issues
show
Documentation introduced by
$this->container is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Container\ContainerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
24
    }
25
26 View Code Duplication
    public function testResolveClass()
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...
27
    {
28
        $className = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1';
29
        $object    = $this->resolver->resolve($className);
30
31
        $this->assertInstanceOf($className, $object);
32
        $this->assertSame($object, $this->resolver->resolve($className));
33
    }
34
35 View Code Duplication
    public function testRegisterClassAndResolve()
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...
36
    {
37
        $className = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1';
38
        $object    = new $className();
39
40
        $this->resolver->register($object);
41
42
        $this->assertSame($object, $this->resolver->resolve($className));
43
    }
44
45
    public function testRegisterServiceAndResolve()
46
    {
47
        $className = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1';
48
        $serviceId = 'app.entity_listener';
49
        $object    = new $className();
50
51
        $this->resolver->registerService($className, $serviceId);
52
        $this->container
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Psr\Container\ContainerInterface.

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...
53
            ->expects($this->any())
54
            ->method('has')
55
            ->with($serviceId)
56
            ->will($this->returnValue(true));
57
        $this->container
58
            ->expects($this->any())
59
            ->method('get')
60
            ->with($serviceId)
61
            ->will($this->returnValue($object));
62
63
        $this->assertInstanceOf($className, $this->resolver->resolve($className));
64
        $this->assertSame($object, $this->resolver->resolve($className));
65
    }
66
67
    /**
68
     * @expectedException \RuntimeException
69
     * @expectedExceptionMessage There is no service named
70
     */
71
    public function testRegisterMissingServiceAndResolve()
72
    {
73
        $className = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1';
74
        $serviceId = 'app.entity_listener';
75
76
        $this->resolver->registerService($className, $serviceId);
77
        $this->container
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Psr\Container\ContainerInterface.

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...
78
            ->expects($this->any())
79
            ->method('has')
80
            ->with($serviceId)
81
            ->will($this->returnValue(false));
82
83
        $this->resolver->resolve($className);
84
    }
85
86 View Code Duplication
    public function testClearOne()
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...
87
    {
88
        $className1 = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1';
89
        $className2 = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener2';
90
91
        $obj1 = $this->resolver->resolve($className1);
92
        $obj2 = $this->resolver->resolve($className2);
93
94
        $this->assertInstanceOf($className1, $obj1);
95
        $this->assertInstanceOf($className2, $obj2);
96
97
        $this->assertSame($obj1, $this->resolver->resolve($className1));
98
        $this->assertSame($obj2, $this->resolver->resolve($className2));
99
100
        $this->resolver->clear($className1);
101
102
        $this->assertInstanceOf($className1, $this->resolver->resolve($className1));
103
        $this->assertInstanceOf($className2, $this->resolver->resolve($className2));
104
105
        $this->assertNotSame($obj1, $this->resolver->resolve($className1));
106
        $this->assertSame($obj2, $this->resolver->resolve($className2));
107
    }
108
109 View Code Duplication
    public function testClearAll()
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...
110
    {
111
        $className1 = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1';
112
        $className2 = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener2';
113
114
        $obj1 = $this->resolver->resolve($className1);
115
        $obj2 = $this->resolver->resolve($className2);
116
117
        $this->assertInstanceOf($className1, $obj1);
118
        $this->assertInstanceOf($className2, $obj2);
119
120
        $this->assertSame($obj1, $this->resolver->resolve($className1));
121
        $this->assertSame($obj2, $this->resolver->resolve($className2));
122
123
        $this->resolver->clear();
124
125
        $this->assertInstanceOf($className1, $this->resolver->resolve($className1));
126
        $this->assertInstanceOf($className2, $this->resolver->resolve($className2));
127
128
        $this->assertNotSame($obj1, $this->resolver->resolve($className1));
129
        $this->assertNotSame($obj2, $this->resolver->resolve($className2));
130
    }
131
132
    /**
133
     * @expectedException \InvalidArgumentException
134
     * @expectedExceptionMessage An object was expected, but got "string".
135
     */
136
    public function testRegisterStringException()
137
    {
138
        $this->resolver->register('CompanyContractListener');
0 ignored issues
show
Documentation introduced by
'CompanyContractListener' is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
139
    }
140
}
141
142
class EntityListener1
143
{
144
}
145
146
class EntityListener2
147
{
148
}
149