Completed
Pull Request — master (#40)
by Marko
116:46 queued 51:48
created

FOSCKEditorBundleTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 49
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testBundle() 0 4 1
A testCompilerPasses() 0 17 1
A createContainerBuilderMock() 0 7 1
1
<?php
2
3
/*
4
 * This file is part of the Ivory CKEditor 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 FOS\CKEditorBundle\Tests;
13
14
use FOS\CKEditorBundle\DependencyInjection\Compiler\ResourceCompilerPass;
15
use FOS\CKEditorBundle\DependencyInjection\Compiler\TemplatingCompilerPass;
16
use FOS\CKEditorBundle\FOSCKEditorBundle;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\HttpKernel\Bundle\Bundle;
19
20
/**
21
 * @author GeLo <[email protected]>
22
 * @author Adam Misiorny <[email protected]>
23
 */
24
class FOSCKEditorBundleTest extends AbstractTestCase
25
{
26
    /**
27
     * @var FOSCKEditorBundle
28
     */
29
    private $bundle;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected function setUp()
35
    {
36
        $this->bundle = new FOSCKEditorBundle();
37
    }
38
39
    public function testBundle()
40
    {
41
        $this->assertInstanceOf(Bundle::class, $this->bundle);
42
    }
43
44
    public function testCompilerPasses()
45
    {
46
        $containerBuilder = $this->createContainerBuilderMock();
47
        $containerBuilder
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...
48
            ->expects($this->at(0))
49
            ->method('addCompilerPass')
50
            ->with($this->isInstanceOf(ResourceCompilerPass::class))
51
            ->will($this->returnSelf());
52
53
        $containerBuilder
54
            ->expects($this->at(1))
55
            ->method('addCompilerPass')
56
            ->with($this->isInstanceOf(TemplatingCompilerPass::class))
57
            ->will($this->returnSelf());
58
59
        $this->bundle->build($containerBuilder);
0 ignored issues
show
Bug introduced by
It seems like $containerBuilder defined by $this->createContainerBuilderMock() on line 46 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\CKEditorBundle\FOSCKEditorBundle::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...
60
    }
61
62
    /**
63
     * @return ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject
64
     */
65
    private function createContainerBuilderMock()
66
    {
67
        return $this->getMockBuilder(ContainerBuilder::class)
68
            ->disableOriginalConstructor()
69
            ->setMethods(['addCompilerPass'])
70
            ->getMock();
71
    }
72
}
73