testGetTitleClassAndId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
1
<?php
2
namespace FwlibTest\Html\Generator\Helper;
3
4
use Fwlib\Html\Generator\Helper\GetTitleClassAndIdTrait;
5
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase;
6
use PHPUnit_Framework_MockObject_MockObject as MockObject;
7
8
/**
9
 * @copyright   Copyright 2015 Fwolf
10
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
11
 */
12
class GetTitleClassAndIdTraitTest extends PHPUnitTestCase
13
{
14
    /**
15
     * @param   string[] $methods
16
     * @return  MockObject|GetTitleClassAndIdTrait
17
     */
18
    protected function buildMock(array $methods = null)
19
    {
20
        $mock = $this->getMockBuilder(GetTitleClassAndIdTrait::class)
21
            ->setMethods($methods)
22
            ->getMockForTrait();
23
24
        return $mock;
25
    }
26
27
28
    public function testGetTitleClassAndId()
29
    {
30
        $trait = $this->buildMock(['getClass', 'getId']);
31
        $trait->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Fwlib\Html\Generator\Hel...GetTitleClassAndIdTrait.

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...
32
            ->method('getClass')
33
            ->willReturnOnConsecutiveCalls('', 'foo');
34
35
        $this->assertEquals('', $this->reflectionCall($trait, 'getTitleClass'));
36
        $this->assertEquals(
37
            'foo__title',
38
            $this->reflectionCall($trait, 'getTitleClass')
39
        );
40
41
42
        $trait->expects($this->any())
43
            ->method('getId')
44
            ->willReturnOnConsecutiveCalls('', 'bar', 'bar');
45
46
        $this->assertEquals('', $this->reflectionCall($trait, 'getTitleId'));
47
        $this->assertEquals(
48
            'bar__title',
49
            $this->reflectionCall($trait, 'getTitleId')
50
        );
51
        $this->assertEquals(
52
            'bar__title--42',
53
            $this->reflectionCall($trait, 'getTitleId', [42])
54
        );
55
    }
56
}
57