|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CouchDB\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use CouchDB\Connection; |
|
6
|
|
|
use CouchDB\Database; |
|
7
|
|
|
use GuzzleHttp\Psr7\Response; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @author Markus Bachmann <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class DatabaseTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
protected function setUp() |
|
15
|
|
|
{ |
|
16
|
|
|
parent::setUp(); |
|
17
|
|
|
|
|
18
|
|
|
$this->db = new Database('test', $this->connection, $this->client); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function testGetConnection() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->assertEquals($this->connection, $this->db->getConnection()); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function testGetName() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->assertEquals('test', $this->db->getName()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testGetInfo() |
|
32
|
|
|
{ |
|
33
|
|
|
$this->mock->append(new Response(200, [], |
|
34
|
|
|
'{ |
|
35
|
|
|
"compact_running": false, |
|
36
|
|
|
"db_name": "test", |
|
37
|
|
|
"disk_format_version": 5, |
|
38
|
|
|
"disk_size": 12377, |
|
39
|
|
|
"doc_count": 1, |
|
40
|
|
|
"doc_del_count": 1, |
|
41
|
|
|
"instance_start_time": "1267612389906234", |
|
42
|
|
|
"purge_seq": 0, |
|
43
|
|
|
"update_seq": 4 |
|
44
|
|
|
}' |
|
45
|
|
|
)); |
|
46
|
|
|
|
|
47
|
|
|
$info = $this->db->getInfo(); |
|
48
|
|
|
|
|
49
|
|
|
$request = $this->mock->getLastRequest(); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertEquals('GET', $request->getMethod()); |
|
52
|
|
|
$this->assertEquals('/test/', $request->getUri()->getPath()); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertEquals('test', $info['db_name']); |
|
55
|
|
|
$this->assertEquals(false, $info['compact_running']); |
|
56
|
|
|
$this->assertEquals(5, $info['disk_format_version']); |
|
57
|
|
|
$this->assertEquals(12377, $info['disk_size']); |
|
58
|
|
|
$this->assertEquals(1, $info['doc_count']); |
|
59
|
|
|
$this->assertEquals(1, $info['doc_del_count']); |
|
60
|
|
|
$this->assertEquals('1267612389906234', $info['instance_start_time']); |
|
61
|
|
|
$this->assertEquals(0, $info['purge_seq']); |
|
62
|
|
|
$this->assertEquals(4, $info['update_seq']); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
View Code Duplication |
public function testInsertWithOutId() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->mock->append(new Response(201, [], '{"ok":true, "id":"123BAC", "rev":"946B7D1C"}')); |
|
68
|
|
|
$doc = ['author' => 'JohnDoe']; |
|
69
|
|
|
|
|
70
|
|
|
$this->db->insert($doc); |
|
71
|
|
|
$request = $this->mock->getLastRequest(); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertEquals('POST', $request->getMethod()); |
|
74
|
|
|
$this->assertEquals('/test/', $request->getUri()->getPath()); |
|
75
|
|
|
$this->assertEquals('{"author":"JohnDoe"}', (string) $request->getBody()); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertEquals('123BAC', $doc['_id']); |
|
78
|
|
|
$this->assertEquals('946B7D1C', $doc['_rev']); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
View Code Duplication |
public function testInsertWithId() |
|
82
|
|
|
{ |
|
83
|
|
|
$this->mock->append(new Response(201, [], '{"ok":true, "id":"john-doe", "rev":"946B7D1C"}')); |
|
84
|
|
|
|
|
85
|
|
|
$doc = ['author' => 'JohnDoe', '_id' => 'john-doe']; |
|
86
|
|
|
$this->db->insert($doc); |
|
87
|
|
|
|
|
88
|
|
|
$request = $this->mock->getLastRequest(); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertEquals('PUT', $request->getMethod()); |
|
91
|
|
|
$this->assertEquals('/test/john-doe', $request->getUri()->getPath()); |
|
92
|
|
|
$this->assertEquals('{"author":"JohnDoe"}', (string) $request->getBody()); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertEquals('john-doe', $doc['_id']); |
|
95
|
|
|
$this->assertEquals('946B7D1C', $doc['_rev']); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @expectedException CouchDB\Exception\Exception |
|
100
|
|
|
*/ |
|
101
|
|
|
public function testFailedInsert() |
|
102
|
|
|
{ |
|
103
|
|
|
$this->mock->append(new Response(500, [], '{}')); |
|
104
|
|
|
|
|
105
|
|
|
$doc = ['author' => 'JohnDoe', '_id' => 'john-doe']; |
|
106
|
|
|
$this->db->insert($doc); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function testFind() |
|
110
|
|
|
{ |
|
111
|
|
|
$this->mock->append(new Response(200, [], |
|
112
|
|
|
'{ |
|
113
|
|
|
"_id":"john-doe", |
|
114
|
|
|
"_rev":"946B7D1C", |
|
115
|
|
|
"author": "johnDoe", |
|
116
|
|
|
"title": "CouchDB" |
|
117
|
|
|
}' |
|
118
|
|
|
)); |
|
119
|
|
|
|
|
120
|
|
|
$doc = $this->db->find('john-doe'); |
|
121
|
|
|
|
|
122
|
|
|
$request = $this->mock->getLastRequest(); |
|
123
|
|
|
|
|
124
|
|
|
$this->assertEquals('GET', $request->getMethod()); |
|
125
|
|
|
$this->assertEquals('/test/john-doe', $request->getUri()->getPath()); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertEquals('john-doe', $doc['_id']); |
|
128
|
|
|
$this->assertEquals('946B7D1C', $doc['_rev']); |
|
129
|
|
|
$this->assertEquals('johnDoe', $doc['author']); |
|
130
|
|
|
$this->assertEquals('CouchDB', $doc['title']); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @expectedException \CouchDB\Exception\Exception |
|
135
|
|
|
*/ |
|
136
|
|
|
public function testFindNotExistDocument() |
|
137
|
|
|
{ |
|
138
|
|
|
$this->mock->append(new Response(404, [], '{}')); |
|
139
|
|
|
$this->db->find('john-doe'); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function testUpdate() |
|
143
|
|
|
{ |
|
144
|
|
|
$this->mock->append(new Response(201, [], '{"ok":true, "id":"john-doe", "rev":"946B7D1C"}')); |
|
145
|
|
|
|
|
146
|
|
|
$doc = [ |
|
147
|
|
|
'_id' => 'john-doe', |
|
148
|
|
|
'_rev' => '946B7D1C', |
|
149
|
|
|
'author' => 'johnDoe', |
|
150
|
|
|
'title' => 'CouchDB', |
|
151
|
|
|
]; |
|
152
|
|
|
|
|
153
|
|
|
$this->db->update('john-doe', $doc); |
|
154
|
|
|
|
|
155
|
|
|
$this->assertEquals('john-doe', $doc['_id']); |
|
156
|
|
|
$this->assertEquals('946B7D1C', $doc['_rev']); |
|
157
|
|
|
$this->assertEquals('johnDoe', $doc['author']); |
|
158
|
|
|
$this->assertEquals('CouchDB', $doc['title']); |
|
159
|
|
|
|
|
160
|
|
|
$request = $this->mock->getLastRequest(); |
|
161
|
|
|
|
|
162
|
|
|
$this->assertEquals('PUT', $request->getMethod()); |
|
163
|
|
|
$this->assertEquals('/test/john-doe', $request->getUri()->getPath()); |
|
164
|
|
|
$this->assertEquals( |
|
165
|
|
|
'{"_id":"john-doe","_rev":"946B7D1C","author":"johnDoe","title":"CouchDB"}', |
|
166
|
|
|
(string) $request->getBody() |
|
167
|
|
|
); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @expectedException CouchDB\Exception\Exception |
|
172
|
|
|
*/ |
|
173
|
|
|
public function testFailUpdate() |
|
174
|
|
|
{ |
|
175
|
|
|
$this->mock->append(new Response(500, [], '{}')); |
|
176
|
|
|
|
|
177
|
|
|
$doc = [ |
|
178
|
|
|
'_id' => 'john-doe', |
|
179
|
|
|
'_rev' => '946B7D1C', |
|
180
|
|
|
'author' => 'johnDoe', |
|
181
|
|
|
'title' => 'CouchDB', |
|
182
|
|
|
]; |
|
183
|
|
|
|
|
184
|
|
|
$this->db->update('john-doe', $doc); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
View Code Duplication |
public function testDelete() |
|
188
|
|
|
{ |
|
189
|
|
|
$this->mock->append(new Response(200, [], '{"ok":true,"rev":"946B7D1C"}')); |
|
190
|
|
|
|
|
191
|
|
|
$this->assertTrue($this->db->delete('some-doc', '946B7D1C')); |
|
192
|
|
|
|
|
193
|
|
|
$request = $this->mock->getLastRequest(); |
|
194
|
|
|
|
|
195
|
|
|
$this->assertEquals('DELETE', $request->getMethod()); |
|
196
|
|
|
$this->assertEquals('/test/some-doc', $request->getUri()->getPath()); |
|
197
|
|
|
$this->assertEquals('rev=946B7D1C', $request->getUri()->getQuery()); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @expectedException CouchDB\Exception\Exception |
|
202
|
|
|
*/ |
|
203
|
|
|
public function testFailingDelete() |
|
204
|
|
|
{ |
|
205
|
|
|
$this->mock->append(new Response(500, [], '{}')); |
|
206
|
|
|
|
|
207
|
|
|
$this->assertTrue($this->db->delete('some-doc', '946B7D1C')); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
View Code Duplication |
public function testFindAll() |
|
211
|
|
|
{ |
|
212
|
|
|
$this->mock->append(new Response(200, [], '{ |
|
213
|
|
|
"total_rows": 3, "offset": 0, "rows": [ |
|
214
|
|
|
{"id": "doc1", "key": "doc1", "value": {"rev": "4324BB"}}, |
|
215
|
|
|
{"id": "doc2", "key": "doc2", "value": {"rev":"2441HF"}}, |
|
216
|
|
|
{"id": "doc3", "key": "doc3", "value": {"rev":"74EC24"}} |
|
217
|
|
|
] |
|
218
|
|
|
}')); |
|
219
|
|
|
|
|
220
|
|
|
$result = $this->db->findAll(0, 'doc1'); |
|
221
|
|
|
|
|
222
|
|
|
$this->assertEquals(3, $result['total_rows']); |
|
223
|
|
|
$this->assertEquals(0, $result['offset']); |
|
224
|
|
|
$this->assertCount(3, $result['rows']); |
|
225
|
|
|
|
|
226
|
|
|
$request = $this->mock->getLastRequest(); |
|
227
|
|
|
|
|
228
|
|
|
$this->assertEquals('GET', $request->getMethod()); |
|
229
|
|
|
$this->assertEquals('/test/_all_docs', $request->getUri()->getPath()); |
|
230
|
|
|
$this->assertEquals('include_docs=true&limit=0&startkey=doc1', $request->getUri()->getQuery()); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
View Code Duplication |
public function testFindDocuments() |
|
234
|
|
|
{ |
|
235
|
|
|
$this->mock->append(new Response(200, [], '{ |
|
236
|
|
|
"total_rows": 100, "offset": 0, "rows": [ |
|
237
|
|
|
{"id": "doc1", "key": "1", "value": {"rev":"4324BB"}}, |
|
238
|
|
|
{"id": "doc2", "key": "2", "value": {"rev":"2441HF"}} |
|
239
|
|
|
] |
|
240
|
|
|
}')); |
|
241
|
|
|
|
|
242
|
|
|
$docs = $this->db->findDocuments(['1', '2'], 0, 100); |
|
243
|
|
|
$this->assertEquals(100, $docs['total_rows']); |
|
244
|
|
|
$this->assertCount(2, $docs['rows']); |
|
245
|
|
|
|
|
246
|
|
|
$request = $this->mock->getLastRequest(); |
|
247
|
|
|
|
|
248
|
|
|
$this->assertEquals('POST', $request->getMethod()); |
|
249
|
|
|
$this->assertEquals('/test/_all_docs', $request->getUri()->getPath()); |
|
250
|
|
|
$this->assertEquals('include_docs=true&limit=0&skip=100', $request->getUri()->getQuery()); |
|
251
|
|
|
$this->assertEquals('{"keys":["1","2"]}', (string) $request->getBody()); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
public function testCreateBatchUpdater() |
|
255
|
|
|
{ |
|
256
|
|
|
$this->assertInstanceOf('CouchDB\\Util\\BatchUpdater', $this->db->createBatchUpdater()); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
View Code Duplication |
public function testGetChanges() |
|
260
|
|
|
{ |
|
261
|
|
|
$this->mock->append(new Response(200, [], '{"results":[ |
|
262
|
|
|
{"seq":1,"id":"fresh","changes":[{"rev":"1-967a00dff5e02add41819138abb3284d"}]}, |
|
263
|
|
|
{"seq":3,"id":"updated","changes":[{"rev":"2-7051cbe5c8faecd085a3fa619e6e6337"}]}, |
|
264
|
|
|
{"seq":5,"id":"deleted","changes":[{"rev":"2-eec205a9d413992850a6e32678485900"}],"deleted":true} |
|
265
|
|
|
], |
|
266
|
|
|
"last_seq":5}')); |
|
267
|
|
|
|
|
268
|
|
|
$changes = $this->db->getChanges(); |
|
269
|
|
|
|
|
270
|
|
|
$this->assertArrayHasKey('results', $changes); |
|
271
|
|
|
$this->assertCount(3, $changes['results']); |
|
272
|
|
|
$this->assertEquals(5, $changes['last_seq']); |
|
273
|
|
|
|
|
274
|
|
|
$request = $this->mock->getLastRequest(); |
|
275
|
|
|
|
|
276
|
|
|
$this->assertEquals('GET', $request->getMethod()); |
|
277
|
|
|
$this->assertEquals('/test/_changes', $request->getUri()->getPath()); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* @expectedException CouchDB\Exception\Exception |
|
282
|
|
|
*/ |
|
283
|
|
|
public function testFailChanges() |
|
284
|
|
|
{ |
|
285
|
|
|
$this->mock->append(new Response(500, [], '{}')); |
|
286
|
|
|
$this->db->getChanges(); |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
|