Completed
Pull Request — master (#40)
by Marko
445:50 queued 380:43
created

TemplatingCompilerPassTest::testPhpTemplating()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
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\DependencyInjection\Compiler;
13
14
use FOS\CKEditorBundle\DependencyInjection\Compiler\TemplatingCompilerPass;
15
use FOS\CKEditorBundle\Tests\AbstractTestCase;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
18
/**
19
 * @author GeLo <[email protected]>
20
 */
21
class TemplatingCompilerPassTest extends AbstractTestCase
22
{
23
    /**
24
     * @var TemplatingCompilerPass
25
     */
26
    private $compilerPass;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function setUp()
32
    {
33
        $this->compilerPass = new TemplatingCompilerPass();
34
    }
35
36 View Code Duplication
    public function testPhpTemplating()
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...
37
    {
38
        $containerBuilder = $this->createContainerBuilderMock();
39
        $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...
40
            ->expects($this->exactly(2))
41
            ->method('hasDefinition')
42
            ->will($this->returnValueMap([
43
                ['templating.engine.php', false],
44
                ['twig', true],
45
            ]));
46
47
        $containerBuilder
48
            ->expects($this->once())
49
            ->method('removeDefinition')
50
            ->with($this->identicalTo('fosck_editor.templating.helper'));
51
52
        $this->compilerPass->process($containerBuilder);
0 ignored issues
show
Bug introduced by
It seems like $containerBuilder defined by $this->createContainerBuilderMock() on line 38 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\CKEditorBundle\Depen...CompilerPass::process() 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...
53
    }
54
55 View Code Duplication
    public function testTwigTemplating()
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...
56
    {
57
        $containerBuilder = $this->createContainerBuilderMock();
58
        $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...
59
            ->expects($this->exactly(2))
60
            ->method('hasDefinition')
61
            ->will($this->returnValueMap([
62
                ['templating.engine.php', true],
63
                ['twig', false],
64
            ]));
65
66
        $containerBuilder
67
            ->expects($this->once())
68
            ->method('removeDefinition')
69
            ->with($this->identicalTo('fosck_editor.twig_extension'));
70
71
        $this->compilerPass->process($containerBuilder);
0 ignored issues
show
Bug introduced by
It seems like $containerBuilder defined by $this->createContainerBuilderMock() on line 57 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, FOS\CKEditorBundle\Depen...CompilerPass::process() 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...
72
    }
73
74
    /**
75
     * @return ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject
76
     */
77
    private function createContainerBuilderMock()
78
    {
79
        return $this->getMockBuilder(ContainerBuilder::class)
80
            ->disableOriginalConstructor()
81
            ->setMethods(['hasDefinition', 'removeDefinition'])
82
            ->getMock();
83
    }
84
}
85