BaseCommandTest::testConfigure()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 30
nc 1
nop 0
1
<?php
2
3
namespace Stp\SndApi\Common\Test\Command;
4
5
use Stp\SndApi\Common\Command\BaseCommand;
6
use Stp\SndApi\Common\Test\InvokeInaccessibleMethodTrait;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputOption;
9
10
class BaseCommandTest extends \PHPUnit_Framework_TestCase
11
{
12
    use InvokeInaccessibleMethodTrait;
13
14
    public function testConfigure()
15
    {
16
        /** @var Command|\PHPUnit_Framework_MockObject_MockObject $stub */
17
        $stub = $this->getMockBuilder(BaseCommand::class)
18
            ->disableOriginalConstructor()
19
            ->setMethods(['addOption'])
20
            ->getMock();
21
22
        $stub->expects($this->at(0))
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Console\Command\Command.

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...
23
            ->method('addOption')
24
            ->with(
25
                'key',
26
                'k',
27
                InputOption::VALUE_OPTIONAL,
28
                $this->anything()
29
            )
30
            ->willReturn($stub);
31
32
        $stub->expects($this->at(1))
33
            ->method('addOption')
34
            ->with(
35
                'secret',
36
                's',
37
                InputOption::VALUE_REQUIRED,
38
                $this->anything()
39
            )
40
            ->willReturn($stub);
41
42
        $stub->expects($this->at(2))
43
            ->method('addOption')
44
            ->with(
45
                'publicationId',
46
                'p',
47
                InputOption::VALUE_REQUIRED,
48
                $this->anything()
49
            )
50
            ->willReturn($stub);
51
52
        $this->invokeMethod($stub, 'configure');
53
    }
54
}
55