Completed
Pull Request — master (#35)
by Eric
28:24 queued 21:05
created

ReplaceBase64FileExtensionPassTest::testProcess()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 25
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 25
loc 25
rs 8.8571
cc 2
eloc 19
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\DependencyInjection\Compiler;
13
14
use Lug\Bundle\ResourceBundle\DependencyInjection\Compiler\ReplaceBase64FileExtensionPass;
15
use Lug\Bundle\ResourceBundle\Form\Extension\Base64FileExtension;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Reference;
19
20
/**
21
 * @author GeLo <[email protected]>
22
 */
23
class ReplaceBase64FileExtensionPassTest extends \PHPUnit_Framework_TestCase
24
{
25
    /**
26
     * @var ReplaceBase64FileExtensionPass
27
     */
28
    private $compiler;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected function setUp()
34
    {
35
        $this->compiler = new ReplaceBase64FileExtensionPass();
36
    }
37
38
    public function testProcessWithService()
39
    {
40
        $container = $this->createContainerBuilderMock();
41
        $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...
42
            ->expects($this->once())
43
            ->method('hasDefinition')
44
            ->with($this->identicalTo($service = 'ivory.base64_file.form.extension'))
45
            ->will($this->returnValue(true));
46
47
        $container
48
            ->expects($this->once())
49
            ->method('getDefinition')
50
            ->with($this->identicalTo($service))
51
            ->will($this->returnValue($definition = $this->createDefinitionMock()));
52
53
        $definition
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\DependencyInjection\Definition.

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...
54
            ->expects($this->once())
55
            ->method('setClass')
56
            ->with($this->identicalTo(Base64FileExtension::class))
57
            ->will($this->returnSelf());
58
59
        $definition
60
            ->expects($this->once())
61
            ->method('addArgument')
62
            ->with($this->callback(function ($reference) {
63
                return $reference instanceof Reference
64
                    && (string) $reference === 'lug.resource.routing.parameter_resolver';
65
            }));
66
67
        $this->compiler->process($container);
0 ignored issues
show
Bug introduced by
It seems like $container defined by $this->createContainerBuilderMock() on line 40 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...xtensionPass::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...
68
    }
69
70
    public function testProcessWithoutService()
71
    {
72
        $container = $this->createContainerBuilderMock();
73
        $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...
74
            ->expects($this->once())
75
            ->method('hasDefinition')
76
            ->with($this->identicalTo($service = 'ivory.base64_file.form.extension'))
77
            ->will($this->returnValue(false));
78
79
        $container
80
            ->expects($this->never())
81
            ->method('getDefinition');
82
83
        $this->compiler->process($container);
0 ignored issues
show
Bug introduced by
It seems like $container defined by $this->createContainerBuilderMock() on line 72 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Lug\Bundle\ResourceBundl...xtensionPass::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...
84
    }
85
86
    /**
87
     * @return \PHPUnit_Framework_MockObject_MockObject|ContainerBuilder
88
     */
89
    private function createContainerBuilderMock()
90
    {
91
        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...
92
    }
93
94
    /**
95
     * @return \PHPUnit_Framework_MockObject_MockObject|Definition
96
     */
97
    private function createDefinitionMock()
98
    {
99
        return $this->getMock(Definition::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...
100
    }
101
}
102