1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\HttpRequest\Tests; |
4
|
|
|
|
5
|
|
|
use Onoi\HttpRequest\MultiCurlRequest; |
6
|
|
|
use Onoi\HttpRequest\CurlRequest; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers \Onoi\HttpRequest\MultiCurlRequest |
10
|
|
|
* @group onoi-http-request |
11
|
|
|
* |
12
|
|
|
* @license GNU GPL v2+ |
13
|
|
|
* @since 1.0 |
14
|
|
|
* |
15
|
|
|
* @author mwjames |
16
|
|
|
*/ |
17
|
|
|
class MultiCurlRequestTest extends \PHPUnit_Framework_TestCase { |
18
|
|
|
|
19
|
|
|
public function testCanConstruct() { |
20
|
|
|
|
21
|
|
|
$instance = new MultiCurlRequest( curl_multi_init() ); |
22
|
|
|
|
23
|
|
|
$this->assertInstanceOf( |
24
|
|
|
'\Onoi\HttpRequest\HttpRequest', |
25
|
|
|
$instance |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
$this->assertInstanceOf( |
29
|
|
|
'\Onoi\HttpRequest\MultiCurlRequest', |
30
|
|
|
$instance |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testWrongResourceTypeThrowsException() { |
35
|
|
|
$this->setExpectedException( 'InvalidArgumentException' ); |
36
|
|
|
new MultiCurlRequest( curl_init() ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testPingForEmptyHttpRequest() { |
40
|
|
|
|
41
|
|
|
$instance = new MultiCurlRequest( curl_multi_init() ); |
42
|
|
|
|
43
|
|
|
$this->assertFalse( |
44
|
|
|
$instance->ping() |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @dataProvider pingConnectionStateProvider |
50
|
|
|
*/ |
51
|
|
|
public function testPingConnectionState( $connectionState ) { |
52
|
|
|
|
53
|
|
|
$instance = new MultiCurlRequest( curl_multi_init() ); |
54
|
|
|
|
55
|
|
|
$httpRequest = $this->getMockBuilder( '\Onoi\HttpRequest\CurlRequest' ) |
56
|
|
|
->disableOriginalConstructor() |
57
|
|
|
->getMock(); |
58
|
|
|
|
59
|
|
|
$httpRequest->expects( $this->once() ) |
60
|
|
|
->method( 'ping' ) |
61
|
|
|
->will( $this->returnValue( $connectionState ) ); |
62
|
|
|
|
63
|
|
|
$instance->addHttpRequest( |
64
|
|
|
$httpRequest |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$this->assertEquals( |
68
|
|
|
$connectionState, |
69
|
|
|
$instance->ping() |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testSetGetOption() { |
74
|
|
|
|
75
|
|
|
// https://github.com/facebook/hhvm/issues/5761 |
76
|
|
|
if ( !defined( 'CURLMOPT_MAXCONNECTS' ) ) { |
77
|
|
|
$this->markTestSkipped( "Option is not supported for current PHP version" ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$instance = new MultiCurlRequest( curl_multi_init() ); |
81
|
|
|
|
82
|
|
|
$instance->setOption( |
83
|
|
|
CURLMOPT_MAXCONNECTS, |
84
|
|
|
5 |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$this->assertSame( |
88
|
|
|
5, |
89
|
|
|
$instance->getOption( CURLMOPT_MAXCONNECTS ) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testExecuteForResponse() { |
94
|
|
|
|
95
|
|
|
$instance = new MultiCurlRequest( curl_multi_init() ); |
96
|
|
|
|
97
|
|
|
$instance->addHttpRequest( |
98
|
|
|
new CurlRequest( curl_init() ) |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
$this->assertInternalType( |
102
|
|
|
'array', |
103
|
|
|
$instance->execute() |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$this->assertInternalType( |
107
|
|
|
'string', |
108
|
|
|
$instance->getLastError() |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
// http://php.net/manual/en/function.curl-multi-info-read.php |
112
|
|
|
$this->assertFalse( |
113
|
|
|
$instance->getLastTransferInfo() |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testDeprecatedSetCallback() { |
118
|
|
|
|
119
|
|
|
$instance = new MultiCurlRequest( curl_multi_init() ); |
120
|
|
|
|
121
|
|
|
$instance->addHttpRequest( |
122
|
|
|
new CurlRequest( curl_init() ) |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$requestResponse = null; |
126
|
|
|
|
127
|
|
|
$instance->setCallback( function( $requestResponseCompleted ) use ( &$requestResponse ) { |
|
|
|
|
128
|
|
|
$requestResponse = $requestResponseCompleted; |
129
|
|
|
} ); |
130
|
|
|
|
131
|
|
|
$instance->execute(); |
132
|
|
|
|
133
|
|
|
$this->assertRequestResponse( $requestResponse ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testOnCompletedCallback() { |
137
|
|
|
|
138
|
|
|
$instance = new MultiCurlRequest( curl_multi_init() ); |
139
|
|
|
|
140
|
|
|
$instance->addHttpRequest( |
141
|
|
|
new CurlRequest( curl_init() ) |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$requestResponse = null; |
145
|
|
|
|
146
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_ON_COMPLETED_CALLBACK, function( $requestResponseCompleted ) use ( &$requestResponse ) { |
147
|
|
|
$requestResponse = $requestResponseCompleted; |
148
|
|
|
} ); |
149
|
|
|
|
150
|
|
|
$instance->execute(); |
151
|
|
|
|
152
|
|
|
$this->assertSame( |
153
|
|
|
0, |
154
|
|
|
$instance->getLastErrorCode() |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
$this->assertRequestResponse( $requestResponse ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
private function assertRequestResponse( $requestResponse ) { |
161
|
|
|
|
162
|
|
|
$this->assertInstanceOf( |
163
|
|
|
'\Onoi\HttpRequest\RequestResponse', |
164
|
|
|
$requestResponse |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
$expectedRequestResponseFields = array( |
168
|
|
|
'contents', |
169
|
|
|
'info' |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
foreach ( $expectedRequestResponseFields as $field ) { |
173
|
|
|
$this->assertTrue( $requestResponse->has( $field ) ); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function pingConnectionStateProvider() { |
178
|
|
|
|
179
|
|
|
$provider[] = array( |
|
|
|
|
180
|
|
|
true |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
$provider[] = array( |
184
|
|
|
false |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
return $provider; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
} |
191
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.