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