1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mapado\RestClientSdk\Tests\Units; |
4
|
|
|
|
5
|
|
|
use atoum; |
6
|
|
|
use Mapado\RestClientSdk\Mapping as RestMapping; |
7
|
|
|
use Mapado\RestClientSdk\Mapping\Attribute; |
8
|
|
|
use Mapado\RestClientSdk\Mapping\ClassMetadata; |
9
|
|
|
use Mapado\RestClientSdk\Mapping\Driver\AnnotationDriver; |
10
|
|
|
use Mapado\RestClientSdk\UnitOfWork; |
|
|
|
|
11
|
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class EntityRepository |
15
|
|
|
* |
16
|
|
|
* @author Julien Deniau <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class EntityRepository extends atoum |
19
|
|
|
{ |
20
|
|
|
private $mockedRestClient; |
21
|
|
|
|
22
|
|
|
private $mockedSdk; |
23
|
|
|
|
24
|
|
|
private $mockedHydrator; |
25
|
|
|
|
26
|
|
|
private $repository; |
27
|
|
|
|
28
|
|
|
private $mapping; |
29
|
|
|
|
30
|
|
|
private $unitOfWork; |
31
|
|
|
|
32
|
|
|
public function beforeTestMethod($method) |
33
|
|
|
{ |
34
|
|
|
$this->mockGenerator->orphanize('__construct'); |
35
|
|
|
$this->mockedSdk = new \mock\Mapado\RestClientSdk\SdkClient(); |
|
|
|
|
36
|
|
|
$mockedHydrator = new \mock\Mapado\RestClientSdk\Model\ModelHydrator($this->mockedSdk); |
|
|
|
|
37
|
|
|
$this->calling($this->mockedSdk)->getModelHydrator = $mockedHydrator; |
38
|
|
|
|
39
|
|
|
$this->mockGenerator->orphanize('__construct'); |
40
|
|
|
$this->mockedRestClient = new \mock\Mapado\RestClientSdk\RestClient(); |
|
|
|
|
41
|
|
|
// $this->resetMock($this->mockedRestClient); |
42
|
|
|
|
43
|
|
|
$this->mockedHydrator = new \mock\Mapado\RestClientSdk\Model\ModelHydrator($this->mockedSdk); |
44
|
|
|
$this->calling($this->mockedSdk)->getModelHydrator = $this->mockedHydrator; |
45
|
|
|
|
46
|
|
|
$this->mapping = new RestMapping('v12'); |
47
|
|
|
$this->mapping->setMapping([ |
48
|
|
|
new ClassMetadata( |
49
|
|
|
'orders', |
50
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Model', |
51
|
|
|
'mock\Mapado\RestClientSdk\EntityRepository' |
52
|
|
|
), |
53
|
|
|
]); |
54
|
|
|
$this->unitOfWork = new UnitOfWork($this->mapping); |
55
|
|
|
|
56
|
|
|
$this->calling($this->mockedSdk)->getMapping = $this->mapping; |
57
|
|
|
|
58
|
|
|
$this->repository = new \mock\Mapado\RestClientSdk\EntityRepository( |
|
|
|
|
59
|
|
|
$this->mockedSdk, |
60
|
|
|
$this->mockedRestClient, |
61
|
|
|
$this->unitOfWork, |
62
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Model' |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* testFind |
68
|
|
|
*/ |
69
|
|
|
public function testFind() |
70
|
|
|
{ |
71
|
|
|
$this->calling($this->mockedRestClient)->get = []; |
72
|
|
|
|
73
|
|
|
$this |
74
|
|
|
->if($this->repository->find('1')) |
75
|
|
|
->then |
76
|
|
|
->mock($this->mockedRestClient) |
77
|
|
|
->call('get') |
78
|
|
|
->withArguments('v12/orders/1')->once() |
79
|
|
|
|
80
|
|
|
->given($this->resetMock($this->mockedRestClient)) |
81
|
|
|
->if($this->repository->find('v12/orders/999')) |
82
|
|
|
->then |
83
|
|
|
->mock($this->mockedRestClient) |
84
|
|
|
->call('get') |
85
|
|
|
->withArguments('v12/orders/999')->once() |
86
|
|
|
|
87
|
|
|
->if($this->repository->findAll()) |
88
|
|
|
->then |
89
|
|
|
->mock($this->mockedRestClient) |
90
|
|
|
->call('get') |
91
|
|
|
->withArguments('v12/orders')->once() |
92
|
|
|
|
93
|
|
|
->if($this->repository->findOneByFoo('bar')) |
94
|
|
|
->then |
95
|
|
|
->mock($this->mockedRestClient) |
96
|
|
|
->call('get') |
97
|
|
|
->withArguments('v12/orders?foo=bar')->once() |
98
|
|
|
->mock($this->mockedHydrator) |
99
|
|
|
->call('hydrate') |
100
|
|
|
->twice() |
101
|
|
|
|
102
|
|
|
->if($this->repository->findByFoo('baz')) |
103
|
|
|
->then |
104
|
|
|
->mock($this->mockedRestClient) |
105
|
|
|
->call('get') |
106
|
|
|
->withArguments('v12/orders?foo=baz')->once() |
107
|
|
|
->mock($this->mockedHydrator) |
108
|
|
|
->call('hydrateList') |
109
|
|
|
->twice() |
110
|
|
|
; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* testFindWithQueryParameters |
115
|
|
|
*/ |
116
|
|
|
public function testFindWithQueryParameters() |
117
|
|
|
{ |
118
|
|
|
$this->calling($this->mockedRestClient)->get = []; |
119
|
|
|
|
120
|
|
|
$this |
121
|
|
|
->if($this->repository->find('1', ['foo' => 'bar', 'bar' => 'baz'])) |
122
|
|
|
->then |
123
|
|
|
->mock($this->mockedRestClient) |
124
|
|
|
->call('get') |
125
|
|
|
->withArguments('v12/orders/1?foo=bar&bar=baz')->once() |
126
|
|
|
; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* testFindWithCache |
131
|
|
|
*/ |
132
|
|
|
public function testFindWithCache() |
133
|
|
|
{ |
134
|
|
|
$mockOrder1 = new \mock\entity(); |
|
|
|
|
135
|
|
|
$mockOrder2 = new \mock\entity(); |
136
|
|
|
$mockOrder3 = new \mock\entity(); |
137
|
|
|
$this->calling($mockOrder1)->getId = 'v12/orders/1'; |
138
|
|
|
$this->calling($mockOrder2)->getId = 'v12/orders/2'; |
139
|
|
|
$this->calling($mockOrder3)->getId = 'v12/orders/3'; |
140
|
|
|
|
141
|
|
|
$this->calling($this->mockedHydrator)->hydrate = $mockOrder1; |
142
|
|
|
$this->calling($this->mockedHydrator)->hydrateList = [$mockOrder1, $mockOrder2, $mockOrder3]; |
143
|
|
|
|
144
|
|
|
$arrayAdapter = new ArrayAdapter(0, false); |
145
|
|
|
$this->calling($this->mockedSdk)->getCacheItemPool = $arrayAdapter; |
146
|
|
|
$this->calling($this->mockedSdk)->getCachePrefix = 'test_prefix_'; |
147
|
|
|
|
148
|
|
|
$this->calling($this->mockedRestClient)->get = []; |
149
|
|
|
|
150
|
|
|
$this->calling($this->mockedHydrator)->convertId[0] = 'v12/orders/1'; |
151
|
|
|
$this->calling($this->mockedHydrator)->convertId[1] = 'v12/orders/1'; |
152
|
|
|
$this->calling($this->mockedHydrator)->convertId[4] = 'v12/orders/3'; |
153
|
|
|
|
154
|
|
|
$this |
155
|
|
|
->if($this->repository->find(1)) |
156
|
|
|
->and($this->repository->find(1)) |
157
|
|
|
->and($this->repository->find(1, ['foo' => 'bar'])) |
158
|
|
|
->then |
159
|
|
|
->mock($this->mockedRestClient) |
160
|
|
|
->call('get') |
161
|
|
|
->withArguments('v12/orders/1')->once() |
162
|
|
|
->call('get') |
163
|
|
|
->withArguments('v12/orders/1?foo=bar')->once() |
164
|
|
|
|
165
|
|
|
// find all |
166
|
|
|
->if($this->repository->findAll()) |
167
|
|
|
->and($this->repository->findAll()) |
168
|
|
|
->if($this->repository->find(3)) |
169
|
|
|
->then |
170
|
|
|
->mock($this->mockedRestClient) |
171
|
|
|
->call('get') |
172
|
|
|
->withArguments('v12/orders')->once() |
173
|
|
|
->call('get') |
174
|
|
|
->withArguments('v12/orders/3')->never() |
175
|
|
|
|
176
|
|
|
// find by |
177
|
|
|
->given($this->resetMock($this->mockedRestClient)) |
178
|
|
|
->and($this->mockedSdk->getCacheItemPool()->clear()) |
179
|
|
|
|
180
|
|
|
->if($this->repository->findBy(['foo' => 'bar', 'bar' => 'baz'])) |
181
|
|
|
->and($this->repository->findBy(['foo' => 'bar', 'bar' => 'baz'])) |
182
|
|
|
->if($this->repository->find(1)) |
183
|
|
|
->then |
184
|
|
|
->mock($this->mockedRestClient) |
185
|
|
|
->call('get') |
186
|
|
|
->withArguments('v12/orders?foo=bar&bar=baz')->once() |
187
|
|
|
->call('get') |
188
|
|
|
->withArguments('v12/orders/1')->never() |
189
|
|
|
|
190
|
|
|
// find by something |
191
|
|
|
->given($this->resetMock($this->mockedRestClient)) |
192
|
|
|
|
193
|
|
|
->if($this->repository->findByBar('baz')) |
194
|
|
|
->and($this->repository->findByBar('baz')) |
195
|
|
|
->and($this->repository->find(1)) |
196
|
|
|
->then |
197
|
|
|
->mock($this->mockedRestClient) |
198
|
|
|
->call('get') |
199
|
|
|
->withArguments('v12/orders?bar=baz')->once() |
200
|
|
|
->call('get') |
201
|
|
|
->withArguments('v12/orders/1')->never() |
202
|
|
|
|
203
|
|
|
// find one by |
204
|
|
|
->given($this->resetMock($this->mockedRestClient)) |
205
|
|
|
|
206
|
|
|
->if($this->repository->findOneBy(['foo' => 'baz', 'bar' => 'bar'])) |
207
|
|
|
->and($this->repository->findOneBy(['foo' => 'baz', 'bar' => 'bar'])) |
208
|
|
|
->then |
209
|
|
|
->mock($this->mockedRestClient) |
210
|
|
|
->call('get') |
211
|
|
|
->withArguments('v12/orders?foo=baz&bar=bar')->once() |
212
|
|
|
|
213
|
|
|
// find one by thing |
214
|
|
|
->given($this->resetMock($this->mockedRestClient)) |
215
|
|
|
|
216
|
|
|
->if($this->repository->findOneByFoo('bar')) |
217
|
|
|
->and($this->repository->findOneByFoo('bar')) |
218
|
|
|
->then |
219
|
|
|
->mock($this->mockedRestClient) |
220
|
|
|
->call('get') |
221
|
|
|
->withArguments('v12/orders?foo=bar')->once() |
222
|
|
|
|
223
|
|
|
// find one by with data already in cache |
224
|
|
|
->given($this->resetMock($this->mockedRestClient)) |
225
|
|
|
->if($this->repository->findOneBy(['foo' => 'bar', 'bar' => 'baz'])) |
226
|
|
|
->then |
227
|
|
|
->mock($this->mockedRestClient) |
228
|
|
|
->call('get') |
229
|
|
|
->withArguments('v12/orders?foo=bar&bar=baz')->never() |
230
|
|
|
; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* testClearCacheAfterUpdate |
235
|
|
|
*/ |
236
|
|
|
public function testClearCacheAfterUpdate() |
237
|
|
|
{ |
238
|
|
|
$mapping = new RestMapping('/v12'); |
239
|
|
|
$mapping->setMapping([ |
240
|
|
|
new ClassMetadata( |
241
|
|
|
'products', |
242
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Product', |
243
|
|
|
'mock\Mapado\RestClientSdk\EntityRepository' |
244
|
|
|
), |
245
|
|
|
]); |
246
|
|
|
|
247
|
|
|
$this->calling($this->mockedSdk)->getMapping = $mapping; |
248
|
|
|
$this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping, $this->unitOfWork); |
249
|
|
|
|
250
|
|
|
$product1 = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Product(); |
251
|
|
|
$product2 = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Product(); |
252
|
|
|
$product3 = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Product(); |
253
|
|
|
$product1->setId('/v12/products/1'); |
|
|
|
|
254
|
|
|
$product2->setId('/v12/products/2'); |
255
|
|
|
$product3->setId('/v12/products/3'); |
256
|
|
|
|
257
|
|
|
$this->calling($this->mockedHydrator)->hydrate = $product1; |
258
|
|
|
$this->calling($this->mockedHydrator)->hydrateList = [$product1, $product2, $product3]; |
259
|
|
|
|
260
|
|
|
$arrayAdapter = new ArrayAdapter(0, false); |
261
|
|
|
$this->calling($this->mockedSdk)->getCacheItemPool = $arrayAdapter; |
262
|
|
|
$this->calling($this->mockedSdk)->getCachePrefix = 'test_prefix_'; |
263
|
|
|
|
264
|
|
|
$this->calling($this->mockedRestClient)->get = $product1; |
265
|
|
|
$this->calling($this->mockedRestClient)->put = $product1; |
266
|
|
|
$this->calling($this->mockedRestClient)->delete = null; |
267
|
|
|
|
268
|
|
|
$repository = new \mock\Mapado\RestClientSdk\EntityRepository( |
269
|
|
|
$this->mockedSdk, |
270
|
|
|
$this->mockedRestClient, |
271
|
|
|
$this->unitOfWork, |
272
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Product' |
273
|
|
|
); |
274
|
|
|
|
275
|
|
|
$this |
276
|
|
|
->if($repository->find(1)) |
277
|
|
|
->then |
278
|
|
|
->boolean($arrayAdapter->hasItem('test_prefix__v12_products_1')) |
279
|
|
|
->isTrue() |
280
|
|
|
|
281
|
|
|
->if($repository->find(1)) |
282
|
|
|
->then |
283
|
|
|
->mock($this->mockedRestClient) |
284
|
|
|
->call('get') |
285
|
|
|
->withArguments('/v12/products/1')->once() |
286
|
|
|
|
287
|
|
|
// after update |
288
|
|
|
->if($repository->update($product1)) |
289
|
|
|
->boolean($arrayAdapter->hasItem('test_prefix__v12_products_1')) |
290
|
|
|
->isFalse() |
291
|
|
|
|
292
|
|
|
->if($repository->find(1)) |
293
|
|
|
->then |
294
|
|
|
->mock($this->mockedRestClient) |
295
|
|
|
->call('get') |
296
|
|
|
->withArguments('/v12/products/1')->twice() |
297
|
|
|
|
298
|
|
|
// after deletion |
299
|
|
|
->if($repository->remove($product1)) |
300
|
|
|
->then |
301
|
|
|
->boolean($arrayAdapter->hasItem('test_prefix__v12_products_1')) |
302
|
|
|
->isFalse() |
303
|
|
|
; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
public function testPutWithoutStore() |
307
|
|
|
{ |
308
|
|
|
$product1 = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Order(); |
309
|
|
|
$product1->setId('/v12/orders/1'); |
310
|
|
|
|
311
|
|
|
$this->calling($this->mockedHydrator)->hydrate = $product1; |
312
|
|
|
$this->calling($this->mockedHydrator)->hydrateList = [$product1]; |
313
|
|
|
|
314
|
|
|
$this->calling($this->mockedRestClient)->put = [$product1]; |
315
|
|
|
$this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($this->mapping, $this->unitOfWork); |
316
|
|
|
|
317
|
|
|
$this |
318
|
|
|
->given($updatedProduct = $this->repository->update($product1)) |
319
|
|
|
->then |
320
|
|
|
->object($updatedProduct) |
321
|
|
|
->isIdenticalTo($product1); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
public function testCacheWithIriAsId() |
325
|
|
|
{ |
326
|
|
|
$annotationDriver = new AnnotationDriver(__DIR__ . '/../cache/'); |
327
|
|
|
$mapping = new RestMapping(); |
328
|
|
|
$mapping->setMapping($annotationDriver->loadDirectory(__DIR__ . '/../Model/Issue46/')); |
329
|
|
|
|
330
|
|
|
$unitOfWork = new UnitOfWork($mapping); |
331
|
|
|
|
332
|
|
|
$this->calling($this->mockedSdk)->getMapping = $mapping; |
333
|
|
|
$this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping, $unitOfWork); |
334
|
|
|
|
335
|
|
|
$section1 = new \Mapado\RestClientSdk\Tests\Model\Issue46\Section(); |
336
|
|
|
$section1->setIri('/sections/1'); |
337
|
|
|
|
338
|
|
|
$this->calling($this->mockedHydrator)->hydrate = $section1; |
339
|
|
|
$this->calling($this->mockedHydrator)->hydrateList = [$section1]; |
340
|
|
|
|
341
|
|
|
$arrayAdapter = new ArrayAdapter(0, false); |
342
|
|
|
$this->calling($this->mockedSdk)->getCacheItemPool = $arrayAdapter; |
343
|
|
|
$this->calling($this->mockedSdk)->getCachePrefix = 'test_prefix_'; |
344
|
|
|
|
345
|
|
|
$this->calling($this->mockedRestClient)->get = $section1; |
346
|
|
|
$this->calling($this->mockedRestClient)->put = $section1; |
347
|
|
|
$this->calling($this->mockedRestClient)->delete = null; |
348
|
|
|
|
349
|
|
|
$repository = new \mock\Mapado\RestClientSdk\EntityRepository( |
350
|
|
|
$this->mockedSdk, |
351
|
|
|
$this->mockedRestClient, |
352
|
|
|
$unitOfWork, |
353
|
|
|
'Mapado\RestClientSdk\Tests\Model\Issue46\Section' |
354
|
|
|
); |
355
|
|
|
|
356
|
|
|
$this |
357
|
|
|
->if($repository->findBy(['section' => $section1])) |
358
|
|
|
->then |
359
|
|
|
->mock($this->mockedRestClient) |
360
|
|
|
->call('get') |
361
|
|
|
->withArguments('/sections?section=%2Fsections%2F1')->once() |
362
|
|
|
|
363
|
|
|
->if($repository->findAll()) |
364
|
|
|
->then |
365
|
|
|
->boolean($arrayAdapter->hasItem('test_prefix__sections_1')) |
366
|
|
|
->isTrue() |
367
|
|
|
|
368
|
|
|
->if($repository->find(1)) |
369
|
|
|
->then |
370
|
|
|
->mock($this->mockedRestClient) |
371
|
|
|
->call('get') |
372
|
|
|
->withArguments('/sections/1')->never() |
373
|
|
|
|
374
|
|
|
// after update |
375
|
|
|
->if($repository->update($section1)) |
376
|
|
|
->boolean($arrayAdapter->hasItem('test_prefix__sections_1')) |
377
|
|
|
->isFalse() |
378
|
|
|
->then |
379
|
|
|
->mock($this->mockedRestClient) |
380
|
|
|
->call('put') |
381
|
|
|
->withArguments('/sections/1')->once() |
382
|
|
|
|
383
|
|
|
->if($repository->find(1)) |
384
|
|
|
->then |
385
|
|
|
->mock($this->mockedRestClient) |
386
|
|
|
->call('get') |
387
|
|
|
->withArguments('/sections/1')->once() |
388
|
|
|
|
389
|
|
|
// after deletion |
390
|
|
|
->if($repository->remove($section1)) |
391
|
|
|
->then |
392
|
|
|
->boolean($arrayAdapter->hasItem('test_prefix__sections_1')) |
393
|
|
|
->isFalse() |
394
|
|
|
; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* testFindNotFound |
399
|
|
|
*/ |
400
|
|
|
public function testFindNotFound() |
401
|
|
|
{ |
402
|
|
|
$this->calling($this->mockedRestClient)->get = null; |
403
|
|
|
|
404
|
|
|
$this |
405
|
|
|
->variable($this->repository->find('1')) |
406
|
|
|
->isNull() |
407
|
|
|
; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
public function testFindOneByObject() |
411
|
|
|
{ |
412
|
|
|
$mapping = new RestMapping('v12'); |
413
|
|
|
$mapping->setMapping([ |
414
|
|
|
new ClassMetadata( |
415
|
|
|
'carts', |
416
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart', |
417
|
|
|
'mock\Mapado\RestClientSdk\EntityRepository' |
418
|
|
|
), |
419
|
|
|
new ClassMetadata( |
420
|
|
|
'cart_items', |
421
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem', |
422
|
|
|
'mock\Mapado\RestClientSdk\EntityRepository' |
423
|
|
|
), |
424
|
|
|
]); |
425
|
|
|
|
426
|
|
|
$this->calling($this->mockedSdk)->getMapping = $mapping; |
427
|
|
|
|
428
|
|
|
$this->calling($this->mockedRestClient)->get = []; |
429
|
|
|
|
430
|
|
|
$cartItemRepository = new \mock\Mapado\RestClientSdk\EntityRepository( |
431
|
|
|
$this->mockedSdk, |
432
|
|
|
$this->mockedRestClient, |
433
|
|
|
$this->unitOfWork, |
434
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem' |
435
|
|
|
); |
436
|
|
|
|
437
|
|
|
$cart = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Cart(); |
438
|
|
|
$cart->setId(1); |
439
|
|
|
|
440
|
|
|
$this |
441
|
|
|
->given($cart = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Cart()) |
442
|
|
|
->and($cart->setId(1)) |
443
|
|
|
->if($cartItemRepository->findOneByCart($cart)) |
444
|
|
|
->then |
445
|
|
|
->mock($this->mockedRestClient) |
446
|
|
|
->call('get') |
447
|
|
|
->withArguments('v12/cart_items?cart=1')->once() |
448
|
|
|
|
449
|
|
|
// test with unmapped class |
450
|
|
|
->given($cart = new \mock\stdClass()) |
|
|
|
|
451
|
|
|
->if($cartItemRepository->findOneByCart($cart)) |
452
|
|
|
->then |
453
|
|
|
->mock($this->mockedRestClient) |
454
|
|
|
->call('get') |
455
|
|
|
->withArguments('v12/cart_items?')->once() |
456
|
|
|
; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
public function testWithoutMappingPrefix() |
460
|
|
|
{ |
461
|
|
|
$mapping = new RestMapping('/v12'); |
|
|
|
|
462
|
|
|
$mapping = new RestMapping(); |
463
|
|
|
$mapping->setMapping([ |
464
|
|
|
new ClassMetadata( |
465
|
|
|
'carts', |
466
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart', |
467
|
|
|
'mock\Mapado\RestClientSdk\EntityRepository' |
468
|
|
|
), |
469
|
|
|
new ClassMetadata( |
470
|
|
|
'cart_items', |
471
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem', |
472
|
|
|
'mock\Mapado\RestClientSdk\EntityRepository' |
473
|
|
|
), |
474
|
|
|
]); |
475
|
|
|
|
476
|
|
|
$this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping, $this->unitOfWork); |
477
|
|
|
$this->calling($this->mockedSdk)->getMapping = $mapping; |
478
|
|
|
|
479
|
|
|
$this->calling($this->mockedRestClient)->get = []; |
480
|
|
|
$this->calling($this->mockedRestClient)->post = []; |
481
|
|
|
|
482
|
|
|
$cartItemRepository = new \mock\Mapado\RestClientSdk\EntityRepository( |
483
|
|
|
$this->mockedSdk, |
484
|
|
|
$this->mockedRestClient, |
485
|
|
|
$this->unitOfWork, |
486
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem' |
487
|
|
|
); |
488
|
|
|
|
489
|
|
|
$cart = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Cart(); |
490
|
|
|
$cart->setId(1); |
491
|
|
|
|
492
|
|
|
$this |
493
|
|
|
->if($cartItemRepository->find(1)) |
494
|
|
|
->then |
495
|
|
|
->mock($this->mockedRestClient) |
496
|
|
|
->call('get') |
497
|
|
|
->withArguments('/cart_items/1')->once() |
498
|
|
|
|
499
|
|
|
->if($cartItemRepository->findAll()) |
500
|
|
|
->then |
501
|
|
|
->mock($this->mockedRestClient) |
502
|
|
|
->call('get') |
503
|
|
|
->withArguments('/cart_items')->once() |
504
|
|
|
|
505
|
|
|
->if($cartItemRepository->findBy(['foo' => 'bar'])) |
506
|
|
|
->then |
507
|
|
|
->mock($this->mockedRestClient) |
508
|
|
|
->call('get') |
509
|
|
|
->withArguments('/cart_items?foo=bar')->once() |
510
|
|
|
|
511
|
|
|
->given($cartItem = new \mock\Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem()) |
|
|
|
|
512
|
|
|
->if($cartItemRepository->persist($cartItem)) |
513
|
|
|
->then |
514
|
|
|
->mock($this->mockedRestClient) |
515
|
|
|
->call('post') |
516
|
|
|
->withArguments('/cart_items')->once() |
517
|
|
|
; |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
public function testFindOneByWithHal() |
521
|
|
|
{ |
522
|
|
|
$mapping = new RestMapping('v12'); |
523
|
|
|
$classMetadata = new ClassMetadata( |
524
|
|
|
'orders', |
525
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Order', |
526
|
|
|
'mock\Mapado\RestClientSdk\EntityRepository' |
527
|
|
|
); |
528
|
|
|
$classMetadata->setAttributeList([ |
529
|
|
|
new Attribute('@id', 'id', 'string', true), |
530
|
|
|
]); |
531
|
|
|
$mapping->setMapping([$classMetadata]); |
532
|
|
|
|
533
|
|
|
$this->calling($this->mockedSdk)->getMapping = $mapping; |
534
|
|
|
|
535
|
|
|
$this->repository = new \mock\Mapado\RestClientSdk\EntityRepository( |
536
|
|
|
$this->mockedSdk, |
537
|
|
|
$this->mockedRestClient, |
538
|
|
|
$this->unitOfWork, |
539
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Order' |
540
|
|
|
); |
541
|
|
|
|
542
|
|
|
$mapping->setConfig([ |
543
|
|
|
'collectionKey' => 'fooList', |
544
|
|
|
]); |
545
|
|
|
$this->calling($this->mockedRestClient)->get = [ |
546
|
|
|
'fooList' => [ |
547
|
|
|
[ |
548
|
|
|
'@id' => '/orders/2', |
549
|
|
|
], |
550
|
|
|
], |
551
|
|
|
]; |
552
|
|
|
$this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping, $this->unitOfWork); |
553
|
|
|
|
554
|
|
|
$this |
555
|
|
|
->then |
556
|
|
|
->object($order = $this->repository->findOneBy(['a' => 'a'])) |
557
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\Order') |
558
|
|
|
->string($order->getId()) |
559
|
|
|
->isEqualTo('/orders/2') |
560
|
|
|
; |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
/** |
564
|
|
|
* testFindOneByWithoutResult |
565
|
|
|
*/ |
566
|
|
|
public function testFindOneByWithoutResult() |
567
|
|
|
{ |
568
|
|
|
$mapping = new RestMapping('v12'); |
569
|
|
|
$classMetadata = new ClassMetadata( |
570
|
|
|
'orders', |
571
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Order', |
572
|
|
|
'mock\Mapado\RestClientSdk\EntityRepository' |
573
|
|
|
); |
574
|
|
|
$classMetadata->setAttributeList([ |
575
|
|
|
new Attribute('@id', 'id', 'string', true), |
576
|
|
|
]); |
577
|
|
|
$mapping->setMapping([$classMetadata]); |
578
|
|
|
|
579
|
|
|
$this->calling($this->mockedSdk)->getMapping = $mapping; |
580
|
|
|
|
581
|
|
|
$this->repository = new \mock\Mapado\RestClientSdk\EntityRepository( |
582
|
|
|
$this->mockedSdk, |
583
|
|
|
$this->mockedRestClient, |
584
|
|
|
$this->unitOfWork, |
585
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Order' |
586
|
|
|
); |
587
|
|
|
|
588
|
|
|
$mapping->setConfig([ |
589
|
|
|
'collectionKey' => 'fooList', |
590
|
|
|
]); |
591
|
|
|
$this->calling($this->mockedRestClient)->get = [ |
592
|
|
|
'fooList' => [ |
593
|
|
|
], |
594
|
|
|
]; |
595
|
|
|
$this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping, $this->unitOfWork); |
596
|
|
|
|
597
|
|
|
$this |
598
|
|
|
->then |
599
|
|
|
->variable($order = $this->repository->findOneBy(['a' => 'a'])) |
600
|
|
|
->isNull() |
601
|
|
|
; |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
public function testPersistWithUnitOfWork() |
605
|
|
|
{ |
606
|
|
|
$annotationDriver = new AnnotationDriver(__DIR__ . '/../cache/'); |
607
|
|
|
$mapping = new RestMapping(); |
608
|
|
|
$mapping->setMapping($annotationDriver->loadDirectory(__DIR__ . '/../Model/JsonLd/')); |
609
|
|
|
|
610
|
|
|
$unitOfWork = $this->newMockInstance(UnitOfWork::class, null, null, [$mapping]); |
611
|
|
|
|
612
|
|
|
$this->calling($this->mockedSdk)->getMapping = $mapping; |
613
|
|
|
$this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping, $unitOfWork); |
614
|
|
|
|
615
|
|
|
$cartItemRepository = new \mock\Mapado\RestClientSdk\EntityRepository( |
|
|
|
|
616
|
|
|
$this->mockedSdk, |
617
|
|
|
$this->mockedRestClient, |
618
|
|
|
$unitOfWork, |
619
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem' |
620
|
|
|
); |
621
|
|
|
$cartRepository = new \mock\Mapado\RestClientSdk\EntityRepository( |
622
|
|
|
$this->mockedSdk, |
623
|
|
|
$this->mockedRestClient, |
624
|
|
|
$unitOfWork, |
625
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart' |
626
|
|
|
); |
627
|
|
|
|
628
|
|
|
$cart = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Cart(); |
629
|
|
|
$cart->setStatus('pending'); |
630
|
|
|
$cartItem = new \Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem(); |
631
|
|
|
$cartItem->setCart($cart); |
632
|
|
|
$cartItem->setAmount(0); |
633
|
|
|
$cartItem2 = new \Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem(); |
634
|
|
|
$cartItem2->setCart($cart); |
635
|
|
|
$cartItem2->setData(['foo' => 'bar']); |
636
|
|
|
|
637
|
|
|
$this->calling($this->mockedRestClient)->post = []; |
638
|
|
|
|
639
|
|
|
$this |
640
|
|
|
->if($cartRepository->persist($cart)) |
641
|
|
|
->then |
642
|
|
|
->mock($this->mockedRestClient) |
643
|
|
|
->call('post') |
644
|
|
|
->withArguments( |
645
|
|
|
'/cart', |
646
|
|
|
[ |
647
|
|
|
'status' => 'pending', |
648
|
|
|
'cart_items' => [ |
649
|
|
|
[ |
650
|
|
|
'amount' => 0, |
651
|
|
|
'data' => [], |
652
|
|
|
], |
653
|
|
|
[ |
654
|
|
|
'data' => ['foo' => 'bar'], |
655
|
|
|
], |
656
|
|
|
], |
657
|
|
|
] |
658
|
|
|
) |
659
|
|
|
->once() |
660
|
|
|
; |
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
public function testUpdatingInstanceDoesGetDataFromUnitOfWork() |
664
|
|
|
{ |
665
|
|
|
$annotationDriver = new AnnotationDriver(__DIR__ . '/../cache/'); |
666
|
|
|
$mapping = new RestMapping(); |
667
|
|
|
$mapping->setMapping($annotationDriver->loadDirectory(__DIR__ . '/../Model/JsonLd/')); |
668
|
|
|
|
669
|
|
|
$unitOfWork = $this->newMockInstance(UnitOfWork::class, null, null, [$mapping]); |
670
|
|
|
|
671
|
|
|
$this->calling($this->mockedSdk)->getMapping = $mapping; |
672
|
|
|
$this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping, $unitOfWork); |
673
|
|
|
|
674
|
|
|
$cartRepository = new \mock\Mapado\RestClientSdk\EntityRepository( |
675
|
|
|
$this->mockedSdk, |
676
|
|
|
$this->mockedRestClient, |
677
|
|
|
$unitOfWork, |
678
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart' |
679
|
|
|
); |
680
|
|
|
|
681
|
|
|
$this->calling($this->mockedRestClient)->get = [ |
682
|
|
|
'id' => '/v1/carts/1', |
683
|
|
|
'status' => 'pending', |
684
|
|
|
'created_at' => '2019-01-01', |
685
|
|
|
]; |
686
|
|
|
|
687
|
|
|
$this->calling($this->mockedRestClient)->put = [ |
688
|
|
|
'id' => '/v1/carts/1', |
689
|
|
|
'status' => 'payed', |
690
|
|
|
'created_at' => '2019-01-01', |
691
|
|
|
]; |
692
|
|
|
|
693
|
|
|
$this |
694
|
|
|
->if($cart = $cartRepository->find('/v1/carts/1')) |
695
|
|
|
->then |
696
|
|
|
->mock($this->mockedRestClient) |
697
|
|
|
->call('get') |
698
|
|
|
->withArguments( |
699
|
|
|
'/v1/carts/1' |
700
|
|
|
) |
701
|
|
|
->once() |
702
|
|
|
->mock($unitOfWork) |
703
|
|
|
->call('registerClean') |
704
|
|
|
->withArguments('/v1/carts/1')->exactly(2) |
705
|
|
|
->withAnyArguments()->exactly(2) |
706
|
|
|
|
707
|
|
|
->if($cart->setStatus('payed')) |
708
|
|
|
->if($cartRepository->update($cart)) |
709
|
|
|
->then |
710
|
|
|
->mock($this->mockedRestClient) |
711
|
|
|
->call('put') |
712
|
|
|
->withArguments( |
713
|
|
|
'/v1/carts/1', |
714
|
|
|
['status' => 'payed'] |
715
|
|
|
) |
716
|
|
|
->once() |
717
|
|
|
->mock($unitOfWork) |
718
|
|
|
->call('registerClean') |
719
|
|
|
->withArguments('/v1/carts/1')->exactly(4) |
720
|
|
|
->withAnyArguments()->exactly(4) |
721
|
|
|
; |
722
|
|
|
} |
723
|
|
|
|
724
|
|
|
public function testUpdatingInstanceDoesGetDataFromUnitOfWorkWithQueryParam() |
725
|
|
|
{ |
726
|
|
|
$annotationDriver = new AnnotationDriver(__DIR__ . '/../cache/'); |
727
|
|
|
$mapping = new RestMapping(); |
728
|
|
|
$mapping->setMapping($annotationDriver->loadDirectory(__DIR__ . '/../Model/JsonLd/')); |
729
|
|
|
|
730
|
|
|
$unitOfWork = $this->newMockInstance(UnitOfWork::class, null, null, [$mapping]); |
731
|
|
|
|
732
|
|
|
$this->calling($this->mockedSdk)->getMapping = $mapping; |
733
|
|
|
$this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping, $unitOfWork); |
734
|
|
|
|
735
|
|
|
$cartRepository = new \mock\Mapado\RestClientSdk\EntityRepository( |
736
|
|
|
$this->mockedSdk, |
737
|
|
|
$this->mockedRestClient, |
738
|
|
|
$unitOfWork, |
739
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart' |
740
|
|
|
); |
741
|
|
|
|
742
|
|
|
$this->calling($this->mockedRestClient)->get = [ |
743
|
|
|
'id' => '/v1/carts/1', |
744
|
|
|
'status' => 'pending', |
745
|
|
|
]; |
746
|
|
|
|
747
|
|
|
$this->calling($this->mockedRestClient)->put = [ |
748
|
|
|
'id' => '/v1/carts/1', |
749
|
|
|
'status' => 'payed', |
750
|
|
|
]; |
751
|
|
|
|
752
|
|
|
$this |
753
|
|
|
->if($cart = $cartRepository->find('/v1/carts/1', ['fields' => 'id,status'])) |
754
|
|
|
->then |
755
|
|
|
->mock($this->mockedRestClient) |
756
|
|
|
->call('get') |
757
|
|
|
->withArguments( |
758
|
|
|
'/v1/carts/1?fields=id%2Cstatus' |
759
|
|
|
) |
760
|
|
|
->once() |
761
|
|
|
->mock($unitOfWork) |
762
|
|
|
->call('registerClean') |
763
|
|
|
->withArguments('/v1/carts/1')->exactly(1) |
764
|
|
|
->withArguments('/v1/carts/1?fields=id%2Cstatus')->exactly(1) |
765
|
|
|
->withAnyArguments()->exactly(2) |
766
|
|
|
|
767
|
|
|
->if($cart->setStatus('payed')) |
768
|
|
|
->if($cartRepository->update($cart, [], ['fields' => 'id'])) |
769
|
|
|
->then |
770
|
|
|
->mock($this->mockedRestClient) |
771
|
|
|
->call('put') |
772
|
|
|
->withArguments( |
773
|
|
|
'/v1/carts/1?fields=id', |
774
|
|
|
['status' => 'payed'] |
775
|
|
|
) |
776
|
|
|
->once() |
777
|
|
|
->mock($unitOfWork) |
778
|
|
|
->call('registerClean') |
779
|
|
|
->withArguments('/v1/carts/1')->exactly(3) |
780
|
|
|
->withArguments('/v1/carts/1?fields=id%2Cstatus')->exactly(1) |
781
|
|
|
->withAnyArguments()->exactly(4) |
782
|
|
|
; |
783
|
|
|
} |
784
|
|
|
} |
785
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: