@@ -63,6 +63,44 @@ public function testInitializeDocument()
63
63
}
64
64
65
65
66
+ /**
67
+ * Try to create a document silently
68
+ */
69
+ public function testInsertSilent ()
70
+ {
71
+ $ connection = $ this ->connection ;
72
+ $ collection = $ this ->collection ;
73
+ $ document = Document::createFromArray (['_key ' => 'me ' , 'value ' => 1 ]);
74
+ $ documentHandler = new DocumentHandler ($ connection );
75
+
76
+ $ document = $ documentHandler ->insert ($ collection ->getName (), $ document , ['silent ' => true ]);
77
+ static ::assertNull ($ document );
78
+ }
79
+
80
+
81
+ /**
82
+ * Try to create a document silently - with an error
83
+ */
84
+ public function testInsertSilentWithError ()
85
+ {
86
+ $ connection = $ this ->connection ;
87
+ $ collection = $ this ->collection ;
88
+ $ document = Document::createFromArray (['_key ' => 'me ' , 'value ' => 1 ]);
89
+ $ documentHandler = new DocumentHandler ($ connection );
90
+
91
+ // insert the document once
92
+ $ result = $ documentHandler ->insert ($ collection ->getName (), $ document , ['silent ' => true ]);
93
+ static ::assertNull ($ result );
94
+
95
+ // and try to insert it again
96
+ try {
97
+ $ documentHandler ->insert ($ collection ->getName (), $ document , ['silent ' => true ]);
98
+ } catch (\Exception $ exception409 ) {
99
+ }
100
+ static ::assertEquals (409 , $ exception409 ->getCode ());
101
+ }
102
+
103
+
66
104
/**
67
105
* Try to create a document and return it
68
106
*/
@@ -82,7 +120,7 @@ public function testInsertReturnNew()
82
120
83
121
84
122
/**
85
- * Try to create a document and overwrite it
123
+ * Try to create a document and overwrite it, using deprecated overwrite option
86
124
*/
87
125
public function testInsertOverwrite ()
88
126
{
@@ -91,21 +129,36 @@ public function testInsertOverwrite()
91
129
$ document = Document::createFromArray (['_key ' => 'me ' , 'value ' => 1 ]);
92
130
$ documentHandler = new DocumentHandler ($ connection );
93
131
94
- $ document = $ documentHandler ->insert ($ collection ->getName (), $ document , ['returnNew ' => true ]);
132
+ $ document = $ documentHandler ->insert ($ collection ->getName (), $ document , ['returnNew ' => true ]);
95
133
96
134
static ::assertEquals ('me ' , $ document ['_key ' ]);
97
135
static ::assertEquals ('me ' , $ document ['new ' ]['_key ' ]);
98
136
static ::assertEquals (1 , $ document ['new ' ]['value ' ]);
99
137
138
+ try {
139
+ $ documentHandler ->insert ($ collection ->getName (), $ document , ['overwrite ' => false ]);
140
+ } catch (\Exception $ exception409 ) {
141
+ }
142
+ static ::assertEquals (409 , $ exception409 ->getCode ());
143
+
144
+ $ document = Document::createFromArray (['_key ' => 'me ' , 'value ' => 2 ]);
145
+ $ document = $ documentHandler ->insert ($ collection ->getName (), $ document , ['overwrite ' => true , 'returnOld ' => true , 'returnNew ' => true ]);
146
+ static ::assertEquals ('me ' , $ document ['_key ' ]);
147
+ static ::assertEquals ('me ' , $ document ['old ' ]['_key ' ]);
148
+ static ::assertEquals ('me ' , $ document ['new ' ]['_key ' ]);
149
+ static ::assertEquals (1 , $ document ['old ' ]['value ' ]);
150
+ static ::assertEquals (2 , $ document ['new ' ]['value ' ]);
151
+
152
+
100
153
$ document = Document::createFromArray (['_key ' => 'other ' , 'value ' => 2 ]);
101
- $ document = $ documentHandler ->insert ($ collection ->getName (), $ document , ['overwrite ' => false , 'returnOld ' => true , 'returnNew ' => true ]);
154
+ $ document = $ documentHandler ->insert ($ collection ->getName (), $ document , ['overwrite ' => false , 'returnOld ' => true , 'returnNew ' => true ]);
102
155
103
156
static ::assertEquals ('other ' , $ document ['_key ' ]);
104
157
static ::assertEquals ('other ' , $ document ['new ' ]['_key ' ]);
105
158
static ::assertEquals (2 , $ document ['new ' ]['value ' ]);
106
159
107
160
$ document = Document::createFromArray (['_key ' => 'other ' , 'value ' => 3 ]);
108
- $ document = $ documentHandler ->insert ($ collection ->getName (), $ document , ['overwrite ' => true , 'returnOld ' => true , 'returnNew ' => true ]);
161
+ $ document = $ documentHandler ->insert ($ collection ->getName (), $ document , ['overwrite ' => true , 'returnOld ' => true , 'returnNew ' => true ]);
109
162
110
163
static ::assertEquals ('other ' , $ document ['_key ' ]);
111
164
static ::assertEquals ('other ' , $ document ['old ' ]['_key ' ]);
@@ -114,12 +167,90 @@ public function testInsertOverwrite()
114
167
static ::assertEquals (3 , $ document ['new ' ]['value ' ]);
115
168
116
169
$ document = Document::createFromArray (['_key ' => 'foo ' , 'value ' => 4 ]);
117
- $ document = $ documentHandler ->insert ($ collection ->getName (), $ document , ['overwrite ' => true , 'returnOld ' => true , 'returnNew ' => true ]);
170
+ $ document = $ documentHandler ->insert ($ collection ->getName (), $ document , ['overwrite ' => true , 'returnOld ' => true , 'returnNew ' => true ]);
118
171
119
172
static ::assertEquals ('foo ' , $ document ['_key ' ]);
120
173
static ::assertEquals ('foo ' , $ document ['new ' ]['_key ' ]);
121
174
static ::assertEquals (4 , $ document ['new ' ]['value ' ]);
122
175
}
176
+
177
+ /**
178
+ * Try to create a document and overwrite it, using overwriteMode option
179
+ */
180
+ public function testInsertOverwriteMode ()
181
+ {
182
+ $ connection = $ this ->connection ;
183
+ $ collection = $ this ->collection ;
184
+ $ document = Document::createFromArray (['_key ' => 'me ' , 'value ' => 1 ]);
185
+ $ documentHandler = new DocumentHandler ($ connection );
186
+
187
+ $ document = $ documentHandler ->insert ($ collection ->getName (), $ document , ['returnNew ' => true ]);
188
+
189
+ static ::assertEquals ('me ' , $ document ['_key ' ]);
190
+ static ::assertEquals ('me ' , $ document ['new ' ]['_key ' ]);
191
+ static ::assertEquals (1 , $ document ['new ' ]['value ' ]);
192
+
193
+ // conflict mode
194
+ try {
195
+ $ documentHandler ->insert ($ collection ->getName (), $ document , ['overwriteMode ' => 'conflict ' ]);
196
+ } catch (\Exception $ exception409 ) {
197
+ }
198
+ static ::assertEquals (409 , $ exception409 ->getCode ());
199
+
200
+ $ document = Document::createFromArray (['_key ' => 'other-no-conflict ' , 'value ' => 1 ]);
201
+ $ document = $ documentHandler ->insert ($ collection ->getName (), $ document , ['overwriteMode ' => 'conflict ' ]);
202
+
203
+ static ::assertEquals ($ collection ->getName () . '/other-no-conflict ' , $ document );
204
+
205
+
206
+ // ignore mode
207
+ $ document = Document::createFromArray (['_key ' => 'me ' , 'value ' => 2 ]);
208
+ $ document = $ documentHandler ->insert ($ collection ->getName (), $ document , ['overwriteMode ' => 'ignore ' , 'returnOld ' => true , 'returnNew ' => true ]);
209
+
210
+ static ::assertEquals ('me ' , $ document ['_key ' ]);
211
+ static ::assertFalse (isset ($ document ['_new ' ]));
212
+ static ::assertFalse (isset ($ document ['_old ' ]));
213
+
214
+
215
+ $ document = Document::createFromArray (['_key ' => 'yet-another ' , 'value ' => 3 ]);
216
+ $ document = $ documentHandler ->insert ($ collection ->getName (), $ document , ['overwriteMode ' => 'ignore ' , 'returnOld ' => true , 'returnNew ' => true ]);
217
+
218
+ static ::assertEquals ('yet-another ' , $ document ['_key ' ]);
219
+ static ::assertEquals ('yet-another ' , $ document ['new ' ]['_key ' ]);
220
+ static ::assertEquals (3 , $ document ['new ' ]['value ' ]);
221
+ static ::assertFalse (isset ($ document ['_old ' ]));
222
+
223
+
224
+ $ document = Document::createFromArray (['_key ' => 'yet-another ' , 'value ' => 4 ]);
225
+ $ document = $ documentHandler ->insert ($ collection ->getName (), $ document , ['overwriteMode ' => 'ignore ' ]);
226
+
227
+ static ::assertEquals ($ collection ->getName () . '/yet-another ' , $ document );
228
+
229
+
230
+ // update mode
231
+ $ document = Document::createFromArray (['_key ' => 'me ' , 'foo ' => 'bar ' ]);
232
+ $ document = $ documentHandler ->insert ($ collection ->getName (), $ document , ['overwriteMode ' => 'update ' , 'returnOld ' => true , 'returnNew ' => true ]);
233
+
234
+ static ::assertEquals ('me ' , $ document ['_key ' ]);
235
+ static ::assertEquals ('me ' , $ document ['old ' ]['_key ' ]);
236
+ static ::assertEquals (1 , $ document ['old ' ]['value ' ]);
237
+ static ::assertEquals ('me ' , $ document ['new ' ]['_key ' ]);
238
+ static ::assertEquals (1 , $ document ['new ' ]['value ' ]);
239
+ static ::assertEquals ('bar ' , $ document ['new ' ]['foo ' ]);
240
+
241
+
242
+ // replace mode
243
+ $ document = Document::createFromArray (['_key ' => 'me ' , 'qux ' => 'qaz ' ]);
244
+ $ document = $ documentHandler ->insert ($ collection ->getName (), $ document , ['overwriteMode ' => 'replace ' , 'returnOld ' => true , 'returnNew ' => true ]);
245
+
246
+ static ::assertEquals ('me ' , $ document ['_key ' ]);
247
+ static ::assertEquals ('me ' , $ document ['new ' ]['_key ' ]);
248
+ static ::assertEquals (1 , $ document ['old ' ]['value ' ]);
249
+ static ::assertEquals ('bar ' , $ document ['old ' ]['foo ' ]);
250
+ static ::assertFalse (isset ($ document ['new ' ]['foo ' ]));
251
+ static ::assertFalse (isset ($ document ['new ' ]['value ' ]));
252
+ static ::assertEquals ('qaz ' , $ document ['new ' ]['qux ' ]);
253
+ }
123
254
124
255
125
256
/**
@@ -142,7 +273,7 @@ public function testCreateAndDeleteDocumentWithId()
142
273
$ id = $ resultingDocument ->getHandle ();
143
274
static ::assertSame ($ collection ->getName () . '/ ' . $ key , $ id );
144
275
145
- $ documentHandler ->remove ($ document );
276
+ static :: assertTrue ( $ documentHandler ->remove ($ document) );
146
277
}
147
278
148
279
@@ -165,7 +296,30 @@ public function testCreateAndDeleteDocument()
165
296
$ resultingAttribute = $ resultingDocument ->someAttribute ;
166
297
static ::assertSame ('someValue ' , $ resultingAttribute , 'Resulting Attribute should be "someValue". It \'s : ' . $ resultingAttribute );
167
298
168
- $ documentHandler ->remove ($ document );
299
+ static ::assertTrue ($ documentHandler ->remove ($ document ));
300
+ }
301
+
302
+
303
+ /**
304
+ * Try to create and silently delete a document
305
+ */
306
+ public function testCreateAndDeleteDocumentSilent ()
307
+ {
308
+ $ connection = $ this ->connection ;
309
+ $ collection = $ this ->collection ;
310
+ $ document = new Document ();
311
+ $ documentHandler = new DocumentHandler ($ connection );
312
+
313
+ $ document ->someAttribute = 'someValue ' ;
314
+
315
+ $ documentId = $ documentHandler ->save ($ collection ->getName (), $ document );
316
+
317
+ $ resultingDocument = $ documentHandler ->get ($ collection ->getName (), $ documentId );
318
+
319
+ $ resultingAttribute = $ resultingDocument ->someAttribute ;
320
+ static ::assertSame ('someValue ' , $ resultingAttribute , 'Resulting Attribute should be "someValue". It \'s : ' . $ resultingAttribute );
321
+
322
+ static ::assertTrue ($ documentHandler ->remove ($ document , ['silent ' => true ]));
169
323
}
170
324
171
325
@@ -231,7 +385,6 @@ public function testCreateAndDeleteDocumentWithoutCreatedCollectionAndOptionCrea
231
385
}
232
386
233
387
234
-
235
388
/**
236
389
* Try to create and delete a document using a defined key
237
390
*/
0 commit comments