Passed
Branch master (c482ba)
by Anatoly
03:50 queued 02:22
created

ClientTest::testConstructor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
class ClientTest extends TestCase
21
{
22
    public function testConstructor()
23
    {
24
        $client = new Client(new ResponseFactory(), new StreamFactory());
25
        $this->assertInstanceOf(ClientInterface::class, $client);
26
    }
27
28
    public function testSendRequest()
29
    {
30
        $url = 'https://raw.githubusercontent.com';
31
        $url .= '/sunrise-php/http-client-curl/dea2ea60d8d5b9f0839d8dba8cd714213c1c2b50/LICENSE';
32
        $client = new Client(new ResponseFactory(), new StreamFactory());
33
        $request = (new RequestFactory)->createRequest('GET', $url);
34
        $response = $client->sendRequest($request);
35
        $this->assertInstanceOf(ResponseInterface::class, $response);
36
        $this->assertEquals(200, $response->getStatusCode());
37
        $this->assertEquals('OK', $response->getReasonPhrase());
38
        $this->assertEquals('text/plain; charset=utf-8', $response->getHeaderLine('content-type'));
39
    }
40
41
    public function testSendRequestWithEmptyUri()
42
    {
43
        $client = new Client(new ResponseFactory(), new StreamFactory());
44
        $request = (new RequestFactory)->createRequest('GET', '');
45
46
        $this->expectException(NetworkExceptionInterface::class);
47
        $response = $client->sendRequest($request);
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
48
    }
49
50
    public function testClientException()
51
    {
52
        $message = 'foo';
53
        $code = 1;
54
        $previous = new RuntimeException('bar');
55
56
        $exception = new ClientException($message, $code, $previous);
57
        $this->assertInstanceOf(RuntimeException::class, $exception);
58
        $this->assertInstanceOf(ClientExceptionInterface::class, $exception);
59
60
        $this->assertEquals($message, $exception->getMessage());
61
        $this->assertEquals($code, $exception->getCode());
62
        $this->assertEquals($previous, $exception->getPrevious());
63
    }
64
65
    public function testNetworkException()
66
    {
67
        $request = (new RequestFactory)->createRequest('GET', 'http://php.net/');
68
        $message = 'foo';
69
        $code = 1;
70
        $previous = new RuntimeException('bar');
71
72
        $exception = new NetworkException($request, $message, $code, $previous);
73
        $this->assertInstanceOf(ClientException::class, $exception);
74
        $this->assertInstanceOf(NetworkExceptionInterface::class, $exception);
75
76
        $this->assertEquals($request, $exception->getRequest());
77
        $this->assertEquals($message, $exception->getMessage());
78
        $this->assertEquals($code, $exception->getCode());
79
        $this->assertEquals($previous, $exception->getPrevious());
80
    }
81
82
    public function testRequestException()
83
    {
84
        $request = (new RequestFactory)->createRequest('GET', 'http://php.net/');
85
        $message = 'foo';
86
        $code = 1;
87
        $previous = new RuntimeException('bar');
88
89
        $exception = new RequestException($request, $message, $code, $previous);
90
        $this->assertInstanceOf(ClientException::class, $exception);
91
        $this->assertInstanceOf(RequestExceptionInterface::class, $exception);
92
93
        $this->assertEquals($request, $exception->getRequest());
94
        $this->assertEquals($message, $exception->getMessage());
95
        $this->assertEquals($code, $exception->getCode());
96
        $this->assertEquals($previous, $exception->getPrevious());
97
    }
98
}
99