Completed
Push — master ( 7d206d...883407 )
by Eric
8s
created

LugResourceBundleTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
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;
13
14
use Lug\Bundle\ResourceBundle\DependencyInjection\Compiler\ConfigureResolveTargetEntitySubscriberPass;
15
use Lug\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterDomainManagerPass;
16
use Lug\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterDriverMappingPass;
17
use Lug\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterFactoryPass;
18
use Lug\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterFlashListenerPass;
19
use Lug\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterManagerPass;
20
use Lug\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterManagerTagPass;
21
use Lug\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterMessageListenerPass;
22
use Lug\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterRepositoryPass;
23
use Lug\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterResourcePass;
24
use Lug\Bundle\ResourceBundle\DependencyInjection\Compiler\ReplaceBase64FileExtensionPass;
25
use Lug\Bundle\ResourceBundle\DependencyInjection\Compiler\ReplaceBooleanExtensionPass;
26
use Lug\Bundle\ResourceBundle\LugResourceBundle;
27
use Symfony\Component\DependencyInjection\ContainerBuilder;
28
use Symfony\Component\HttpKernel\Bundle\Bundle;
29
30
/**
31
 * @author GeLo <[email protected]>
32
 */
33
class LugResourceBundleTest extends \PHPUnit_Framework_TestCase
34
{
35
    /**
36
     * @var LugResourceBundle
37
     */
38
    private $bundle;
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected function setUp()
44
    {
45
        $this->bundle = new LugResourceBundle();
46
    }
47
48
    public function testInheritance()
49
    {
50
        $this->assertInstanceOf(Bundle::class, $this->bundle);
51
    }
52
53
    public function testBuild()
54
    {
55
        $container = $this->createContainerBuilderMock();
56
        $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...ection\ContainerBuilder.

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...
57
            ->expects($this->at(0))
58
            ->method('addCompilerPass')
59
            ->with($this->isInstanceOf(RegisterResourcePass::class))
60
            ->will($this->returnSelf());
61
62
        $container
63
            ->expects($this->at(1))
64
            ->method('addCompilerPass')
65
            ->with($this->isInstanceOf(RegisterDriverMappingPass::class))
66
            ->will($this->returnSelf());
67
68
        $container
69
            ->expects($this->at(2))
70
            ->method('addCompilerPass')
71
            ->with($this->isInstanceOf(RegisterFactoryPass::class))
72
            ->will($this->returnSelf());
73
74
        $container
75
            ->expects($this->at(3))
76
            ->method('addCompilerPass')
77
            ->with($this->isInstanceOf(RegisterManagerTagPass::class))
78
            ->will($this->returnSelf());
79
80
        $container
81
            ->expects($this->at(4))
82
            ->method('addCompilerPass')
83
            ->with($this->isInstanceOf(RegisterManagerPass::class))
84
            ->will($this->returnSelf());
85
86
        $container
87
            ->expects($this->at(5))
88
            ->method('addCompilerPass')
89
            ->with($this->isInstanceOf(RegisterRepositoryPass::class))
90
            ->will($this->returnSelf());
91
92
        $container
93
            ->expects($this->at(6))
94
            ->method('addCompilerPass')
95
            ->with($this->isInstanceOf(RegisterDomainManagerPass::class))
96
            ->will($this->returnSelf());
97
98
        $container
99
            ->expects($this->at(7))
100
            ->method('addCompilerPass')
101
            ->with($this->isInstanceOf(ConfigureResolveTargetEntitySubscriberPass::class))
102
            ->will($this->returnSelf());
103
104
        $container
105
            ->expects($this->at(8))
106
            ->method('addCompilerPass')
107
            ->with($this->isInstanceOf(RegisterFlashListenerPass::class))
108
            ->will($this->returnSelf());
109
110
        $container
111
            ->expects($this->at(9))
112
            ->method('addCompilerPass')
113
            ->with($this->isInstanceOf(RegisterMessageListenerPass::class))
114
            ->will($this->returnSelf());
115
116
        $container
117
            ->expects($this->at(10))
118
            ->method('addCompilerPass')
119
            ->with($this->isInstanceOf(ReplaceBase64FileExtensionPass::class))
120
            ->will($this->returnSelf());
121
122
        $container
123
            ->expects($this->at(11))
124
            ->method('addCompilerPass')
125
            ->with($this->isInstanceOf(ReplaceBooleanExtensionPass::class))
126
            ->will($this->returnSelf());
127
128
        $this->bundle->build($container);
0 ignored issues
show
Bug introduced by
It seems like $container defined by $this->createContainerBuilderMock() on line 55 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...ResourceBundle::build() does only seem to accept object<Symfony\Component...ction\ContainerBuilder>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
129
    }
130
131
    /**
132
     * @return \PHPUnit_Framework_MockObject_MockObject|ContainerBuilder
133
     */
134
    private function createContainerBuilderMock()
135
    {
136
        return $this->getMock(ContainerBuilder::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
137
    }
138
}
139