Completed
Pull Request — master (#479)
by Andrey
03:26
created

HttpClientsTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 4
dl 0
loc 155
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 1
A tearDown() 0 6 2
A testHeaders() 0 12 1
A testException() 0 9 2
A testDelete() 0 12 1
A testPut() 0 13 1
A testPost() 0 16 1
A testInvalidGet() 0 9 2
A testGet() 0 13 1
A __doTestRetrieveResponse() 0 7 2
1
<?php
2
3
/**
4
 * @category   OAuth
5
 * @package    Tests
6
 * @author     David Desberg <[email protected]>
7
 * @copyright  Copyright (c) 2012 The authors
8
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
9
 */
10
11
namespace OAuth\Unit\Common\Http;
12
13
use OAuth\Common\Http\Uri\Uri;
14
use OAuth\Common\Http\Uri\UriInterface;
15
use OAuth\Common\Http\Client;
16
17
class HttpClientsTest extends \PHPUnit_Framework_TestCase
18
{
19
    /**
20
     * @var object|\OAuth\Common\Http\Client\ClientInterface[]
21
     */
22
    protected $clients;
23
24
    public function setUp()
25
    {
26
        $streamClient = new Client\StreamClient();
27
        $streamClient->setTimeout(3);
28
29
        $curlClient = new Client\CurlClient();
30
        $curlClient->setTimeout(3);
31
32
        $this->clients[] = $streamClient;
33
        $this->clients[] = $curlClient;
34
    }
35
36
    public function tearDown()
37
    {
38
        foreach ($this->clients as $client) {
39
            unset($client);
40
        }
41
    }
42
43
    /**
44
     * Test that extra headers are passed properly
45
     */
46
    public function testHeaders()
47
    {
48
        $testUri = new Uri('http://httpbin.org/get');
49
50
        $me = $this;
51
        $headerCb = function ($response) use ($me) {
52
            $data = json_decode($response, true);
53
            $me->assertEquals('extraheadertest', $data['headers']['Testingheader']);
54
        };
55
56
        $this->__doTestRetrieveResponse($testUri, array(), array('Testingheader' => 'extraheadertest'), 'GET', $headerCb);
57
    }
58
59
    /**
60
     * Tests that we get an exception for a >= 400 status code
61
     */
62
    public function testException()
63
    {
64
        // sending a post here should get us a 405 which should trigger an exception
65
        $testUri = new Uri('http://httpbin.org/delete');
66
        foreach ($this->clients as $client) {
67
            $this->setExpectedException('OAuth\Common\Http\Exception\TokenResponseException');
68
            $client->retrieveResponse($testUri, array('blah' => 'blih'));
69
        }
70
    }
71
72
    /**
73
     * Tests the DELETE method
74
     */
75
    public function testDelete()
76
    {
77
        $testUri = new Uri('http://httpbin.org/delete');
78
79
        $me = $this;
80
        $deleteTestCb = function ($response) use ($me) {
81
            $data = json_decode($response, true);
82
            $me->assertEquals('', $data['data']);
83
        };
84
85
        $this->__doTestRetrieveResponse($testUri, array(), array(), 'DELETE', $deleteTestCb);
86
    }
87
88
    /**
89
     * Tests the PUT method
90
     */
91
    public function testPut()
92
    {
93
        $testUri = new Uri('http://httpbin.org/put');
94
95
        $me = $this;
96
        $putTestCb = function ($response) use ($me) {
97
            // verify the put response
98
            $data = json_decode($response, true);
99
            $me->assertEquals(json_encode(array('testKey' => 'testValue')), $data['data']);
100
        };
101
102
        $this->__doTestRetrieveResponse($testUri, json_encode(array('testKey' => 'testValue')), array('Content-Type' => 'application/json'), 'PUT', $putTestCb);
0 ignored issues
show
Documentation introduced by
json_encode(array('testKey' => 'testValue')) is of type string, but the function expects a array.

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...
103
    }
104
105
    /**
106
     * Tests the POST method
107
     */
108
    public function testPost()
109
    {
110
        // http test server
111
        $testUri = new Uri('http://httpbin.org/post');
112
113
        $me = $this;
114
        $postTestCb = function ($response) use ($me) {
115
            // verify the post response
116
            $data = json_decode($response, true);
117
            // note that we check this because the retrieveResponse wrapper function automatically adds a content-type
118
            // if there isn't one and it
119
            $me->assertEquals('testValue', $data['form']['testKey']);
120
        };
121
122
        $this->__doTestRetrieveResponse($testUri, array('testKey' => 'testValue'), array(), 'POST', $postTestCb);
123
    }
124
125
    /**
126
     * Expect exception when we try to send a GET request with a body
127
     */
128
    public function testInvalidGet()
129
    {
130
        $testUri =  new Uri('http://site.net');
131
132
        foreach ($this->clients as $client) {
133
            $this->setExpectedException('InvalidArgumentException');
134
            $client->retrieveResponse($testUri, array('blah' => 'blih'), array(), 'GET');
135
        }
136
    }
137
138
    /**
139
     * Tests the GET method
140
     */
141
    public function testGet()
142
    {
143
        // test uri
144
        $testUri = new Uri('http://httpbin.org/get?testKey=testValue');
145
146
        $me = $this;
147
        $getTestCb = function ($response) use ($me) {
148
            $data = json_decode($response, true);
149
            $me->assertEquals('testValue', $data['args']['testKey']);
150
        };
151
152
        $this->__doTestRetrieveResponse($testUri, array(), array(), 'GET', $getTestCb);
153
    }
154
155
    /**
156
     * Test on all HTTP clients.
157
     *
158
     * @param UriInterface $uri
159
     * @param array        $param
160
     * @param array        $header
161
     * @param string       $method
162
     * @param \Closure     $responseCallback
163
     */
164
    protected function __doTestRetrieveResponse(UriInterface $uri, $param, array $header, $method, $responseCallback)
165
    {
166
        foreach ($this->clients as $client) {
167
            $response = $client->retrieveResponse($uri, $param, $header, $method);
168
            $responseCallback($response, $client);
169
        }
170
    }
171
}
172