Completed
Pull Request — master (#145)
by Gabriel
02:12
created

testDeleteUnknownDocumentFromDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\CouchDB\HTTP;
4
5
use Doctrine\CouchDB\HTTP;
6
7
class SocketClientTestCase 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
    public function testNoConnectionPossible()
20
    {
21
        $db = new HTTP\SocketClient( '127.0.0.1', 12345 );
22
23
        try {
24
            $db->request( 'GET', '/' . $this->getTestDatabase() );
25
            $this->fail( 'Expected HTTPException.' );
26
        } catch ( HTTP\HTTPException $e ) {
27
            $this->assertTrue(
28
                // Message depends on the OS, OSX returns 61, Linux 111
29
                $e->getMessage() === 'Could not connect to server at 127.0.0.1:12345: \'111: Connection refused\'' ||
30
                $e->getMessage() === 'Could not connect to server at 127.0.0.1:12345: \'61: Connection refused\''
31
            );
32
33
        }
34
    }
35
36 View Code Duplication
    public function testCreateDatabase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
37
    {
38
        $db = new HTTP\SocketClient();
39
40
        // Remove maybe existing database
41
        try {
42
            $db->request( 'DELETE', '/' . $this->getTestDatabase() );
43
        } catch ( \Exception $e ) { /* Irrelevant exception */ }
44
45
        $response = $db->request( 'PUT', '/' . $this->getTestDatabase() );
46
47
        $this->assertInstanceOf('Doctrine\CouchDB\HTTP\Response', $response);
48
49
        $this->assertTrue($response->body['ok']);
50
    }
51
52
    /**
53
     * @depends testCreateDatabase
54
     */
55 View Code Duplication
    public function testForErrorOnDatabaseRecreation()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
56
    {
57
        $db = new HTTP\SocketClient();
58
59
        $response = $db->request( 'PUT', '/' . $this->getTestDatabase() );
60
        $this->assertInstanceOf('Doctrine\CouchDB\HTTP\ErrorResponse', $response);
61
62
        $this->assertSame( 412, $response->status );
63
        $this->assertSame(
64
            array(
65
                'error'  => 'file_exists',
66
                'reason' => 'The database could not be created, the file already exists.',
67
            ),
68
            $response->body
69
        );
70
    }
71
72
    /**
73
     * @depends testCreateDatabase
74
     */
75 View Code Duplication
    public function testGetDatabaseInformation()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
76
    {
77
        $db = new HTTP\SocketClient();
78
79
        $response = $db->request( 'GET', '/' );
80
81
        $this->assertInstanceOf('Doctrine\CouchDB\HTTP\Response', $response);
82
83
        $this->assertSame(
84
            'Welcome',
85
            $response->body['couchdb']
86
        );
87
    }
88
89
    /**
90
     * @depends testCreateDatabase
91
     */
92 View Code Duplication
    public function testAddDocumentToDatabase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
93
    {
94
        $db = new HTTP\SocketClient();
95
96
        $response = $db->request( 'PUT', '/' . $this->getTestDatabase() . '/123', '{"_id":"123","data":"Foo"}' );
97
98
        $this->assertInstanceOf('Doctrine\CouchDB\HTTP\Response', $response);
99
100
        $this->assertTrue($response->body['ok']);
101
    }
102
103
    /**
104
     * @depends testCreateDatabase
105
     */
106 View Code Duplication
    public function testGetAllDocsFormDatabase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
107
    {
108
        $db = new HTTP\SocketClient();
109
        $db->request( 'PUT', '/' . $this->getTestDatabase() . '/123', '{"_id":"123","data":"Foo"}' );
110
111
        $response = $db->request( 'GET', '/' . $this->getTestDatabase() . '/_all_docs' );
112
113
        $this->assertInstanceOf('Doctrine\CouchDB\HTTP\Response', $response);
114
115
        $this->assertSame(
116
            1,
117
            $response->body['total_rows']
118
        );
119
120
        $this->assertSame(
121
            '123',
122
            $response->body['rows'][0]['id']
123
        );
124
    }
125
126
    /**
127
     * @depends testCreateDatabase
128
     */
129 View Code Duplication
    public function testGetSingleDocumentFromDatabase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
130
    {
131
        $db = new HTTP\SocketClient();
132
        $db->request( 'PUT', '/' . $this->getTestDatabase() . '/123', '{"_id":"123","data":"Foo"}' );
133
134
        $response = $db->request( 'GET', '/' . $this->getTestDatabase() . '/123' );
135
136
        $this->assertInstanceOf('Doctrine\CouchDB\HTTP\Response', $response);
137
138
        $this->assertSame(
139
            '123',
140
            $response->body['_id']
141
        );
142
143
        $this->assertArrayHasKey('_id', $response->body);
144
145
        $this->assertArrayNotHasKey('unknownProperty', $response->body);
146
    }
147
148
    /**
149
     * @depends testCreateDatabase
150
     */
151 View Code Duplication
    public function testGetUnknownDocumentFromDatabase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
152
    {
153
        $db = new HTTP\SocketClient();
154
155
        $response = $db->request( 'GET', '/' . $this->getTestDatabase() . '/not_existant' );
156
        $this->assertInstanceOf('Doctrine\CouchDB\HTTP\ErrorResponse', $response);
157
158
        $this->assertSame( 404, $response->status );
159
        $this->assertSame(
160
            array(
161
                'error'  => 'not_found',
162
                'reason' => 'missing',
163
            ),
164
            $response->body
165
        );
166
    }
167
168
    /**
169
     * @depends testCreateDatabase
170
     */
171 View Code Duplication
    public function testGetDocumentFromNotExistantDatabase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
172
    {
173
        $db = new HTTP\SocketClient();
174
175
        try
176
        {
177
            $db->request( 'DELETE', '/' . $this->getTestDatabase() );
178
        } catch ( \Exception $e ) { /* Ignore */ }
179
180
        $response = $db->request( 'GET', '/' . $this->getTestDatabase() . '/not_existant' );
181
        $this->assertInstanceOf('Doctrine\CouchDB\HTTP\ErrorResponse', $response);
182
183
        $this->assertSame( 404, $response->status );
184
        $this->assertSame(
185
            array(
186
                'error'  => 'not_found',
187
                'reason' => 'no_db_file',
188
            ),
189
            $response->body
190
        );
191
    }
192
193
    /**
194
     * @depends testCreateDatabase
195
     */
196 View Code Duplication
    public function testDeleteUnknownDocumentFromDatabase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
197
    {
198
        $db = new HTTP\SocketClient();
199
200
        $db->request( 'PUT', '/' . $this->getTestDatabase() );
201
        $response = $db->request( 'DELETE', '/' . $this->getTestDatabase() . '/not_existant' );
202
        $this->assertInstanceOf('Doctrine\CouchDB\HTTP\ErrorResponse', $response);
203
204
        $this->assertSame( 404, $response->status );
205
        $this->assertSame(
206
            array(
207
                'error'  => 'not_found',
208
                'reason' => 'missing',
209
            ),
210
            $response->body
211
        );
212
    }
213
214
    /**
215
     * @depends testCreateDatabase
216
     */
217
    public function testDeleteSingleDocumentFromDatabase()
218
    {
219
        $db = new HTTP\SocketClient();
220
        $db->request( 'PUT', '/' . $this->getTestDatabase() . '/123', '{"_id":"123","data":"Foo"}' );
221
222
        $response = $db->request( 'GET', '/' . $this->getTestDatabase() . '/123' );
223
        $db->request( 'DELETE', '/' . $this->getTestDatabase() . '/123?rev=' . $response->body['_rev'] );
224
225
        $response = $db->request( 'GET', '/' . $this->getTestDatabase() . '/123' );
226
        $this->assertInstanceOf('Doctrine\CouchDB\HTTP\ErrorResponse', $response);
227
228
        $this->assertSame( 404, $response->status );
229
        $this->assertSame(
230
            array(
231
                'error'  => 'not_found',
232
                'reason' => 'deleted',
233
            ),
234
            $response->body
235
        );
236
    }
237
238
    /**
239
     * @depends testCreateDatabase
240
     */
241 View Code Duplication
    public function testDeleteDatabase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
242
    {
243
        $db = new HTTP\SocketClient();
244
245
        $response = $db->request( 'DELETE', '/' . $this->getTestDatabase() );
246
247
        $this->assertInstanceOf('Doctrine\CouchDB\HTTP\Response', $response);
248
249
        $this->assertTrue($response->body['ok']);
250
    }
251
252
    /**
253
     * @depends testCreateDatabase
254
     */
255 View Code Duplication
    public function testArrayResponse()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
256
    {
257
        $db = new HTTP\SocketClient();
258
259
        $db->request( 'PUT', '/' . $this->getTestDatabase() );
260
        $response = $db->request( 'GET', '/_all_dbs' );
261
262
        $this->assertInstanceOf('Doctrine\CouchDB\HTTP\Response', $response);
263
264
        $this->assertInternalType('array', $response->body);
265
266
        $this->assertContains(\Doctrine\Tests\ODM\CouchDB\TestUtil::getTestDatabase(), $response->body);
267
    }
268
269
    /**
270
     * @depends testCreateDatabase
271
     */
272 View Code Duplication
    public function testCloseConnection()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
273
    {
274
        $db = new HTTP\SocketClient();
275
        $db->setOption( 'keep-alive', false );
276
277
        $db->request( 'PUT', '/' . $this->getTestDatabase() . '/123', '{"_id":"123","data":"Foo"}' );
278
        $db->request( 'PUT', '/' . $this->getTestDatabase() . '/456', '{"_id":"456","data":"Foo"}' );
279
        $db->request( 'PUT', '/' . $this->getTestDatabase() . '/789', '{"_id":"789","data":"Foo"}' );
280
        $db->request( 'PUT', '/' . $this->getTestDatabase() . '/012', '{"_id":"012","data":"Foo"}' );
281
282
        $response = $db->request( 'GET', '/' . $this->getTestDatabase() . '/_all_docs' );
283
284
        $this->assertInstanceOf('Doctrine\CouchDB\HTTP\Response', $response);
285
286
        $this->assertSame(
287
            4,
288
            $response->body['total_rows']
289
        );
290
    }
291
292
    /**
293
     * @depends testCreateDatabase
294
     */
295 View Code Duplication
    public function testKeepAliveConnection()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
296
    {
297
        $db = new HTTP\SocketClient();
298
        $db->setOption( 'keep-alive', true );
299
300
        $db->request( 'PUT', '/' . $this->getTestDatabase() . '/123', '{"_id":"123","data":"Foo"}' );
301
        $db->request( 'PUT', '/' . $this->getTestDatabase() . '/456', '{"_id":"456","data":"Foo"}' );
302
        $db->request( 'PUT', '/' . $this->getTestDatabase() . '/789', '{"_id":"789","data":"Foo"}' );
303
        $db->request( 'PUT', '/' . $this->getTestDatabase() . '/012', '{"_id":"012","data":"Foo"}' );
304
305
        $response = $db->request( 'GET', '/' . $this->getTestDatabase() . '/_all_docs' );
306
307
        $this->assertInstanceOf('Doctrine\CouchDB\HTTP\Response', $response);
308
309
        $this->assertSame(
310
            4,
311
            $response->body['total_rows']
312
        );
313
    }
314
315
    /**
316
     * @depends testCreateDatabase
317
     */
318
    public function testUnknownOption()
319
    {
320
        $db = new HTTP\SocketClient();
321
322
        try {
323
            $db->setOption( 'unknownOption', 42 );
324
            $this->fail( 'Expected \InvalidArgumentException' );
325
        } catch( \InvalidArgumentException $e ) {
326
            /* Expected */
327
        }
328
    }
329
}
330