Passed
Push — master ( 5319ee...e6b641 )
by Dani
02:13
created

ClientTest::testRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Postpay\Tests\HttpClients;
4
5
use PHPUnit\Framework\TestCase;
6
use Postpay\Exceptions\RESTfulException;
7
use Postpay\Http\Request;
8
use Postpay\Http\Response;
9
use Postpay\HttpClients\Client;
10
use Postpay\HttpClients\ClientInterface;
11
use Postpay\HttpClients\GuzzleClient;
12
13
class ClientTest extends TestCase
14
{
15
    public function testGetClientHandler()
16
    {
17
        $client = new Client();
18
19
        self::assertInstanceOf(
20
            ClientInterface::class,
21
            $client->getClientHandler()
22
        );
23
    }
24
25
    public function testSetClientHandler()
26
    {
27
        $client = new Client();
28
        $clientHandler = new GuzzleClient();
29
        $client->setClientHandler($clientHandler);
30
31
        self::assertEquals($clientHandler, $client->getClientHandler());
32
    }
33
34
    public function testRequest()
35
    {
36
        $clientHandler = $this->createMock(ClientInterface::class);
37
38
        $clientHandler->method('send')->willReturnCallback(
39
            function (Request $request) {
40
                return new Response($request, 200);
41
            }
42
        );
43
        $client = new Client($clientHandler);
1 ignored issue
show
Documentation introduced by
$clientHandler is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a null|object<Postpay\HttpClients\ClientInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
45
        $response = $client->request(new Request('GET'));
46
        self::assertSame(200, $response->getStatusCode());
47
    }
48
49
    public function testRequestError()
50
    {
51
        $clientHandler = $this->createMock(ClientInterface::class);
52
53
        $clientHandler->method('send')->willReturnCallback(
54
            function (Request $request) {
55
                $body = json_encode([RESTfulException::ERROR_KEY => true]);
56
                return new Response($request, 400, [], $body);
57
            }
58
        );
59
        $client = new Client($clientHandler);
1 ignored issue
show
Documentation introduced by
$clientHandler is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a null|object<Postpay\HttpClients\ClientInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
61
        $this->expectException(RESTfulException::class);
62
        $client->request(new Request('GET'));
63
    }
64
}
65