|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* EmbeddingDocumentsTest class file |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\CoreBundle\Tests\Controller; |
|
7
|
|
|
|
|
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
9
|
|
|
use Graviton\TestBundle\Test\RestTestCase; |
|
10
|
|
|
use GravitonDyn\EmbedTestEntityBundle\DataFixtures\MongoDB\LoadEmbedTestEntityData; |
|
11
|
|
|
use GravitonDyn\EmbedTestDocumentAsReferenceBundle\DataFixtures\MongoDB\LoadEmbedTestDocumentAsReferenceData; |
|
12
|
|
|
use GravitonDyn\EmbedTestDocumentAsEmbeddedBundle\DataFixtures\MongoDB\LoadEmbedTestDocumentAsEmbeddedData; |
|
13
|
|
|
use GravitonDyn\EmbedTestDocumentAsDeepReferenceBundle\DataFixtures\MongoDB\LoadEmbedTestDocumentAsDeepReferenceData; |
|
14
|
|
|
use GravitonDyn\EmbedTestDocumentAsDeepEmbeddedBundle\DataFixtures\MongoDB\LoadEmbedTestDocumentAsDeepEmbeddedData; |
|
15
|
|
|
use GravitonDyn\EmbedTestHashAsEmbeddedBundle\DataFixtures\MongoDB\LoadEmbedTestHashAsEmbeddedData; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
19
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
20
|
|
|
* @link http://swisscom.ch |
|
21
|
|
|
*/ |
|
22
|
|
|
class EmbeddingDocumentsTest extends RestTestCase |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* load fixtures |
|
26
|
|
|
* |
|
27
|
|
|
* @return void |
|
28
|
|
|
*/ |
|
29
|
|
|
public function setUp() |
|
30
|
|
|
{ |
|
31
|
|
|
if (!class_exists(LoadEmbedTestEntityData::class) || |
|
32
|
|
|
!class_exists(LoadEmbedTestDocumentAsEmbeddedData::class) || |
|
33
|
|
|
!class_exists(LoadEmbedTestDocumentAsReferenceData::class) || |
|
34
|
|
|
!class_exists(LoadEmbedTestHashAsEmbeddedData::class)) { |
|
35
|
|
|
$this->markTestSkipped('Test definitions are not loaded'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$this->loadFixtures( |
|
39
|
|
|
[ |
|
40
|
|
|
LoadEmbedTestEntityData::class, |
|
41
|
|
|
LoadEmbedTestDocumentAsEmbeddedData::class, |
|
42
|
|
|
LoadEmbedTestDocumentAsReferenceData::class, |
|
43
|
|
|
LoadEmbedTestDocumentAsDeepEmbeddedData::class, |
|
44
|
|
|
LoadEmbedTestDocumentAsDeepReferenceData::class, |
|
45
|
|
|
LoadEmbedTestHashAsEmbeddedData::class, |
|
46
|
|
|
], |
|
47
|
|
|
null, |
|
48
|
|
|
'doctrine_mongodb' |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $id ID |
|
54
|
|
|
* @param mixed $data Data |
|
55
|
|
|
* @return void |
|
56
|
|
|
* @throws \PHPUnit_Framework_AssertionFailedError |
|
57
|
|
|
*/ |
|
58
|
|
|
private function assertEntityExists($id, $data) |
|
59
|
|
|
{ |
|
60
|
|
|
$client = static::createRestClient(); |
|
61
|
|
|
$client->request('GET', '/testcase/embedtest-entity/'.$id); |
|
62
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
63
|
|
|
$this->assertInternalType('object', $client->getResults()); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertEquals($id, $client->getResults()->id); |
|
66
|
|
|
$this->assertEquals($data, $client->getResults()->data); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $id ID |
|
71
|
|
|
* @return void |
|
72
|
|
|
* @throws \PHPUnit_Framework_AssertionFailedError |
|
73
|
|
|
*/ |
|
74
|
|
|
private function assertEntityNotExists($id) |
|
75
|
|
|
{ |
|
76
|
|
|
$client = static::createRestClient(); |
|
77
|
|
|
$client->request('GET', '/testcase/embedtest-entity/'.$id); |
|
78
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $client->getResponse()->getStatusCode()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Test Document as embedded |
|
83
|
|
|
* |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
View Code Duplication |
public function testDocumentAsEmbedded() |
|
|
|
|
|
|
87
|
|
|
{ |
|
88
|
|
|
$original = (object) [ |
|
89
|
|
|
'id' => 'test', |
|
90
|
|
|
'document' => (object) ['id' => 'one', 'data' => 'one'], |
|
91
|
|
|
]; |
|
92
|
|
|
|
|
93
|
|
|
// check entities |
|
94
|
|
|
$this->assertEntityExists('one', 'one'); |
|
95
|
|
|
$this->assertEntityExists('two', 'two'); |
|
96
|
|
|
|
|
97
|
|
|
// check document |
|
98
|
|
|
$client = static::createRestClient(); |
|
99
|
|
|
$client->request('GET', '/testcase/embedtest-document-as-embedded/test'); |
|
100
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
101
|
|
|
$this->assertEquals($original, $client->getResults()); |
|
102
|
|
|
|
|
103
|
|
|
// update document |
|
104
|
|
|
$data = $client->getResults(); |
|
105
|
|
|
$data->document = (object) [ |
|
106
|
|
|
'id' => 'two', |
|
107
|
|
|
'data' => 'two', |
|
108
|
|
|
]; |
|
109
|
|
|
|
|
110
|
|
|
$client = static::createRestClient(); |
|
111
|
|
|
$client->put('/testcase/embedtest-document-as-embedded/test', $data); |
|
112
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
|
113
|
|
|
|
|
114
|
|
|
// check data |
|
115
|
|
|
$client = static::createRestClient(); |
|
116
|
|
|
$client->request('GET', '/testcase/embedtest-document-as-embedded/test'); |
|
117
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
118
|
|
|
$this->assertEquals($data, $client->getResults()); |
|
119
|
|
|
|
|
120
|
|
|
// check entities again |
|
121
|
|
|
$this->assertEntityExists('one', 'one'); |
|
122
|
|
|
$this->assertEntityExists('two', 'two'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Test Document as reference |
|
127
|
|
|
* |
|
128
|
|
|
* @return void |
|
129
|
|
|
*/ |
|
130
|
|
|
public function testDocumentAsReference() |
|
131
|
|
|
{ |
|
132
|
|
|
$original = (object) [ |
|
133
|
|
|
'id' => 'test', |
|
134
|
|
|
'document' => (object) ['id' => 'one', 'data' => 'one'], |
|
135
|
|
|
]; |
|
136
|
|
|
|
|
137
|
|
|
// check entities |
|
138
|
|
|
$this->assertEntityExists('one', 'one'); |
|
139
|
|
|
$this->assertEntityExists('two', 'two'); |
|
140
|
|
|
$this->assertEntityNotExists('three'); |
|
141
|
|
|
|
|
142
|
|
|
// check document |
|
143
|
|
|
$client = static::createRestClient(); |
|
144
|
|
|
$client->request('GET', '/testcase/embedtest-document-as-reference/test'); |
|
145
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
146
|
|
|
$this->assertEquals($original, $client->getResults()); |
|
147
|
|
|
|
|
148
|
|
|
// update document |
|
149
|
|
|
$data = $client->getResults(); |
|
150
|
|
|
$data->document = (object) [ |
|
151
|
|
|
'id' => 'three', |
|
152
|
|
|
'data' => 'three', |
|
153
|
|
|
]; |
|
154
|
|
|
|
|
155
|
|
|
$client = static::createRestClient(); |
|
156
|
|
|
$client->put('/testcase/embedtest-document-as-reference/test', $data); |
|
157
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
|
158
|
|
|
|
|
159
|
|
|
// check data |
|
160
|
|
|
$client = static::createRestClient(); |
|
161
|
|
|
$client->request('GET', '/testcase/embedtest-document-as-reference/test'); |
|
162
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
163
|
|
|
$this->assertEquals($data, $client->getResults()); |
|
164
|
|
|
|
|
165
|
|
|
// check entities again |
|
166
|
|
|
// record "one" was not removed. it is incorrect |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* this is imho the good behavior. a referenced object shall *not* be deleted |
|
170
|
|
|
* when it no longer has any references! |
|
171
|
|
|
*/ |
|
172
|
|
|
|
|
173
|
|
|
// $this->assertEntityNotExists('one'); |
|
174
|
|
|
$this->assertEntityExists('one', 'one'); |
|
175
|
|
|
$this->assertEntityExists('two', 'two'); |
|
176
|
|
|
|
|
177
|
|
|
// ok. new entity was added |
|
178
|
|
|
$this->assertEntityExists('three', 'three'); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Test Document as deep embedded |
|
183
|
|
|
* |
|
184
|
|
|
* @return void |
|
185
|
|
|
*/ |
|
186
|
|
View Code Duplication |
public function testDocumentAsDeepEmbedded() |
|
|
|
|
|
|
187
|
|
|
{ |
|
188
|
|
|
$original = (object) [ |
|
189
|
|
|
'id' => 'test', |
|
190
|
|
|
'deep' => (object) [ |
|
191
|
|
|
'document' => (object) [ |
|
192
|
|
|
'id' => 'one', |
|
193
|
|
|
'data' => 'one', |
|
194
|
|
|
], |
|
195
|
|
|
], |
|
196
|
|
|
]; |
|
197
|
|
|
|
|
198
|
|
|
// check entities |
|
199
|
|
|
$this->assertEntityExists('one', 'one'); |
|
200
|
|
|
$this->assertEntityExists('two', 'two'); |
|
201
|
|
|
|
|
202
|
|
|
// check document |
|
203
|
|
|
$client = static::createRestClient(); |
|
204
|
|
|
$client->request('GET', '/testcase/embedtest-document-as-deep-embedded/test'); |
|
205
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
206
|
|
|
$this->assertEquals($original, $client->getResults()); |
|
207
|
|
|
|
|
208
|
|
|
// update document |
|
209
|
|
|
$data = $client->getResults(); |
|
210
|
|
|
$data->deep->document = (object) [ |
|
211
|
|
|
'id' => 'two', |
|
212
|
|
|
'data' => 'two', |
|
213
|
|
|
]; |
|
214
|
|
|
|
|
215
|
|
|
$client = static::createRestClient(); |
|
216
|
|
|
$client->put('/testcase/embedtest-document-as-deep-embedded/test', $data); |
|
217
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
|
218
|
|
|
|
|
219
|
|
|
// check data |
|
220
|
|
|
$client = static::createRestClient(); |
|
221
|
|
|
$client->request('GET', '/testcase/embedtest-document-as-deep-embedded/test'); |
|
222
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
223
|
|
|
$this->assertEquals($data, $client->getResults()); |
|
224
|
|
|
|
|
225
|
|
|
// check entities again |
|
226
|
|
|
$this->assertEntityExists('one', 'one'); |
|
227
|
|
|
$this->assertEntityExists('two', 'two'); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Assert that if we send a DELETE request to an entity, referenced objects will not be deleted |
|
232
|
|
|
* as well.. |
|
233
|
|
|
* |
|
234
|
|
|
* @return void |
|
235
|
|
|
*/ |
|
236
|
|
|
public function testThatReferencedObjectsWillNotBeDeletedOnDelete() |
|
237
|
|
|
{ |
|
238
|
|
|
$client = static::createRestClient(); |
|
239
|
|
|
$client->request('DELETE', '/testcase/embedtest-document-as-deep-reference/test'); |
|
240
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
|
241
|
|
|
$this->assertEmpty($client->getResults()); |
|
242
|
|
|
|
|
243
|
|
|
// check it's really gone ;-) |
|
244
|
|
|
$client = static::createRestClient(); |
|
245
|
|
|
$client->request('GET', '/testcase/embedtest-document-as-deep-reference/test'); |
|
246
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $client->getResponse()->getStatusCode()); |
|
247
|
|
|
|
|
248
|
|
|
// both entities should still exist |
|
249
|
|
|
$this->assertEntityExists('one', 'one'); |
|
250
|
|
|
$this->assertEntityExists('two', 'two'); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Test Document as deep reference |
|
255
|
|
|
* |
|
256
|
|
|
* @return void |
|
257
|
|
|
*/ |
|
258
|
|
|
public function testDocumentAsDeepReference() |
|
259
|
|
|
{ |
|
260
|
|
|
$original = (object) [ |
|
261
|
|
|
'id' => 'test', |
|
262
|
|
|
'deep' => (object) [ |
|
263
|
|
|
'document' => (object) [ |
|
264
|
|
|
'id' => 'one', |
|
265
|
|
|
'data' => 'one', |
|
266
|
|
|
], |
|
267
|
|
|
], |
|
268
|
|
|
]; |
|
269
|
|
|
|
|
270
|
|
|
// check entities |
|
271
|
|
|
$this->assertEntityExists('one', 'one'); |
|
272
|
|
|
$this->assertEntityExists('two', 'two'); |
|
273
|
|
|
|
|
274
|
|
|
// check document |
|
275
|
|
|
$client = static::createRestClient(); |
|
276
|
|
|
$client->request('GET', '/testcase/embedtest-document-as-deep-reference/test'); |
|
277
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
278
|
|
|
$this->assertEquals($original, $client->getResults()); |
|
279
|
|
|
|
|
280
|
|
|
// update document |
|
281
|
|
|
$data = $client->getResults(); |
|
282
|
|
|
$data->deep->document = (object) [ |
|
283
|
|
|
'id' => 'three', |
|
284
|
|
|
'data' => 'three', |
|
285
|
|
|
]; |
|
286
|
|
|
|
|
287
|
|
|
$client = static::createRestClient(); |
|
288
|
|
|
$client->put('/testcase/embedtest-document-as-deep-reference/test', $data); |
|
289
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
|
290
|
|
|
|
|
291
|
|
|
// check data |
|
292
|
|
|
$client = static::createRestClient(); |
|
293
|
|
|
$client->request('GET', '/testcase/embedtest-document-as-deep-reference/test'); |
|
294
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
295
|
|
|
$this->assertEquals($data, $client->getResults()); |
|
296
|
|
|
|
|
297
|
|
|
// check entities again |
|
298
|
|
|
// record "one" was *not* removed. |
|
299
|
|
|
$this->assertEntityExists('one', 'one'); |
|
300
|
|
|
$this->assertEntityExists('two', 'two'); |
|
301
|
|
|
$this->assertEntityExists('three', 'three'); |
|
302
|
|
|
|
|
303
|
|
|
// change to two again |
|
304
|
|
|
$data = $client->getResults(); |
|
305
|
|
|
$data->deep->document = (object) [ |
|
306
|
|
|
'id' => 'two', |
|
307
|
|
|
'data' => 'two', |
|
308
|
|
|
]; |
|
309
|
|
|
|
|
310
|
|
|
$client = static::createRestClient(); |
|
311
|
|
|
$client->put('/testcase/embedtest-document-as-deep-reference/test', $data); |
|
312
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
|
313
|
|
|
|
|
314
|
|
|
$client = static::createRestClient(); |
|
315
|
|
|
$client->request('GET', '/testcase/embedtest-document-as-deep-reference/test'); |
|
316
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
317
|
|
|
$this->assertEquals($data, $client->getResults()); |
|
318
|
|
|
|
|
319
|
|
|
// all still exist |
|
320
|
|
|
$this->assertEntityExists('one', 'one'); |
|
321
|
|
|
$this->assertEntityExists('two', 'two'); |
|
322
|
|
|
$this->assertEntityExists('three', 'three'); |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* Test Hash as embedded |
|
327
|
|
|
* |
|
328
|
|
|
* @return void |
|
329
|
|
|
*/ |
|
330
|
|
|
public function testHashAsEmbedded() |
|
331
|
|
|
{ |
|
332
|
|
|
$original = (object) [ |
|
333
|
|
|
'id' => 'test', |
|
334
|
|
|
'document' => (object) ['id' => 'one', 'data' => 'one'], |
|
335
|
|
|
'documents' => [ |
|
336
|
|
|
(object) ['id' => 'one', 'data' => 'one'], |
|
337
|
|
|
(object) ['id' => 'two', 'data' => 'two'], |
|
338
|
|
|
], |
|
339
|
|
|
]; |
|
340
|
|
|
|
|
341
|
|
|
// check entities |
|
342
|
|
|
$this->assertEntityExists('one', 'one'); |
|
343
|
|
|
$this->assertEntityExists('two', 'two'); |
|
344
|
|
|
|
|
345
|
|
|
// check document |
|
346
|
|
|
$client = static::createRestClient(); |
|
347
|
|
|
$client->request('GET', '/testcase/embedtest-hash-as-embedded/test'); |
|
348
|
|
|
|
|
349
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
350
|
|
|
$this->assertEquals($original, $client->getResults()); |
|
351
|
|
|
|
|
352
|
|
|
// update document |
|
353
|
|
|
$data = $client->getResults(); |
|
354
|
|
|
$data->document = (object) ['id' => 'two', 'data' => 'two']; |
|
355
|
|
|
$data->documents = [ |
|
356
|
|
|
(object) ['id' => 'three', 'data' => 'three'], |
|
357
|
|
|
]; |
|
358
|
|
|
|
|
359
|
|
|
$client = static::createRestClient(); |
|
360
|
|
|
$client->put('/testcase/embedtest-hash-as-embedded/test', $data); |
|
361
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
|
362
|
|
|
|
|
363
|
|
|
// check data |
|
364
|
|
|
$client = static::createRestClient(); |
|
365
|
|
|
$client->request('GET', '/testcase/embedtest-hash-as-embedded/test'); |
|
366
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
367
|
|
|
$this->assertEquals($data, $client->getResults()); |
|
368
|
|
|
|
|
369
|
|
|
// check entities again |
|
370
|
|
|
$this->assertEntityExists('one', 'one'); |
|
371
|
|
|
$this->assertEntityExists('two', 'two'); |
|
372
|
|
|
|
|
373
|
|
|
|
|
374
|
|
|
|
|
375
|
|
|
// update document with empty embed-many |
|
376
|
|
|
$data = $client->getResults(); |
|
377
|
|
|
$data->document = (object) ['id' => 'two', 'data' => 'two']; |
|
378
|
|
|
$data->documents = []; |
|
379
|
|
|
|
|
380
|
|
|
$client = static::createRestClient(); |
|
381
|
|
|
$client->put('/testcase/embedtest-hash-as-embedded/test', $data); |
|
382
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
|
383
|
|
|
|
|
384
|
|
|
// check data |
|
385
|
|
|
$client = static::createRestClient(); |
|
386
|
|
|
$client->request('GET', '/testcase/embedtest-hash-as-embedded/test'); |
|
387
|
|
|
$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
|
388
|
|
|
$this->assertEquals($data, $client->getResults()); |
|
389
|
|
|
|
|
390
|
|
|
// check entities again |
|
391
|
|
|
$this->assertEntityExists('one', 'one'); |
|
392
|
|
|
$this->assertEntityExists('two', 'two'); |
|
393
|
|
|
} |
|
394
|
|
|
} |
|
395
|
|
|
|
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.