Passed
Pull Request — master (#3)
by lee
01:38
created

ClientTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 61
dl 0
loc 103
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testSendRequestWithEmptyUri() 0 8 1
A testNetworkException() 0 15 1
A testConstructor() 0 4 1
A testGetCurlOptions() 0 10 1
A testSetCurlOptions() 0 11 1
A testRequestException() 0 15 1
A testSendRequest() 0 11 1
A testClientException() 0 13 1
1
<?php
2
3
namespace Sunrise\Http\Client\Curl\Tests;
4
5
use RuntimeException;
6
use PHPUnit\Framework\TestCase;
7
use Psr\Http\Client\ClientInterface;
8
use Psr\Http\Client\ClientExceptionInterface;
9
use Psr\Http\Client\NetworkExceptionInterface;
10
use Psr\Http\Client\RequestExceptionInterface;
11
use Psr\Http\Message\ResponseInterface;
12
use Sunrise\Http\Client\Curl\Exception\ClientException;
13
use Sunrise\Http\Client\Curl\Exception\NetworkException;
14
use Sunrise\Http\Client\Curl\Exception\RequestException;
15
use Sunrise\Http\Client\Curl\Client;
16
use Sunrise\Http\Factory\RequestFactory;
17
use Sunrise\Http\Factory\ResponseFactory;
18
use Sunrise\Http\Factory\StreamFactory;
19
20
use const CURLOPT_URL;
21
use const CURLOPT_POST;
22
use const CURLOPT_HEADER;
23
24
class ClientTest extends TestCase
25
{
26
    public function testConstructor()
27
    {
28
        $client = new Client(new ResponseFactory(), new StreamFactory());
29
        $this->assertInstanceOf(ClientInterface::class, $client);
30
    }
31
32
    public function testSendRequest()
33
    {
34
        $url = 'https://raw.githubusercontent.com';
35
        $url .= '/sunrise-php/http-client-curl/dea2ea60d8d5b9f0839d8dba8cd714213c1c2b50/LICENSE';
36
        $client = new Client(new ResponseFactory(), new StreamFactory());
37
        $request = (new RequestFactory)->createRequest('GET', $url);
38
        $response = $client->sendRequest($request);
39
        $this->assertInstanceOf(ResponseInterface::class, $response);
40
        $this->assertEquals(200, $response->getStatusCode());
41
        $this->assertEquals('OK', $response->getReasonPhrase());
42
        $this->assertEquals('text/plain; charset=utf-8', $response->getHeaderLine('content-type'));
43
    }
44
45
    public function testSendRequestWithEmptyUri()
46
    {
47
        $client = new Client(new ResponseFactory(), new StreamFactory());
48
        $request = (new RequestFactory)->createRequest('GET', '');
49
50
        $this->expectException(NetworkExceptionInterface::class);
51
        $this->expectExceptionMessage('<url> malformed');
52
        $client->sendRequest($request);
53
    }
54
55
    public function testClientException()
56
    {
57
        $message = 'foo';
58
        $code = 1;
59
        $previous = new RuntimeException('bar');
60
61
        $exception = new ClientException($message, $code, $previous);
62
        $this->assertInstanceOf(RuntimeException::class, $exception);
63
        $this->assertInstanceOf(ClientExceptionInterface::class, $exception);
64
65
        $this->assertEquals($message, $exception->getMessage());
66
        $this->assertEquals($code, $exception->getCode());
67
        $this->assertEquals($previous, $exception->getPrevious());
68
    }
69
70
    public function testNetworkException()
71
    {
72
        $request = (new RequestFactory)->createRequest('GET', 'http://php.net/');
73
        $message = 'foo';
74
        $code = 1;
75
        $previous = new RuntimeException('bar');
76
77
        $exception = new NetworkException($request, $message, $code, $previous);
78
        $this->assertInstanceOf(ClientException::class, $exception);
79
        $this->assertInstanceOf(NetworkExceptionInterface::class, $exception);
80
81
        $this->assertEquals($request, $exception->getRequest());
82
        $this->assertEquals($message, $exception->getMessage());
83
        $this->assertEquals($code, $exception->getCode());
84
        $this->assertEquals($previous, $exception->getPrevious());
85
    }
86
87
    public function testRequestException()
88
    {
89
        $request = (new RequestFactory)->createRequest('GET', 'http://php.net/');
90
        $message = 'foo';
91
        $code = 1;
92
        $previous = new RuntimeException('bar');
93
94
        $exception = new RequestException($request, $message, $code, $previous);
95
        $this->assertInstanceOf(ClientException::class, $exception);
96
        $this->assertInstanceOf(RequestExceptionInterface::class, $exception);
97
98
        $this->assertEquals($request, $exception->getRequest());
99
        $this->assertEquals($message, $exception->getMessage());
100
        $this->assertEquals($code, $exception->getCode());
101
        $this->assertEquals($previous, $exception->getPrevious());
102
    }
103
104
    public function testSetCurlOptions()
105
    {
106
        $client = new Client(new ResponseFactory(), new StreamFactory());
107
        $curlOptions = [
108
            CURLOPT_URL => 'https://github.com',
109
            CURLOPT_POST => true,
110
            CURLOPT_HEADER => false,
111
        ];
112
        $client->setCurlOptions($curlOptions);
113
114
        $this->assertEquals($curlOptions, $client->getCurlOptions());
115
    }
116
117
    public function testGetCurlOptions()
118
    {
119
        $curlOptions = [
120
            CURLOPT_URL => 'https://github.com',
121
            CURLOPT_POST => true,
122
            CURLOPT_HEADER => false,
123
        ];
124
        $client = new Client(new ResponseFactory(), new StreamFactory(), $curlOptions);
125
126
        $this->assertEquals($curlOptions, $client->getCurlOptions());
127
    }
128
}
129