|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the beebot package. |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Bee4 2015 |
|
8
|
|
|
* @author Stephane HULARD <[email protected]> |
|
9
|
|
|
* @package Bee4\Test\Transport |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Bee4\Test\Transport; |
|
13
|
|
|
|
|
14
|
|
|
use Bee4\Test\FakeDispatcher; |
|
15
|
|
|
use Bee4\PHPUnit\HttpClientTestCase; |
|
16
|
|
|
use Bee4\Transport\Client; |
|
17
|
|
|
use Bee4\Transport\MagicHandler; |
|
18
|
|
|
use Bee4\Transport\Events\MessageEvent; |
|
19
|
|
|
use Bee4\Transport\Events\ErrorEvent; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Transfer client test |
|
23
|
|
|
* @package Bee4\Test\Transport |
|
24
|
|
|
*/ |
|
25
|
|
|
class ClientTest extends HttpClientTestCase |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var Client |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $object; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Sets up the fixture, for example, opens a network connection. |
|
34
|
|
|
* This method is called before a test is executed. |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function setUp() |
|
37
|
|
|
{ |
|
38
|
|
|
if (!extension_loaded('curl')) { |
|
39
|
|
|
$this->markTestSkipped('The curl extension is not available.'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$this->object = new MagicHandler(new Client(self::getBaseUrl())); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @expectedException \InvalidArgumentException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function testNonStringUrl() |
|
49
|
|
|
{ |
|
50
|
|
|
$method = new \ReflectionMethod('\Bee4\Transport\Client', 'createRequest'); |
|
51
|
|
|
$method->setAccessible(true); |
|
52
|
|
|
$method->invoke(new Client(), 'get', new \stdClass()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @expectedException \InvalidArgumentException |
|
57
|
|
|
*/ |
|
58
|
|
|
public function testEmptyUrl() |
|
59
|
|
|
{ |
|
60
|
|
|
$method = new \ReflectionMethod('\Bee4\Transport\Client', 'createRequest'); |
|
61
|
|
|
$method->setAccessible(true); |
|
62
|
|
|
$method->invoke(new Client(), 'post', ''); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testSend() |
|
66
|
|
|
{ |
|
67
|
|
|
//Check that Post request is nicely mapped |
|
68
|
|
|
$request1 = $this->object->get('/index.html'); |
|
|
|
|
|
|
69
|
|
|
$this->assertEquals(self::getBaseUrl().'/index.html', (string)$request1->getUrl()); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertInstanceOf('\Bee4\Transport\Message\AbstractMessage', $request1); |
|
72
|
|
|
$this->assertInstanceOf('\Bee4\Transport\Message\Request\AbstractRequest', $request1); |
|
73
|
|
|
$this->assertInstanceOf('\Bee4\Transport\Message\Request\Http\Get', $request1); |
|
74
|
|
|
$this->assertInstanceOf('\Bee4\Transport\Message\Response', $request1->send()); |
|
75
|
|
|
|
|
76
|
|
|
//Check that Post request is nicely mapped |
|
77
|
|
|
$request2 = $this->object->post('/index.html'); |
|
|
|
|
|
|
78
|
|
|
$this->assertInstanceOf('\Bee4\Transport\Message\AbstractMessage', $request2); |
|
79
|
|
|
$this->assertInstanceOf('\Bee4\Transport\Message\Request\AbstractRequest', $request2); |
|
80
|
|
|
$this->assertInstanceOf('\Bee4\Transport\Message\Request\Http\Post', $request2); |
|
81
|
|
|
$this->assertInstanceOf('\Bee4\Transport\Message\Response', $request2->send()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
View Code Duplication |
public function testGet() |
|
|
|
|
|
|
85
|
|
|
{ |
|
86
|
|
|
$request = $this->object->get('/index.html'); |
|
|
|
|
|
|
87
|
|
|
$response = $request->send(); |
|
88
|
|
|
$options = $request->getOptions(); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertArrayHasKey(CURLOPT_HTTPGET, $options); |
|
91
|
|
|
$this->assertTrue($options[CURLOPT_HTTPGET]); |
|
92
|
|
|
$this->assertInstanceOf('\Bee4\Transport\Message\Request\Http\Get', $response->getRequest()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
public function testPost() |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
$request = $this->object->post('/index.html')->setBody('{"body": "I\'m the body"}}'); |
|
|
|
|
|
|
98
|
|
|
$response = $request->send(); |
|
99
|
|
|
$options = $request->getOptions(); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertArrayHasKey(CURLOPT_POST, $options); |
|
102
|
|
|
$this->assertArrayHasKey(CURLOPT_POSTFIELDS, $options); |
|
103
|
|
|
$this->assertTrue($options[CURLOPT_POST]); |
|
104
|
|
|
$this->assertEquals('{"body": "I\'m the body"}}', $options[CURLOPT_POSTFIELDS]); |
|
105
|
|
|
$this->assertInstanceOf('\Bee4\Transport\Message\Request\Http\Post', $response->getRequest()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
View Code Duplication |
public function testHead() |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
$request = $this->object->head('/index.html'); |
|
|
|
|
|
|
111
|
|
|
$response = $request->send(); |
|
112
|
|
|
$options = $request->getOptions(); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertArrayHasKey(CURLOPT_NOBODY, $options); |
|
115
|
|
|
$this->assertTrue($options[CURLOPT_NOBODY]); |
|
116
|
|
|
$this->assertInstanceOf('\Bee4\Transport\Message\Request\Http\Head', $response->getRequest()); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
View Code Duplication |
public function testDelete() |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
|
|
$request = $this->object->delete('/index.html'); |
|
|
|
|
|
|
122
|
|
|
$response = $request->send(); |
|
123
|
|
|
$options = $request->getOptions(); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertArrayHasKey(CURLOPT_CUSTOMREQUEST, $options); |
|
126
|
|
|
$this->assertArrayHasKey(CURLOPT_POSTFIELDS, $options); |
|
127
|
|
|
$this->assertEquals('DELETE', $options[CURLOPT_CUSTOMREQUEST]); |
|
128
|
|
|
$this->assertEquals(false, $options[CURLOPT_POSTFIELDS]); |
|
129
|
|
|
$this->assertInstanceOf('\Bee4\Transport\Message\Request\Http\Delete', $response->getRequest()); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
View Code Duplication |
public function testPutFromString() |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
|
|
$request = $this->object->put('/index.html'); |
|
|
|
|
|
|
135
|
|
|
$response = $request->send(); |
|
136
|
|
|
$options = $request->getOptions(); |
|
137
|
|
|
|
|
138
|
|
|
$this->assertArrayHasKey(CURLOPT_CUSTOMREQUEST, $options); |
|
139
|
|
|
$this->assertArrayHasKey(CURLOPT_POSTFIELDS, $options); |
|
140
|
|
|
$this->assertEquals('PUT', $options[CURLOPT_CUSTOMREQUEST]); |
|
141
|
|
|
$this->assertEquals(false, $options[CURLOPT_POSTFIELDS]); |
|
142
|
|
|
$this->assertInstanceOf('\Bee4\Transport\Message\Request\Http\Put', $response->getRequest()); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function testPutFromStream() |
|
146
|
|
|
{ |
|
147
|
|
|
$stream = tmpfile(); |
|
148
|
|
|
$size = 200; |
|
149
|
|
|
$content = str_repeat('*', $size); |
|
150
|
|
|
|
|
151
|
|
|
fwrite($stream, $content); |
|
152
|
|
|
|
|
153
|
|
|
$request = $this->object->put('/index.html'); |
|
|
|
|
|
|
154
|
|
|
$request->setBody($stream); |
|
155
|
|
|
$response = $request->send(); |
|
156
|
|
|
$options = $request->getOptions(); |
|
157
|
|
|
|
|
158
|
|
|
$this->assertArrayHasKey(CURLOPT_PUT, $options); |
|
159
|
|
|
$this->assertArrayHasKey(CURLOPT_INFILE, $options); |
|
160
|
|
|
$this->assertArrayHasKey(CURLOPT_INFILESIZE, $options); |
|
161
|
|
|
$this->assertEquals(true, $options[CURLOPT_PUT]); |
|
162
|
|
|
$this->assertEquals($stream, $options[CURLOPT_INFILE]); |
|
163
|
|
|
$this->assertEquals($size, $options[CURLOPT_INFILESIZE]); |
|
164
|
|
|
$this->assertInstanceOf('\Bee4\Transport\Message\Request\Http\Put', $response->getRequest()); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @expectedException \Exception |
|
169
|
|
|
* @expectedExceptionMessage Yes event triggered |
|
170
|
|
|
*/ |
|
171
|
|
|
public function testRegister() |
|
172
|
|
|
{ |
|
173
|
|
|
$dispatcher = new FakeDispatcher(); |
|
174
|
|
|
$dispatcher->add(MessageEvent::REQUEST, function () { |
|
175
|
|
|
throw new \Exception("Yes event triggered"); |
|
176
|
|
|
}); |
|
177
|
|
|
|
|
178
|
|
|
$this->object->setDispatcher($dispatcher); |
|
179
|
|
|
$this->object->get('/index.html')->send(); |
|
|
|
|
|
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function testInvalidProtocol() |
|
183
|
|
|
{ |
|
184
|
|
|
$this->object = new Client("unmapped://127.0.0.1"); |
|
185
|
|
|
|
|
186
|
|
|
try { |
|
187
|
|
|
$this->object->createRequest('GET')->send(); |
|
188
|
|
|
} catch (\Exception $error) { |
|
189
|
|
|
$this->assertInstanceOf("\\Bee4\\Transport\\Exception\\UnknownProtocolException", $error); |
|
190
|
|
|
return; |
|
191
|
|
|
} |
|
192
|
|
|
$this->fail(); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function testCurlError() |
|
196
|
|
|
{ |
|
197
|
|
|
$this->expectOutputString('error'); |
|
198
|
|
|
$this->object = new Client("ftp://127.0.0.1:8888"); |
|
199
|
|
|
|
|
200
|
|
|
$dispatcher = new FakeDispatcher(); |
|
201
|
|
|
$dispatcher->add(ErrorEvent::ERROR, function () { |
|
202
|
|
|
echo "error"; |
|
203
|
|
|
}); |
|
204
|
|
|
$this->object->setDispatcher($dispatcher); |
|
205
|
|
|
|
|
206
|
|
|
try { |
|
207
|
|
|
$this->object->createRequest('GET')->send(); |
|
208
|
|
|
} catch (\Exception $error) { |
|
209
|
|
|
$this->assertInstanceOf("\\Bee4\\Transport\\Exception\\CurlException", $error); |
|
210
|
|
|
return; |
|
211
|
|
|
} |
|
212
|
|
|
$this->fail(); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..