Completed
Push — develop ( 2c75a2...3f95e8 )
by Adam
08:12
created

RequestBuilderTest::testDiscoverMessageFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace IBM\Watson\Common\Tests;
4
5
use Http\Message\MessageFactory;
6
use Http\Message\RequestFactory;
7
use IBM\Watson\Common\RequestBuilder;
8
use Mockery as m;
9
use PHPUnit\Framework\TestCase;
10
use Psr\Http\Message\RequestInterface;
11
12
class RequestBuilderTest extends TestCase
13
{
14
    private $requestFactory;
15
16
    public function setUp()
17
    {
18
        $this->requestFactory = m::mock(RequestFactory::class);
19
    }
20
21
    public function testCreate()
22
    {
23
        $this->requestFactory->shouldReceive('createRequest')->andReturn(m::mock(RequestInterface::class));
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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...
24
        $requestBuilder = new RequestBuilder($this->requestFactory);
25
        $request = $requestBuilder->create('GET', '/api', [], null);
26
27
        $this->assertInstanceOf(RequestInterface::class, $request);
28
    }
29
30
    public function testDiscoverMessageFactory()
31
    {
32
        $requestBuilder = new RequestBuilder($this->requestFactory);
33
        $this->assertInstanceOf(MessageFactory::class, $requestBuilder->discoverMessageFactory());
34
    }
35
}
36