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\UnitOfWork; |
|
|
|
|
9
|
|
|
use Mapado\RestClientSdk\Mapping\ClassMetadata; |
10
|
|
|
use Mapado\RestClientSdk\Mapping\Driver\AnnotationDriver; |
11
|
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class EntityRepository |
15
|
|
|
* @author Julien Deniau <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class EntityRepository extends atoum |
18
|
|
|
{ |
19
|
|
|
private $mockedRestClient; |
20
|
|
|
|
21
|
|
|
private $mockedSdk; |
22
|
|
|
|
23
|
|
|
private $mockedHydrator; |
24
|
|
|
|
25
|
|
|
private $repository; |
26
|
|
|
|
27
|
|
|
private $mapping; |
28
|
|
|
|
29
|
|
|
private $unitOfWork; |
30
|
|
|
|
31
|
|
|
public function beforeTestMethod($method) |
32
|
|
|
{ |
33
|
|
|
$this->mockGenerator->orphanize('__construct'); |
34
|
|
|
$this->mockedSdk = new \mock\Mapado\RestClientSdk\SdkClient(); |
35
|
|
|
$mockedHydrator = new \mock\Mapado\RestClientSdk\Model\ModelHydrator($this->mockedSdk); |
36
|
|
|
$this->calling($this->mockedSdk)->getModelHydrator = $mockedHydrator; |
37
|
|
|
|
38
|
|
|
$this->mockGenerator->orphanize('__construct'); |
39
|
|
|
$this->mockedRestClient = new \mock\Mapado\RestClientSdk\RestClient(); |
40
|
|
|
// $this->resetMock($this->mockedRestClient); |
|
|
|
|
41
|
|
|
|
42
|
|
|
$this->mockedHydrator = new \mock\Mapado\RestClientSdk\Model\ModelHydrator($this->mockedSdk); |
43
|
|
|
$this->calling($this->mockedSdk)->getModelHydrator = $this->mockedHydrator; |
44
|
|
|
|
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
|
|
|
* @access public |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
public function testFind() |
73
|
|
|
{ |
74
|
|
|
$this->calling($this->mockedRestClient)->get = []; |
75
|
|
|
|
76
|
|
|
$this |
77
|
|
|
->if($this->repository->find('1')) |
78
|
|
|
->then |
79
|
|
|
->mock($this->mockedRestClient) |
80
|
|
|
->call('get') |
81
|
|
|
->withArguments('v12/orders/1')->once() |
82
|
|
|
|
83
|
|
|
->given($this->resetMock($this->mockedRestClient)) |
84
|
|
|
->if($this->repository->find('v12/orders/999')) |
85
|
|
|
->then |
86
|
|
|
->mock($this->mockedRestClient) |
87
|
|
|
->call('get') |
88
|
|
|
->withArguments('v12/orders/999')->once() |
89
|
|
|
|
90
|
|
|
->if($this->repository->findAll()) |
91
|
|
|
->then |
92
|
|
|
->mock($this->mockedRestClient) |
93
|
|
|
->call('get') |
94
|
|
|
->withArguments('v12/orders')->once() |
95
|
|
|
|
96
|
|
|
->if($this->repository->findOneByFoo('bar')) |
97
|
|
|
->then |
98
|
|
|
->mock($this->mockedRestClient) |
99
|
|
|
->call('get') |
100
|
|
|
->withArguments('v12/orders?foo=bar')->once() |
101
|
|
|
->mock($this->mockedHydrator) |
102
|
|
|
->call('hydrate') |
103
|
|
|
->twice() |
104
|
|
|
|
105
|
|
|
->if($this->repository->findByFoo('baz')) |
106
|
|
|
->then |
107
|
|
|
->mock($this->mockedRestClient) |
108
|
|
|
->call('get') |
109
|
|
|
->withArguments('v12/orders?foo=baz')->once() |
110
|
|
|
->mock($this->mockedHydrator) |
111
|
|
|
->call('hydrateList') |
112
|
|
|
->twice() |
113
|
|
|
; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* testFindWithQueryParameters |
118
|
|
|
* |
119
|
|
|
* @access public |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
public function testFindWithQueryParameters() |
123
|
|
|
{ |
124
|
|
|
$this->calling($this->mockedRestClient)->get = []; |
125
|
|
|
|
126
|
|
|
$this |
127
|
|
|
->if($this->repository->find('1', [ 'foo' => 'bar', 'bar' => 'baz' ])) |
128
|
|
|
->then |
129
|
|
|
->mock($this->mockedRestClient) |
130
|
|
|
->call('get') |
131
|
|
|
->withArguments('v12/orders/1?foo=bar&bar=baz')->once() |
132
|
|
|
; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* testFindWithCache |
137
|
|
|
* |
138
|
|
|
* @access public |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
|
|
public function testFindWithCache() |
142
|
|
|
{ |
143
|
|
|
$mockOrder1 = new \mock\entity; |
144
|
|
|
$mockOrder2 = new \mock\entity; |
145
|
|
|
$mockOrder3 = new \mock\entity; |
146
|
|
|
$this->calling($mockOrder1)->getId = 'v12/orders/1'; |
147
|
|
|
$this->calling($mockOrder2)->getId = 'v12/orders/2'; |
148
|
|
|
$this->calling($mockOrder3)->getId = 'v12/orders/3'; |
149
|
|
|
|
150
|
|
|
$this->calling($this->mockedHydrator)->hydrate = $mockOrder1; |
151
|
|
|
$this->calling($this->mockedHydrator)->hydrateList = [$mockOrder1, $mockOrder2, $mockOrder3]; |
152
|
|
|
|
153
|
|
|
$arrayAdapter = new ArrayAdapter(0, false); |
154
|
|
|
$this->calling($this->mockedSdk)->getCacheItemPool = $arrayAdapter; |
155
|
|
|
$this->calling($this->mockedSdk)->getCachePrefix = 'test_prefix_'; |
156
|
|
|
|
157
|
|
|
$this->calling($this->mockedRestClient)->get = []; |
158
|
|
|
|
159
|
|
|
$this->calling($this->mockedHydrator)->convertId[0] = 'v12/orders/1'; |
160
|
|
|
$this->calling($this->mockedHydrator)->convertId[1] = 'v12/orders/1'; |
161
|
|
|
$this->calling($this->mockedHydrator)->convertId[4] = 'v12/orders/3'; |
162
|
|
|
|
163
|
|
|
$this |
164
|
|
|
->if($this->repository->find(1)) |
165
|
|
|
->and($this->repository->find(1)) |
166
|
|
|
->and($this->repository->find(1, ['foo' => 'bar'])) |
167
|
|
|
->then |
168
|
|
|
->mock($this->mockedRestClient) |
169
|
|
|
->call('get') |
170
|
|
|
->withArguments('v12/orders/1')->once() |
171
|
|
|
->call('get') |
172
|
|
|
->withArguments('v12/orders/1?foo=bar')->once() |
173
|
|
|
|
174
|
|
|
// find all |
175
|
|
|
->if($this->repository->findAll()) |
176
|
|
|
->and($this->repository->findAll()) |
177
|
|
|
->if($this->repository->find(3)) |
178
|
|
|
->then |
179
|
|
|
->mock($this->mockedRestClient) |
180
|
|
|
->call('get') |
181
|
|
|
->withArguments('v12/orders')->once() |
182
|
|
|
->call('get') |
183
|
|
|
->withArguments('v12/orders/3')->never() |
184
|
|
|
|
185
|
|
|
// find by |
186
|
|
|
->given($this->resetMock($this->mockedRestClient)) |
187
|
|
|
->and($this->mockedSdk->getCacheItemPool()->clear()) |
188
|
|
|
|
189
|
|
|
->if($this->repository->findBy([ 'foo' => 'bar', 'bar' => 'baz' ])) |
190
|
|
|
->and($this->repository->findBy([ 'foo' => 'bar', 'bar' => 'baz' ])) |
191
|
|
|
->if($this->repository->find(1)) |
192
|
|
|
->then |
193
|
|
|
->mock($this->mockedRestClient) |
194
|
|
|
->call('get') |
195
|
|
|
->withArguments('v12/orders?foo=bar&bar=baz')->once() |
196
|
|
|
->call('get') |
197
|
|
|
->withArguments('v12/orders/1')->never() |
198
|
|
|
|
199
|
|
|
// find by something |
200
|
|
|
->given($this->resetMock($this->mockedRestClient)) |
201
|
|
|
|
202
|
|
|
->if($this->repository->findByBar('baz')) |
203
|
|
|
->and($this->repository->findByBar('baz')) |
204
|
|
|
->and($this->repository->find(1)) |
205
|
|
|
->then |
206
|
|
|
->mock($this->mockedRestClient) |
207
|
|
|
->call('get') |
208
|
|
|
->withArguments('v12/orders?bar=baz')->once() |
209
|
|
|
->call('get') |
210
|
|
|
->withArguments('v12/orders/1')->never() |
211
|
|
|
|
212
|
|
|
// find one by |
213
|
|
|
->given($this->resetMock($this->mockedRestClient)) |
214
|
|
|
|
215
|
|
|
->if($this->repository->findOneBy([ 'foo' => 'baz', 'bar' => 'bar' ])) |
216
|
|
|
->and($this->repository->findOneBy([ 'foo' => 'baz', 'bar' => 'bar' ])) |
217
|
|
|
->then |
218
|
|
|
->mock($this->mockedRestClient) |
219
|
|
|
->call('get') |
220
|
|
|
->withArguments('v12/orders?foo=baz&bar=bar')->once() |
221
|
|
|
|
222
|
|
|
// find one by thing |
223
|
|
|
->given($this->resetMock($this->mockedRestClient)) |
224
|
|
|
|
225
|
|
|
->if($this->repository->findOneByFoo('bar')) |
226
|
|
|
->and($this->repository->findOneByFoo('bar')) |
227
|
|
|
->then |
228
|
|
|
->mock($this->mockedRestClient) |
229
|
|
|
->call('get') |
230
|
|
|
->withArguments('v12/orders?foo=bar')->once() |
231
|
|
|
|
232
|
|
|
// find one by with data already in cache |
233
|
|
|
->given($this->resetMock($this->mockedRestClient)) |
234
|
|
|
->if($this->repository->findOneBy([ 'foo' => 'bar', 'bar' => 'baz' ])) |
235
|
|
|
->then |
236
|
|
|
->mock($this->mockedRestClient) |
237
|
|
|
->call('get') |
238
|
|
|
->withArguments('v12/orders?foo=bar&bar=baz')->never() |
239
|
|
|
; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* testClearCacheAfterUpdate |
244
|
|
|
* |
245
|
|
|
* @access public |
246
|
|
|
* |
247
|
|
|
* @return void |
248
|
|
|
*/ |
249
|
|
|
public function testClearCacheAfterUpdate() |
250
|
|
|
{ |
251
|
|
|
$mapping = new RestMapping('/v12'); |
252
|
|
|
$mapping->setMapping([ |
253
|
|
|
new ClassMetadata( |
254
|
|
|
'products', |
255
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Product', |
256
|
|
|
'mock\Mapado\RestClientSdk\EntityRepository' |
257
|
|
|
), |
258
|
|
|
]); |
259
|
|
|
|
260
|
|
|
$this->calling($this->mockedSdk)->getMapping = $mapping; |
261
|
|
|
$this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping, $this->unitOfWork); |
262
|
|
|
|
263
|
|
|
|
264
|
|
|
$product1 = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Product; |
265
|
|
|
$product2 = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Product; |
266
|
|
|
$product3 = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Product; |
267
|
|
|
$product1->setId('/v12/products/1'); |
268
|
|
|
$product2->setId('/v12/products/2'); |
269
|
|
|
$product3->setId('/v12/products/3'); |
270
|
|
|
|
271
|
|
|
$this->calling($this->mockedHydrator)->hydrate = $product1; |
272
|
|
|
$this->calling($this->mockedHydrator)->hydrateList = [$product1, $product2, $product3]; |
273
|
|
|
|
274
|
|
|
$arrayAdapter = new ArrayAdapter(0, false); |
275
|
|
|
$this->calling($this->mockedSdk)->getCacheItemPool = $arrayAdapter; |
276
|
|
|
$this->calling($this->mockedSdk)->getCachePrefix = 'test_prefix_'; |
277
|
|
|
|
278
|
|
|
$this->calling($this->mockedRestClient)->get = $product1; |
279
|
|
|
$this->calling($this->mockedRestClient)->put = $product1; |
280
|
|
|
$this->calling($this->mockedRestClient)->delete = null; |
281
|
|
|
|
282
|
|
|
$repository = new \mock\Mapado\RestClientSdk\EntityRepository( |
283
|
|
|
$this->mockedSdk, |
284
|
|
|
$this->mockedRestClient, |
285
|
|
|
$this->unitOfWork, |
286
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Product' |
287
|
|
|
); |
288
|
|
|
|
289
|
|
|
$this |
290
|
|
|
->if($repository->find(1)) |
291
|
|
|
->then |
292
|
|
|
->boolean($arrayAdapter->hasItem('test_prefix__v12_products_1')) |
293
|
|
|
->isTrue() |
294
|
|
|
|
295
|
|
|
->if($repository->find(1)) |
296
|
|
|
->then |
297
|
|
|
->mock($this->mockedRestClient) |
298
|
|
|
->call('get') |
299
|
|
|
->withArguments('/v12/products/1')->once() |
300
|
|
|
|
301
|
|
|
// after update |
302
|
|
|
->if($repository->update($product1)) |
303
|
|
|
->boolean($arrayAdapter->hasItem('test_prefix__v12_products_1')) |
304
|
|
|
->isFalse() |
305
|
|
|
|
306
|
|
|
->if($repository->find(1)) |
307
|
|
|
->then |
308
|
|
|
->mock($this->mockedRestClient) |
309
|
|
|
->call('get') |
310
|
|
|
->withArguments('/v12/products/1')->twice() |
311
|
|
|
|
312
|
|
|
// after deletion |
313
|
|
|
->if($repository->remove($product1)) |
314
|
|
|
->then |
315
|
|
|
->boolean($arrayAdapter->hasItem('test_prefix__v12_products_1')) |
316
|
|
|
->isFalse() |
317
|
|
|
; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
public function testPutWithoutStore() |
321
|
|
|
{ |
322
|
|
|
$product1 = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Order; |
323
|
|
|
$product1->setId('/v12/orders/1'); |
324
|
|
|
|
325
|
|
|
$this->calling($this->mockedHydrator)->hydrate = $product1; |
326
|
|
|
$this->calling($this->mockedHydrator)->hydrateList = [$product1]; |
327
|
|
|
|
328
|
|
|
$this->calling($this->mockedRestClient)->put = [$product1]; |
329
|
|
|
$this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($this->mapping, $this->unitOfWork); |
330
|
|
|
|
331
|
|
|
$this |
332
|
|
|
->given($updatedProduct = $this->repository->update($product1)) |
333
|
|
|
->then |
334
|
|
|
->object($updatedProduct) |
335
|
|
|
->isIdenticalTo($product1); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
public function testCacheWithIriAsId() |
339
|
|
|
{ |
340
|
|
|
$annotationDriver = new AnnotationDriver(__DIR__ . '/../cache/'); |
341
|
|
|
$mapping = new RestMapping(); |
342
|
|
|
$mapping->setMapping($annotationDriver->loadDirectory(__DIR__ . '/../Model/Issue46/')); |
343
|
|
|
|
344
|
|
|
$unitOfWork = new UnitOfWork($mapping); |
345
|
|
|
|
346
|
|
|
$this->calling($this->mockedSdk)->getMapping = $mapping; |
347
|
|
|
$this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping, $unitOfWork); |
348
|
|
|
|
349
|
|
|
$section1 = new \Mapado\RestClientSdk\Tests\Model\Issue46\Section; |
350
|
|
|
$section1->setIri('/sections/1'); |
351
|
|
|
|
352
|
|
|
$this->calling($this->mockedHydrator)->hydrate = $section1; |
353
|
|
|
$this->calling($this->mockedHydrator)->hydrateList = [$section1]; |
354
|
|
|
|
355
|
|
|
$arrayAdapter = new ArrayAdapter(0, false); |
356
|
|
|
$this->calling($this->mockedSdk)->getCacheItemPool = $arrayAdapter; |
357
|
|
|
$this->calling($this->mockedSdk)->getCachePrefix = 'test_prefix_'; |
358
|
|
|
|
359
|
|
|
$this->calling($this->mockedRestClient)->get = $section1; |
360
|
|
|
$this->calling($this->mockedRestClient)->put = $section1; |
361
|
|
|
$this->calling($this->mockedRestClient)->delete = null; |
362
|
|
|
|
363
|
|
|
$repository = new \mock\Mapado\RestClientSdk\EntityRepository( |
364
|
|
|
$this->mockedSdk, |
365
|
|
|
$this->mockedRestClient, |
366
|
|
|
$unitOfWork, |
367
|
|
|
'Mapado\RestClientSdk\Tests\Model\Issue46\Section' |
368
|
|
|
); |
369
|
|
|
|
370
|
|
|
$this |
371
|
|
|
->if($repository->findBy(['section' => $section1])) |
372
|
|
|
->then |
373
|
|
|
->mock($this->mockedRestClient) |
374
|
|
|
->call('get') |
375
|
|
|
->withArguments('/sections?section=%2Fsections%2F1')->once() |
376
|
|
|
|
377
|
|
|
->if($repository->findAll()) |
378
|
|
|
->then |
379
|
|
|
->boolean($arrayAdapter->hasItem('test_prefix__sections_1')) |
380
|
|
|
->isTrue() |
381
|
|
|
|
382
|
|
|
->if($repository->find(1)) |
383
|
|
|
->then |
384
|
|
|
->mock($this->mockedRestClient) |
385
|
|
|
->call('get') |
386
|
|
|
->withArguments('/sections/1')->never() |
387
|
|
|
|
388
|
|
|
// after update |
389
|
|
|
->if($repository->update($section1)) |
390
|
|
|
->boolean($arrayAdapter->hasItem('test_prefix__sections_1')) |
391
|
|
|
->isFalse() |
392
|
|
|
->then |
393
|
|
|
->mock($this->mockedRestClient) |
394
|
|
|
->call('put') |
395
|
|
|
->withArguments('/sections/1')->once() |
396
|
|
|
|
397
|
|
|
->if($repository->find(1)) |
398
|
|
|
->then |
399
|
|
|
->mock($this->mockedRestClient) |
400
|
|
|
->call('get') |
401
|
|
|
->withArguments('/sections/1')->once() |
402
|
|
|
|
403
|
|
|
// after deletion |
404
|
|
|
->if($repository->remove($section1)) |
405
|
|
|
->then |
406
|
|
|
->boolean($arrayAdapter->hasItem('test_prefix__sections_1')) |
407
|
|
|
->isFalse() |
408
|
|
|
; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* testFindNotFound |
413
|
|
|
* |
414
|
|
|
* @access public |
415
|
|
|
* @return void |
416
|
|
|
*/ |
417
|
|
|
public function testFindNotFound() |
418
|
|
|
{ |
419
|
|
|
$this->calling($this->mockedRestClient)->get = null; |
420
|
|
|
|
421
|
|
|
$this |
422
|
|
|
->variable($this->repository->find('1')) |
423
|
|
|
->isNull() |
424
|
|
|
; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
public function testFindOneByObject() |
428
|
|
|
{ |
429
|
|
|
$mapping = new RestMapping('v12'); |
430
|
|
|
$mapping->setMapping([ |
431
|
|
|
new ClassMetadata( |
432
|
|
|
'carts', |
433
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart', |
434
|
|
|
'mock\Mapado\RestClientSdk\EntityRepository' |
435
|
|
|
), |
436
|
|
|
new ClassMetadata( |
437
|
|
|
'cart_items', |
438
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem', |
439
|
|
|
'mock\Mapado\RestClientSdk\EntityRepository' |
440
|
|
|
), |
441
|
|
|
]); |
442
|
|
|
|
443
|
|
|
$this->calling($this->mockedSdk)->getMapping = $mapping; |
444
|
|
|
|
445
|
|
|
$this->calling($this->mockedRestClient)->get = []; |
446
|
|
|
|
447
|
|
|
$cartItemRepository = new \mock\Mapado\RestClientSdk\EntityRepository( |
448
|
|
|
$this->mockedSdk, |
449
|
|
|
$this->mockedRestClient, |
450
|
|
|
$this->unitOfWork, |
451
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem' |
452
|
|
|
); |
453
|
|
|
|
454
|
|
|
|
455
|
|
|
$cart = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Cart; |
456
|
|
|
$cart->setId(1); |
457
|
|
|
|
458
|
|
|
$this |
459
|
|
|
->given($cart = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Cart) |
460
|
|
|
->and($cart->setId(1)) |
461
|
|
|
->if($cartItemRepository->findOneByCart($cart)) |
462
|
|
|
->then |
463
|
|
|
->mock($this->mockedRestClient) |
464
|
|
|
->call('get') |
465
|
|
|
->withArguments('v12/cart_items?cart=1')->once() |
466
|
|
|
|
467
|
|
|
// test with unmapped class |
468
|
|
|
->given($cart = new \mock\stdClass) |
469
|
|
|
->if($cartItemRepository->findOneByCart($cart)) |
470
|
|
|
->then |
471
|
|
|
->mock($this->mockedRestClient) |
472
|
|
|
->call('get') |
473
|
|
|
->withArguments('v12/cart_items?')->once() |
474
|
|
|
; |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
public function testWithoutMappingPrefix() |
478
|
|
|
{ |
479
|
|
|
$mapping = new RestMapping('/v12'); |
|
|
|
|
480
|
|
|
$mapping = new RestMapping(); |
481
|
|
|
$mapping->setMapping([ |
482
|
|
|
new ClassMetadata( |
483
|
|
|
'carts', |
484
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart', |
485
|
|
|
'mock\Mapado\RestClientSdk\EntityRepository' |
486
|
|
|
), |
487
|
|
|
new ClassMetadata( |
488
|
|
|
'cart_items', |
489
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem', |
490
|
|
|
'mock\Mapado\RestClientSdk\EntityRepository' |
491
|
|
|
), |
492
|
|
|
]); |
493
|
|
|
|
494
|
|
|
$this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping, $this->unitOfWork); |
495
|
|
|
$this->calling($this->mockedSdk)->getMapping = $mapping; |
496
|
|
|
|
497
|
|
|
$this->calling($this->mockedRestClient)->get = []; |
498
|
|
|
$this->calling($this->mockedRestClient)->post = []; |
499
|
|
|
|
500
|
|
|
$cartItemRepository = new \mock\Mapado\RestClientSdk\EntityRepository( |
501
|
|
|
$this->mockedSdk, |
502
|
|
|
$this->mockedRestClient, |
503
|
|
|
$this->unitOfWork, |
504
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem' |
505
|
|
|
); |
506
|
|
|
|
507
|
|
|
|
508
|
|
|
$cart = new \Mapado\RestClientSdk\Tests\Model\JsonLd\Cart; |
509
|
|
|
$cart->setId(1); |
510
|
|
|
|
511
|
|
|
$this |
512
|
|
|
->if($cartItemRepository->find(1)) |
513
|
|
|
->then |
514
|
|
|
->mock($this->mockedRestClient) |
515
|
|
|
->call('get') |
516
|
|
|
->withArguments('/cart_items/1')->once() |
517
|
|
|
|
518
|
|
|
->if($cartItemRepository->findAll()) |
519
|
|
|
->then |
520
|
|
|
->mock($this->mockedRestClient) |
521
|
|
|
->call('get') |
522
|
|
|
->withArguments('/cart_items')->once() |
523
|
|
|
|
524
|
|
|
->if($cartItemRepository->findBy(['foo' => 'bar'])) |
525
|
|
|
->then |
526
|
|
|
->mock($this->mockedRestClient) |
527
|
|
|
->call('get') |
528
|
|
|
->withArguments('/cart_items?foo=bar')->once() |
529
|
|
|
|
530
|
|
|
->given($cartItem = new \mock\Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem) |
531
|
|
|
->if($cartItemRepository->persist($cartItem)) |
532
|
|
|
->then |
533
|
|
|
->mock($this->mockedRestClient) |
534
|
|
|
->call('post') |
535
|
|
|
->withArguments('/cart_items')->once() |
536
|
|
|
; |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
public function testFindOneByWithHal() |
540
|
|
|
{ |
541
|
|
|
$mapping = new RestMapping('v12'); |
542
|
|
|
$classMetadata = new ClassMetadata( |
543
|
|
|
'orders', |
544
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Order', |
545
|
|
|
'mock\Mapado\RestClientSdk\EntityRepository' |
546
|
|
|
); |
547
|
|
|
$classMetadata->setAttributeList([ |
548
|
|
|
new Attribute('@id', 'id', 'string', true), |
549
|
|
|
]); |
550
|
|
|
$mapping->setMapping([$classMetadata]); |
551
|
|
|
|
552
|
|
|
$this->calling($this->mockedSdk)->getMapping = $mapping; |
553
|
|
|
|
554
|
|
|
$this->repository = new \mock\Mapado\RestClientSdk\EntityRepository( |
555
|
|
|
$this->mockedSdk, |
556
|
|
|
$this->mockedRestClient, |
557
|
|
|
$this->unitOfWork, |
558
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Order' |
559
|
|
|
); |
560
|
|
|
|
561
|
|
|
$mapping->setConfig([ |
562
|
|
|
'collectionKey' => 'fooList', |
563
|
|
|
]); |
564
|
|
|
$this->calling($this->mockedRestClient)->get = [ |
565
|
|
|
'fooList' => [ |
566
|
|
|
[ |
567
|
|
|
'@id' => '/orders/2', |
568
|
|
|
] |
569
|
|
|
], |
570
|
|
|
]; |
571
|
|
|
$this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping, $this->unitOfWork); |
572
|
|
|
|
573
|
|
|
$this |
574
|
|
|
->then |
575
|
|
|
->object($order = $this->repository->findOneBy(['a' => 'a'])) |
576
|
|
|
->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\Order') |
577
|
|
|
->string($order->getId()) |
578
|
|
|
->isEqualTo('/orders/2') |
579
|
|
|
; |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* testFindOneByWithoutResult |
584
|
|
|
* |
585
|
|
|
* @access public |
586
|
|
|
* @return void |
587
|
|
|
*/ |
588
|
|
|
public function testFindOneByWithoutResult() |
589
|
|
|
{ |
590
|
|
|
$mapping = new RestMapping('v12'); |
591
|
|
|
$classMetadata = new ClassMetadata( |
592
|
|
|
'orders', |
593
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Order', |
594
|
|
|
'mock\Mapado\RestClientSdk\EntityRepository' |
595
|
|
|
); |
596
|
|
|
$classMetadata->setAttributeList([ |
597
|
|
|
new Attribute('@id', 'id', 'string', true), |
598
|
|
|
]); |
599
|
|
|
$mapping->setMapping([$classMetadata]); |
600
|
|
|
|
601
|
|
|
$this->calling($this->mockedSdk)->getMapping = $mapping; |
602
|
|
|
|
603
|
|
|
$this->repository = new \mock\Mapado\RestClientSdk\EntityRepository( |
604
|
|
|
$this->mockedSdk, |
605
|
|
|
$this->mockedRestClient, |
606
|
|
|
$this->unitOfWork, |
607
|
|
|
'Mapado\RestClientSdk\Tests\Model\JsonLd\Order' |
608
|
|
|
); |
609
|
|
|
|
610
|
|
|
$mapping->setConfig([ |
611
|
|
|
'collectionKey' => 'fooList', |
612
|
|
|
]); |
613
|
|
|
$this->calling($this->mockedRestClient)->get = [ |
614
|
|
|
'fooList' => [ |
615
|
|
|
], |
616
|
|
|
]; |
617
|
|
|
$this->calling($this->mockedSdk)->getSerializer = new \Mapado\RestClientSdk\Model\Serializer($mapping, $this->unitOfWork); |
618
|
|
|
|
619
|
|
|
$this |
620
|
|
|
->then |
621
|
|
|
->variable($order = $this->repository->findOneBy(['a' => 'a'])) |
622
|
|
|
->isNull() |
623
|
|
|
; |
624
|
|
|
} |
625
|
|
|
} |
626
|
|
|
|
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: