ClientTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 52
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetClientHandler() 0 9 1
A testSetClientHandler() 0 8 1
A testRequest() 0 14 1
A testRequestError() 0 15 1
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\Postpay;
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 = Postpay::createClientHandler();
29
        $client->setClientHandler($clientHandler);
30
31
        self::assertSame($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);
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);
60
61
        $this->expectException(RESTfulException::class);
62
        $client->request(new Request('GET'));
63
    }
64
}
65