1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\CouchDB\HTTP; |
4
|
|
|
|
5
|
|
|
use Doctrine\CouchDB\HTTP; |
6
|
|
|
|
7
|
|
|
class StreamClientTestCase extends \Doctrine\Tests\ODM\CouchDB\CouchDBFunctionalTestCase |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Return test suite |
11
|
|
|
* |
12
|
|
|
* @return PHPUnit_Framework_TestSuite |
13
|
|
|
*/ |
14
|
|
|
public static function suite() |
15
|
|
|
{ |
16
|
|
|
return new \PHPUnit_Framework_TestSuite( __CLASS__ ); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Check that curl-wrapers are enabled, test cases fail otherwise. |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function setUp() |
25
|
|
|
{ |
26
|
|
|
ob_start(); |
27
|
|
|
phpinfo(); |
28
|
|
|
if ( strpos( ob_get_clean(), 'curlwrappers' ) === false ) { |
29
|
|
|
$this->markTestSkipped( 'Enable --with-curlwrappers to run this test.' ); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testNoConnectionPossible() |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
$db = new HTTP\StreamClient( '127.0.0.1', 12345 ); |
37
|
|
|
|
38
|
|
|
try { |
39
|
|
|
$db->request( 'GET', '/' . $this->getTestDatabase() . '' ); |
40
|
|
|
$this->fail( 'Expected HTTP\HTTPException.' ); |
41
|
|
|
} catch ( HTTP\HTTPException $e ) { |
42
|
|
|
$this->assertTrue( |
43
|
|
|
// Message depends on whether the internal stream wrapper or the curlwrappers are used |
44
|
|
|
$e->getMessage() === 'Could not connect to server at 127.0.0.1:12345: \'0: fopen(http://127.0.0.1:12345/'.$this->getTestDatabase().'): failed to open stream: operation failed\'' || |
45
|
|
|
$e->getMessage() === 'Could not connect to server at 127.0.0.1:12345: \'0: fopen(http://127.0.0.1:12345/'.$this->getTestDatabase().'): failed to open stream: Connection refused\'' || |
46
|
|
|
$e->getMessage() === 'Could read from server at 127.0.0.1:12345: \'0: Received an empty response or not status code\'' |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
public function testCreateDatabase() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$db = new HTTP\StreamClient(); |
54
|
|
|
|
55
|
|
|
// Remove maybe existing database |
56
|
|
|
try { |
57
|
|
|
$db->request( 'DELETE', '/' . $this->getTestDatabase() . '' ); |
58
|
|
|
} catch ( \Exception $e ) { /* Irrelevant exception */ } |
59
|
|
|
|
60
|
|
|
$response = $db->request( 'PUT', '/' . $this->getTestDatabase() . '' ); |
61
|
|
|
|
62
|
|
|
$this->assertInstanceOf('Doctrine\CouchDB\HTTP\Response', $response); |
63
|
|
|
|
64
|
|
|
$this->assertTrue($response->body['ok']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @depends testCreateDatabase |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function testForErrorOnDatabaseRecreation() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$db = new HTTP\StreamClient(); |
73
|
|
|
|
74
|
|
|
$response = $db->request( 'PUT', '/' . $this->getTestDatabase() . '' ); |
75
|
|
|
$this->assertInstanceOf('HTTP\ErrorResponse', $response); |
76
|
|
|
|
77
|
|
|
$this->assertSame( 412, $response->status ); |
78
|
|
|
$this->assertSame( |
79
|
|
|
array( |
80
|
|
|
'error' => 'file_exists', |
81
|
|
|
'reason' => 'The database could not be created, the file already exists.', |
82
|
|
|
), |
83
|
|
|
$response->body |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @depends testCreateDatabase |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function testGetDatabaseInformation() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$db = new HTTP\StreamClient(); |
93
|
|
|
|
94
|
|
|
$response = $db->request( 'GET', '/' ); |
95
|
|
|
|
96
|
|
|
$this->assertInstanceOf('Doctrine\CouchDB\HTTP\Response', $response); |
97
|
|
|
|
98
|
|
|
$this->assertSame( |
99
|
|
|
'Welcome', |
100
|
|
|
$response->body['couchdb'] |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @depends testCreateDatabase |
106
|
|
|
*/ |
107
|
|
View Code Duplication |
public function testAddDocumentToDatabase() |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$db = new HTTP\StreamClient(); |
110
|
|
|
|
111
|
|
|
$response = $db->request( 'PUT', '/' . $this->getTestDatabase() . '/123', '{"_id":"123","data":"Foo"}' ); |
112
|
|
|
|
113
|
|
|
$this->assertInstanceOf('Doctrine\CouchDB\HTTP\Response', $response); |
114
|
|
|
|
115
|
|
|
$this->assertTrue($response->body['ok']); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @depends testCreateDatabase |
120
|
|
|
*/ |
121
|
|
View Code Duplication |
public function testGetAllDocsFormDatabase() |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$db = new HTTP\StreamClient(); |
124
|
|
|
$db->request( 'PUT', '/' . $this->getTestDatabase() . '/123', '{"_id":"123","data":"Foo"}' ); |
125
|
|
|
|
126
|
|
|
$response = $db->request( 'GET', '/' . $this->getTestDatabase() . '/_all_docs' ); |
127
|
|
|
|
128
|
|
|
$this->assertInstanceOf('Doctrine\CouchDB\HTTP\Response', $response); |
129
|
|
|
|
130
|
|
|
$this->assertSame( |
131
|
|
|
1, |
132
|
|
|
$response->body['total_rows'] |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
$this->assertSame( |
136
|
|
|
'123', |
137
|
|
|
$response->body['rows'][0]['id'] |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @depends testCreateDatabase |
143
|
|
|
*/ |
144
|
|
View Code Duplication |
public function testGetSingleDocumentFromDatabase() |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
$db = new HTTP\StreamClient(); |
147
|
|
|
$db->request( 'PUT', '/' . $this->getTestDatabase() . '/123', '{"_id":"123","data":"Foo"}' ); |
148
|
|
|
|
149
|
|
|
$response = $db->request( 'GET', '/' . $this->getTestDatabase() . '/123' ); |
150
|
|
|
|
151
|
|
|
$this->assertInstanceOf('Doctrine\CouchDB\HTTP\Response', $response); |
152
|
|
|
|
153
|
|
|
$this->assertSame( |
154
|
|
|
'123', |
155
|
|
|
$response->body['_id'] |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$this->assertArrayHasKey('_id', $response->body); |
159
|
|
|
|
160
|
|
|
$this->assertArrayNotHasKey('unknownProperty', $response->body); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @depends testCreateDatabase |
165
|
|
|
*/ |
166
|
|
View Code Duplication |
public function testGetUnknownDocumentFromDatabase() |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$db = new HTTP\StreamClient(); |
169
|
|
|
|
170
|
|
|
$response = $db->request( 'GET', '/' . $this->getTestDatabase() . '/not_existant' ); |
171
|
|
|
$this->assertInstanceOf('HTTP\ErrorResponse', $response); |
172
|
|
|
|
173
|
|
|
$this->assertSame( 404, $response->status ); |
174
|
|
|
$this->assertSame( |
175
|
|
|
array( |
176
|
|
|
'error' => 'not_found', |
177
|
|
|
'reason' => 'missing', |
178
|
|
|
), |
179
|
|
|
$response->body |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @depends testCreateDatabase |
185
|
|
|
*/ |
186
|
|
View Code Duplication |
public function testGetDocumentFromNotExistantDatabase() |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
$db = new HTTP\StreamClient(); |
189
|
|
|
|
190
|
|
|
try { |
191
|
|
|
$db->request( 'DELETE', '/' . $this->getTestDatabase() . '' ); |
192
|
|
|
} catch ( \Exception $e ) { /* Ignore */ } |
193
|
|
|
|
194
|
|
|
$response = $db->request( 'GET', '/' . $this->getTestDatabase() . '/not_existant' ); |
195
|
|
|
$this->assertInstanceOf('HTTP\ErrorResponse', $response); |
196
|
|
|
|
197
|
|
|
$this->assertSame( 404, $response->status ); |
198
|
|
|
$this->assertSame( |
199
|
|
|
array( |
200
|
|
|
'error' => 'not_found', |
201
|
|
|
'reason' => 'no_db_file', |
202
|
|
|
), |
203
|
|
|
$response->body |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @depends testCreateDatabase |
209
|
|
|
*/ |
210
|
|
View Code Duplication |
public function testDeleteUnknownDocumentFromDatabase() |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
$db = new HTTP\StreamClient(); |
213
|
|
|
|
214
|
|
|
$response = $db->request( 'DELETE', '/' . $this->getTestDatabase() . '/not_existant' ); |
215
|
|
|
$this->assertInstanceOf('HTTP\ErrorResponse', $response); |
216
|
|
|
|
217
|
|
|
$this->assertSame( 404, $response->status ); |
218
|
|
|
$this->assertSame( |
219
|
|
|
array( |
220
|
|
|
'error' => 'not_found', |
221
|
|
|
'reason' => 'no_db_file', |
222
|
|
|
), |
223
|
|
|
$response->body |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @depends testCreateDatabase |
229
|
|
|
*/ |
230
|
|
|
public function testDeleteSingleDocumentFromDatabase() |
231
|
|
|
{ |
232
|
|
|
$db = new HTTP\StreamClient(); |
233
|
|
|
|
234
|
|
|
$db->request( 'PUT', '/' . $this->getTestDatabase() . '' ); |
235
|
|
|
$db->request( 'PUT', '/' . $this->getTestDatabase() . '/123', '{"_id":"123","data":"Foo"}' ); |
236
|
|
|
$response = $db->request( 'GET', '/' . $this->getTestDatabase() . '/123' ); |
237
|
|
|
$db->request( 'DELETE', '/' . $this->getTestDatabase() . '/123?rev=' . $response->body['_rev'] ); |
238
|
|
|
|
239
|
|
|
$response = $db->request( 'GET', '/' . $this->getTestDatabase() . '/123' ); |
240
|
|
|
$this->assertInstanceOf('HTTP\ErrorResponse', $response); |
241
|
|
|
|
242
|
|
|
$this->assertSame( 404, $response->status ); |
243
|
|
|
$this->assertSame( |
244
|
|
|
array( |
245
|
|
|
'error' => 'not_found', |
246
|
|
|
'reason' => 'deleted', |
247
|
|
|
), |
248
|
|
|
$response->body |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @depends testCreateDatabase |
254
|
|
|
*/ |
255
|
|
View Code Duplication |
public function testDeleteDatabase() |
|
|
|
|
256
|
|
|
{ |
257
|
|
|
$db = new HTTP\StreamClient(); |
258
|
|
|
|
259
|
|
|
$response = $db->request( 'DELETE', '/' . $this->getTestDatabase() . '' ); |
260
|
|
|
|
261
|
|
|
$this->assertInstanceOf('Doctrine\CouchDB\HTTP\Response', $response); |
262
|
|
|
|
263
|
|
|
$this->assertTrue($response->body['ok']); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @depends testCreateDatabase |
268
|
|
|
*/ |
269
|
|
View Code Duplication |
public function testArrayResponse() |
|
|
|
|
270
|
|
|
{ |
271
|
|
|
$db = new HTTP\StreamClient(); |
272
|
|
|
|
273
|
|
|
try { |
274
|
|
|
$db->request( 'PUT', '/' . $this->getTestDatabase() . '' ); |
275
|
|
|
} catch ( \Exception $e ) { /* Ignore */ } |
276
|
|
|
$response = $db->request( 'GET', '/_all_dbs' ); |
277
|
|
|
|
278
|
|
|
$this->assertInstanceOf('Doctrine\CouchDB\HTTP\Response', $response); |
279
|
|
|
|
280
|
|
|
$this->assertInternalType('array', $response->body); |
281
|
|
|
|
282
|
|
|
$this->assertContains(\Doctrine\Tests\ODM\CouchDB\TestUtil::getTestDatabase(), $response->body); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @depends testCreateDatabase |
287
|
|
|
*/ |
288
|
|
View Code Duplication |
public function testCloseConnection() |
|
|
|
|
289
|
|
|
{ |
290
|
|
|
$db = new HTTP\StreamClient(); |
291
|
|
|
$db->setOption( 'keep-alive', false ); |
292
|
|
|
|
293
|
|
|
$db->request( 'PUT', '/' . $this->getTestDatabase() . '/123', '{"_id":"123","data":"Foo"}' ); |
294
|
|
|
$db->request( 'PUT', '/' . $this->getTestDatabase() . '/456', '{"_id":"456","data":"Foo"}' ); |
295
|
|
|
$db->request( 'PUT', '/' . $this->getTestDatabase() . '/789', '{"_id":"789","data":"Foo"}' ); |
296
|
|
|
$db->request( 'PUT', '/' . $this->getTestDatabase() . '/012', '{"_id":"012","data":"Foo"}' ); |
297
|
|
|
|
298
|
|
|
$response = $db->request( 'GET', '/' . $this->getTestDatabase() . '/_all_docs' ); |
299
|
|
|
|
300
|
|
|
$this->assertInstanceOf('Doctrine\CouchDB\HTTP\Response', $response); |
301
|
|
|
|
302
|
|
|
$this->assertSame( |
303
|
|
|
4, |
304
|
|
|
$response->body['total_rows'] |
305
|
|
|
); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @depends testCreateDatabase |
310
|
|
|
*/ |
311
|
|
View Code Duplication |
public function testKeepAliveConnection() |
|
|
|
|
312
|
|
|
{ |
313
|
|
|
$db = new HTTP\StreamClient(); |
314
|
|
|
$db->setOption( 'keep-alive', true ); |
315
|
|
|
|
316
|
|
|
$db->request( 'PUT', '/' . $this->getTestDatabase() . '/123', '{"_id":"123","data":"Foo"}' ); |
317
|
|
|
$db->request( 'PUT', '/' . $this->getTestDatabase() . '/456', '{"_id":"456","data":"Foo"}' ); |
318
|
|
|
$db->request( 'PUT', '/' . $this->getTestDatabase() . '/789', '{"_id":"789","data":"Foo"}' ); |
319
|
|
|
$db->request( 'PUT', '/' . $this->getTestDatabase() . '/012', '{"_id":"012","data":"Foo"}' ); |
320
|
|
|
|
321
|
|
|
$response = $db->request( 'GET', '/' . $this->getTestDatabase() . '/_all_docs' ); |
322
|
|
|
|
323
|
|
|
$this->assertInstanceOf('Doctrine\CouchDB\HTTP\Response', $response); |
324
|
|
|
|
325
|
|
|
$this->assertSame( |
326
|
|
|
4, |
327
|
|
|
$response->body['total_rows'] |
328
|
|
|
); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @depends testCreateDatabase |
333
|
|
|
*/ |
334
|
|
|
public function testUnknownOption() |
335
|
|
|
{ |
336
|
|
|
$db = new HTTP\StreamClient(); |
337
|
|
|
|
338
|
|
|
try { |
339
|
|
|
$db->setOption( 'unknownOption', 42 ); |
340
|
|
|
$this->fail( 'Expected \InvalidArgumentException.' ); |
341
|
|
|
} catch( \InvalidArgumentException $e ) { |
342
|
|
|
/* Expected */ |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.