Completed
Push — master ( d991f2...2b3b24 )
by Julián
04:04
created

ClientTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 86
rs 10
1
<?php
2
3
/*
4
 * A PSR7 aware cURL client (https://github.com/juliangut/spiral).
5
 *
6
 * @license BSD-3-Clause
7
 * @link https://github.com/juliangut/spiral
8
 * @author Julián Gutiérrez <[email protected]>
9
 */
10
11
namespace Jgut\Spiral\Tests;
12
13
use Jgut\Spiral\Client;
14
use Jgut\Spiral\Exception\TransportException;
15
use Zend\Diactoros\Request;
16
use Zend\Diactoros\Response;
17
18
/**
19
 * Client handler tests.
20
 */
21
class ClientTest extends \PHPUnit_Framework_TestCase
22
{
23
    public function testGettersSetters()
24
    {
25
        $transport = $this->getMock('\Jgut\Spiral\Transport');
26
27
        $client = new Client();
28
29
        $client->setTransport($transport);
30
31
        static::assertEquals($transport, $client->getTransport());
32
    }
33
34
    public function testBadRequest()
35
    {
36
        $request = new Request('http://fake_made_up_web.com', 'GET');
37
        $response = new Response;
38
39
        $client = new Client();
40
        try {
41
            $client->request($request, $response);
42
        } catch (TransportException $exception) {
43
            static::assertEquals(CURLE_COULDNT_RESOLVE_HOST, $exception->getCode());
44
            static::assertEquals('', $exception->getCategory());
45
        }
46
    }
47
48
    public function testRequest()
49
    {
50
        $responseContent = <<<RESP
51
HTTP/1.1 302 FOUND
52
Server: nginx
53
Date: Tue, 09 Feb 2016 11:54:35 GMT
54
Content-Type: text/html; charset=utf-8
55
Content-Length: 0
56
Connection: keep-alive
57
Location: get
58
Access-Control-Allow-Origin: *
59
Access-Control-Allow-Credentials: true
60
61
HTTP/1.1 200 OK
62
Server: nginx
63
Date: Tue, 09 Feb 2016 11:54:35 GMT
64
Content-Type: text/html; charset=utf-8
65
Content-Length: 30
66
Connection: keep-alive
67
Access-Control-Allow-Origin: *
68
Access-Control-Allow-Credentials: true
69
70
<!doctype html>
71
<html>
72
</html>
73
RESP;
74
        $transferInfo = [
75
            'header_size' => 452,
76
            'http_code' => 200,
77
            'content_type' => 'text/html; charset=utf-8',
78
        ];
79
80
        $transport = $this->getMockBuilder('\Jgut\Spiral\Transport\Curl')
81
            ->disableOriginalConstructor()
82
            ->getMock();
83
        $transport
84
            ->expects(static::once())
85
            ->method('request')
86
            ->will(static::returnValue($responseContent));
87
        $transport
88
            ->expects(static::once())
89
            ->method('responseInfo')
90
            ->will(static::returnValue($transferInfo));
91
        $transport
92
            ->expects(static::once())
93
            ->method('close');
94
95
        $request = new Request('', 'GET');
96
        $response = new Response;
97
98
        $client = new Client($transport);
99
        $response = $client->request($request, $response);
100
101
        static::assertEquals(200, $response->getStatusCode());
102
        static::assertEquals('nginx', $response->getHeaderLine('Server'));
103
        static::assertFalse($response->hasHeader('Location'));
104
        static::assertEquals(1, preg_match('/^<!doctype html>/i', $response->getBody()));
105
    }
106
}
107