Completed
Pull Request — master (#443)
by Peter
05:01
created

testRegisterServiceAndResolve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 23
rs 9.0856
c 1
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\Tests\Mapping;
4
5
use Doctrine\Bundle\DoctrineBundle\Mapping\ContainerAwareEntityListenerResolver;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
8
class ContainerAwareEntityListenerResolverTest extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @var ContainerAwareEntityListenerResolver
12
     */
13
    private $resolver;
14
15
    /**
16
     * @var ContainerInterface|\PHPUnit_Framework_MockObject_MockObject
17
     */
18
    private $container;
19
20
    protected function setUp()
21
    {
22
        if (!interface_exists('\Doctrine\ORM\Mapping\EntityListenerResolver')) {
23
            $this->markTestSkipped('Entity listeners are not supported in this Doctrine version');
24
        }
25
26
        parent::setUp();
27
28
        $this->container = $this->getMockForAbstractClass('\Symfony\Component\DependencyInjection\ContainerInterface');
29
        $this->resolver  = new ContainerAwareEntityListenerResolver($this->container);
30
    }
31
32 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...
33
    {
34
        $className  = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1';
35
        $object     = $this->resolver->resolve($className);
36
37
        $this->assertInstanceOf($className, $object);
38
        $this->assertSame($object, $this->resolver->resolve($className));
39
    }
40
41 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...
42
    {
43
        $className  = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1';
44
        $object     = new $className();
45
46
        $this->resolver->register($object);
47
48
        $this->assertSame($object, $this->resolver->resolve($className));
49
    }
50
51
    public function testRegisterServiceAndResolve()
52
    {
53
        $className  = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1';
54
        $serviceId  = 'app.entity_listener';
55
        $object     = new $className();
56
57
        $this->resolver->registerService($className, $serviceId);
58
        $this->container
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Depend...tion\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...
59
            ->expects($this->any())
60
            ->method('has')
61
            ->with($serviceId)
62
            ->will($this->returnValue(true))
63
        ;
64
        $this->container
65
            ->expects($this->any())
66
            ->method('get')
67
            ->with($serviceId)
68
            ->will($this->returnValue($object))
69
        ;
70
71
        $this->assertInstanceOf($className, $this->resolver->resolve($className));
72
        $this->assertSame($object, $this->resolver->resolve($className));
73
    }
74
75
    /**
76
     * @expectedException \RuntimeException
77
     * @expectedExceptionMessage There is no service named
78
     */
79
    public function testRegisterMissingServiceAndResolve()
80
    {
81
        $className  = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1';
82
        $serviceId  = 'app.entity_listener';
83
84
        $this->resolver->registerService($className, $serviceId);
85
        $this->container
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Depend...tion\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...
86
            ->expects($this->any())
87
            ->method('has')
88
            ->with($serviceId)
89
            ->will($this->returnValue(false))
90
        ;
91
92
        $this->resolver->resolve($className);
93
    }
94
95 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...
96
    {
97
        $className1  = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1';
98
        $className2  = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener2';
99
100
        $obj1 = $this->resolver->resolve($className1);
101
        $obj2 = $this->resolver->resolve($className2);
102
103
        $this->assertInstanceOf($className1, $obj1);
104
        $this->assertInstanceOf($className2, $obj2);
105
106
        $this->assertSame($obj1, $this->resolver->resolve($className1));
107
        $this->assertSame($obj2, $this->resolver->resolve($className2));
108
109
        $this->resolver->clear($className1);
110
111
        $this->assertInstanceOf($className1, $this->resolver->resolve($className1));
112
        $this->assertInstanceOf($className2, $this->resolver->resolve($className2));
113
114
        $this->assertNotSame($obj1, $this->resolver->resolve($className1));
115
        $this->assertSame($obj2, $this->resolver->resolve($className2));
116
    }
117
118 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...
119
    {
120
        $className1  = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener1';
121
        $className2  = '\Doctrine\Bundle\DoctrineBundle\Tests\Mapping\EntityListener2';
122
123
        $obj1 = $this->resolver->resolve($className1);
124
        $obj2 = $this->resolver->resolve($className2);
125
126
        $this->assertInstanceOf($className1, $obj1);
127
        $this->assertInstanceOf($className2, $obj2);
128
129
        $this->assertSame($obj1, $this->resolver->resolve($className1));
130
        $this->assertSame($obj2, $this->resolver->resolve($className2));
131
132
        $this->resolver->clear();
133
134
        $this->assertInstanceOf($className1, $this->resolver->resolve($className1));
135
        $this->assertInstanceOf($className2, $this->resolver->resolve($className2));
136
137
        $this->assertNotSame($obj1, $this->resolver->resolve($className1));
138
        $this->assertNotSame($obj2, $this->resolver->resolve($className2));
139
    }
140
141
    /**
142
     * @expectedException \InvalidArgumentException
143
     * @expectedExceptionMessage An object was expected, but got "string".
144
     */
145
    public function testRegisterStringException()
146
    {
147
        $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...
148
    }
149
}
150
151
class EntityListener1
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
152
{
153
}
154
155
class EntityListener2
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
156
{
157
}
158