ConnectionTest::test_send_calls_adapter_send()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\Fhp;
4
5
use Fhp\Adapter\AdapterInterface;
6
use Fhp\Adapter\Curl;
7
use Fhp\Connection;
8
use Fhp\Message\Message;
9
10
class ConnectionTest extends \PHPUnit_Framework_TestCase
11
{
12
    /** @var \PHPUnit_Framework_MockObject_MockObject|AdapterInterface */
13
    protected $adapter;
14
    /** @var \PHPUnit_Framework_MockObject_MockObject|Message */
15
    protected $message;
16
17
    public function setUp()
18
    {
19
        $this->adapter = $this->getMockBuilder('\Fhp\Adapter\Curl')
20
            ->disableOriginalConstructor()
21
            ->setMethods(array('send'))
22
            ->getMock();
23
24
        $this->message = $this->getMockBuilder('\Fhp\Message\Message')
25
            ->disableOriginalConstructor()
26
            ->getMock();
27
    }
28
29
    public function test_can_set_and_get_adapter()
30
    {
31
        $conn = new Connection($this->adapter);
32
        $this->assertEquals($this->adapter, $conn->getAdapter());
33
    }
34
35
    public function test_send_calls_adapter_send()
36
    {
37
        $this->adapter->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Fhp\Adapter\AdapterInterface.

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...
38
            ->method('send')
39
            ->with($this->message)
40
            ->will($this->returnValue('response text'));
41
42
        $conn = new Connection($this->adapter);
43
        $res = $conn->send($this->message);
44
45
        $this->assertInternalType('string', $res);
46
    }
47
}
48