1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
namespace Aimeos\Admin\JsonAdm; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class StandardTest extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
private $context; |
15
|
|
|
private $object; |
16
|
|
|
private $view; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
protected function setUp() |
20
|
|
|
{ |
21
|
|
|
$this->context = \TestHelperJadm::getContext(); |
22
|
|
|
$templatePaths = \TestHelperJadm::getJsonadmPaths(); |
23
|
|
|
$this->view = $this->context->getView(); |
24
|
|
|
|
25
|
|
|
$this->object = new \Aimeos\Admin\JsonAdm\Standard( $this->context, $this->view, $templatePaths, 'product' ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
protected function tearDown() |
30
|
|
|
{ |
31
|
|
|
\Aimeos\MShop\Factory::clear(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
public function testDelete() |
36
|
|
|
{ |
37
|
|
|
$this->getProductMock( array( 'deleteItem' ) )->expects( $this->once() )->method( 'deleteItem' ); |
38
|
|
|
|
39
|
|
|
$params = array( 'id' => $this->getProductItem()->getId() ); |
40
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
41
|
|
|
$this->view->addHelper( 'param', $helper ); |
42
|
|
|
|
43
|
|
|
$header = array(); |
44
|
|
|
$status = 500; |
45
|
|
|
|
46
|
|
|
$result = json_decode( $this->object->delete( '', $header, $status ), true ); |
47
|
|
|
|
48
|
|
|
$this->assertEquals( 200, $status ); |
49
|
|
|
$this->assertEquals( 1, count( $header ) ); |
50
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
51
|
|
|
$this->assertArrayNotHasKey( 'included', $result ); |
52
|
|
|
$this->assertArrayNotHasKey( 'data', $result ); |
53
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
public function testDeleteBulk() |
58
|
|
|
{ |
59
|
|
|
$this->getProductMock( array( 'deleteItems' ) )->expects( $this->once() )->method( 'deleteItems' ); |
60
|
|
|
|
61
|
|
|
$body = '{"data":[{"type": "product", "id": "-1"},{"type": "product", "id": "-2"}]}'; |
62
|
|
|
$header = array(); |
63
|
|
|
$status = 500; |
64
|
|
|
|
65
|
|
|
$result = json_decode( $this->object->delete( $body, $header, $status ), true ); |
66
|
|
|
|
67
|
|
|
$this->assertEquals( 200, $status ); |
68
|
|
|
$this->assertEquals( 1, count( $header ) ); |
69
|
|
|
$this->assertEquals( 2, $result['meta']['total'] ); |
70
|
|
|
$this->assertArrayNotHasKey( 'included', $result ); |
71
|
|
|
$this->assertArrayNotHasKey( 'data', $result ); |
72
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
public function testDeleteInvalid() |
77
|
|
|
{ |
78
|
|
|
$body = '{"data":null}'; |
79
|
|
|
$header = array(); |
80
|
|
|
$status = 500; |
81
|
|
|
|
82
|
|
|
$result = json_decode( $this->object->delete( $body, $header, $status ), true ); |
83
|
|
|
|
84
|
|
|
$this->assertEquals( 400, $status ); |
85
|
|
|
$this->assertEquals( 1, count( $header ) ); |
86
|
|
|
$this->assertEquals( 0, $result['meta']['total'] ); |
87
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
88
|
|
|
$this->assertArrayNotHasKey( 'included', $result ); |
89
|
|
|
$this->assertArrayNotHasKey( 'data', $result ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
public function testDeleteException() |
94
|
|
|
{ |
95
|
|
|
$this->getProductMock( array( 'deleteItem' ) )->expects( $this->once() )->method( 'deleteItem' ) |
96
|
|
|
->will( $this->throwException( new \Exception( 'test exception' ) ) ); |
97
|
|
|
|
98
|
|
|
$params = array( 'id' => $this->getProductItem()->getId() ); |
99
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
100
|
|
|
$this->view->addHelper( 'param', $helper ); |
101
|
|
|
|
102
|
|
|
$header = array(); |
103
|
|
|
$status = 500; |
104
|
|
|
|
105
|
|
|
$result = json_decode( $this->object->delete( '', $header, $status ), true ); |
106
|
|
|
|
107
|
|
|
$this->assertEquals( 500, $status ); |
108
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
public function testDeleteMShopException() |
113
|
|
|
{ |
114
|
|
|
$this->getProductMock( array( 'deleteItem' ) )->expects( $this->once() )->method( 'deleteItem' ) |
115
|
|
|
->will( $this->throwException( new \Aimeos\MShop\Exception( 'test exception' ) ) ); |
116
|
|
|
|
117
|
|
|
$params = array( 'id' => $this->getProductItem()->getId() ); |
118
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
119
|
|
|
$this->view->addHelper( 'param', $helper ); |
120
|
|
|
|
121
|
|
|
$header = array(); |
122
|
|
|
$status = 500; |
123
|
|
|
|
124
|
|
|
$result = json_decode( $this->object->delete( '', $header, $status ), true ); |
125
|
|
|
|
126
|
|
|
$this->assertEquals( 404, $status ); |
127
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
public function testGet() |
132
|
|
|
{ |
133
|
|
|
$header = array(); |
134
|
|
|
$status = 500; |
135
|
|
|
|
136
|
|
|
$result = json_decode( $this->object->get( '', $header, $status ), true ); |
137
|
|
|
|
138
|
|
|
$this->assertEquals( 200, $status ); |
139
|
|
|
$this->assertEquals( 1, count( $header ) ); |
140
|
|
|
$this->assertEquals( 28, $result['meta']['total'] ); |
141
|
|
|
$this->assertEquals( 25, count( $result['data'] ) ); |
142
|
|
|
$this->assertEquals( 'product', $result['data'][0]['type'] ); |
143
|
|
|
$this->assertEquals( 0, count( $result['included'] ) ); |
144
|
|
|
$this->assertArrayHasKey( 'next', $result['links'] ); |
145
|
|
|
$this->assertArrayHasKey( 'last', $result['links'] ); |
146
|
|
|
$this->assertArrayHasKey( 'self', $result['links'] ); |
147
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
public function testGetWarehouse() |
152
|
|
|
{ |
153
|
|
|
$header = array(); |
154
|
|
|
$status = 500; |
155
|
|
|
|
156
|
|
|
$templatePaths = \TestHelperJadm::getJsonadmPaths(); |
157
|
|
|
$object = new \Aimeos\Admin\JsonAdm\Standard( $this->context, $this->view, $templatePaths, 'product/stock/warehouse' ); |
158
|
|
|
|
159
|
|
|
$result = json_decode( $object->get( '', $header, $status ), true ); |
160
|
|
|
|
161
|
|
|
$this->assertEquals( 200, $status ); |
162
|
|
|
$this->assertEquals( 1, count( $header ) ); |
163
|
|
|
$this->assertEquals( 6, $result['meta']['total'] ); |
164
|
|
|
$this->assertEquals( 6, count( $result['data'] ) ); |
165
|
|
|
$this->assertEquals( 'product/stock/warehouse', $result['data'][0]['type'] ); |
166
|
|
|
$this->assertEquals( 0, count( $result['included'] ) ); |
167
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
public function testGetInvalid() |
172
|
|
|
{ |
173
|
|
|
$header = array(); |
174
|
|
|
$status = 500; |
175
|
|
|
|
176
|
|
|
$templatePaths = \TestHelperJadm::getJsonadmPaths(); |
177
|
|
|
$object = new \Aimeos\Admin\JsonAdm\Standard( $this->context, $this->view, $templatePaths, 'invalid' ); |
178
|
|
|
|
179
|
|
|
$result = json_decode( $object->get( '', $header, $status ), true ); |
180
|
|
|
|
181
|
|
|
$this->assertEquals( 404, $status ); |
182
|
|
|
$this->assertEquals( 1, count( $header ) ); |
183
|
|
|
$this->assertEquals( 1, count( $result['errors'] ) ); |
184
|
|
|
$this->assertArrayHasKey( 'title', $result['errors'][0] ); |
185
|
|
|
$this->assertArrayHasKey( 'detail', $result['errors'][0] ); |
186
|
|
|
$this->assertArrayNotHasKey( 'data', $result ); |
187
|
|
|
$this->assertArrayNotHasKey( 'indluded', $result ); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
public function testGetException() |
192
|
|
|
{ |
193
|
|
|
$this->getProductMock( array( 'getItem' ) )->expects( $this->once() )->method( 'getItem' ) |
194
|
|
|
->will( $this->throwException( new \Exception( 'test exception' ) ) ); |
195
|
|
|
|
196
|
|
|
$params = array( 'id' => -1 ); |
197
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
198
|
|
|
$this->view->addHelper( 'param', $helper ); |
199
|
|
|
|
200
|
|
|
$header = array(); |
201
|
|
|
$status = 500; |
202
|
|
|
|
203
|
|
|
$result = json_decode( $this->object->get( '', $header, $status ), true ); |
204
|
|
|
|
205
|
|
|
$this->assertEquals( 500, $status ); |
206
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
public function testGetMShopException() |
211
|
|
|
{ |
212
|
|
|
$this->getProductMock( array( 'getItem' ) )->expects( $this->once() )->method( 'getItem' ) |
213
|
|
|
->will( $this->throwException( new \Aimeos\MShop\Exception( 'test exception' ) ) ); |
214
|
|
|
|
215
|
|
|
$params = array( 'id' => -1 ); |
216
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
217
|
|
|
$this->view->addHelper( 'param', $helper ); |
218
|
|
|
|
219
|
|
|
$header = array(); |
220
|
|
|
$status = 500; |
221
|
|
|
|
222
|
|
|
$result = json_decode( $this->object->get( '', $header, $status ), true ); |
223
|
|
|
|
224
|
|
|
$this->assertEquals( 404, $status ); |
225
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
public function testGetFilter() |
230
|
|
|
{ |
231
|
|
|
$params = array( |
232
|
|
|
'filter' => array( |
233
|
|
|
'==' => array( 'product.type.code' => 'select' ) |
234
|
|
|
) |
235
|
|
|
); |
236
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
237
|
|
|
$this->view->addHelper( 'param', $helper ); |
238
|
|
|
|
239
|
|
|
$header = array(); |
240
|
|
|
$status = 500; |
241
|
|
|
|
242
|
|
|
$result = json_decode( $this->object->get( '', $header, $status ), true ); |
243
|
|
|
|
244
|
|
|
$this->assertEquals( 200, $status ); |
245
|
|
|
$this->assertEquals( 1, count( $header ) ); |
246
|
|
|
$this->assertEquals( 3, $result['meta']['total'] ); |
247
|
|
|
$this->assertEquals( 3, count( $result['data'] ) ); |
248
|
|
|
$this->assertEquals( 'product', $result['data'][0]['type'] ); |
249
|
|
|
$this->assertEquals( 0, count( $result['included'] ) ); |
250
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
|
254
|
|
|
public function testGetFilterCombine() |
255
|
|
|
{ |
256
|
|
|
$params = array( |
257
|
|
|
'filter' => array( |
258
|
|
|
'&&' => array( |
259
|
|
|
array( '=~' => array( 'product.label' => 'Unittest: Test' ) ), |
260
|
|
|
array( '==' => array( 'product.type.code' => 'select' ) ), |
261
|
|
|
) |
262
|
|
|
) |
263
|
|
|
); |
264
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
265
|
|
|
$this->view->addHelper( 'param', $helper ); |
266
|
|
|
|
267
|
|
|
$header = array(); |
268
|
|
|
$status = 500; |
269
|
|
|
|
270
|
|
|
$result = json_decode( $this->object->get( '', $header, $status ), true ); |
271
|
|
|
|
272
|
|
|
$this->assertEquals( 200, $status ); |
273
|
|
|
$this->assertEquals( 1, count( $header ) ); |
274
|
|
|
$this->assertEquals( 2, $result['meta']['total'] ); |
275
|
|
|
$this->assertEquals( 2, count( $result['data'] ) ); |
276
|
|
|
$this->assertEquals( 'product', $result['data'][0]['type'] ); |
277
|
|
|
$this->assertEquals( 0, count( $result['included'] ) ); |
278
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
public function testGetPage() |
283
|
|
|
{ |
284
|
|
|
$params = array( |
285
|
|
|
'page' => array( |
286
|
|
|
'offset' => 25, |
287
|
|
|
'limit' => 25 |
288
|
|
|
) |
289
|
|
|
); |
290
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
291
|
|
|
$this->view->addHelper( 'param', $helper ); |
292
|
|
|
|
293
|
|
|
$header = array(); |
294
|
|
|
$status = 500; |
295
|
|
|
|
296
|
|
|
$result = json_decode( $this->object->get( '', $header, $status ), true ); |
297
|
|
|
|
298
|
|
|
$this->assertEquals( 200, $status ); |
299
|
|
|
$this->assertEquals( 1, count( $header ) ); |
300
|
|
|
$this->assertEquals( 28, $result['meta']['total'] ); |
301
|
|
|
$this->assertEquals( 3, count( $result['data'] ) ); |
302
|
|
|
$this->assertEquals( 'product', $result['data'][0]['type'] ); |
303
|
|
|
$this->assertEquals( 0, count( $result['included'] ) ); |
304
|
|
|
$this->assertArrayHasKey( 'first', $result['links'] ); |
305
|
|
|
$this->assertArrayHasKey( 'prev', $result['links'] ); |
306
|
|
|
$this->assertArrayHasKey( 'self', $result['links'] ); |
307
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
public function testGetSort() |
312
|
|
|
{ |
313
|
|
|
$params = array( |
314
|
|
|
'sort' => 'product.label,-product.code' |
315
|
|
|
); |
316
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
317
|
|
|
$this->view->addHelper( 'param', $helper ); |
318
|
|
|
|
319
|
|
|
$header = array(); |
320
|
|
|
$status = 500; |
321
|
|
|
|
322
|
|
|
$result = json_decode( $this->object->get( '', $header, $status ), true ); |
323
|
|
|
|
324
|
|
|
$this->assertEquals( 200, $status ); |
325
|
|
|
$this->assertEquals( 1, count( $header ) ); |
326
|
|
|
$this->assertEquals( 28, $result['meta']['total'] ); |
327
|
|
|
$this->assertEquals( 25, count( $result['data'] ) ); |
328
|
|
|
$this->assertEquals( 'product', $result['data'][0]['type'] ); |
329
|
|
|
$this->assertEquals( 'ABCD', $result['data'][0]['attributes']['product.code'] ); |
330
|
|
|
$this->assertEquals( '16 discs', $result['data'][0]['attributes']['product.label'] ); |
331
|
|
|
$this->assertEquals( 0, count( $result['included'] ) ); |
332
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
|
336
|
|
|
public function testGetFields() |
337
|
|
|
{ |
338
|
|
|
$params = array( |
339
|
|
|
'fields' => array( |
340
|
|
|
'product' => 'product.id,product.label' |
341
|
|
|
), |
342
|
|
|
'sort' => 'product.id', |
343
|
|
|
'include' => 'product' |
344
|
|
|
); |
345
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
346
|
|
|
$this->view->addHelper( 'param', $helper ); |
347
|
|
|
|
348
|
|
|
$header = array(); |
349
|
|
|
$status = 500; |
350
|
|
|
|
351
|
|
|
$result = json_decode( $this->object->get( '', $header, $status ), true ); |
352
|
|
|
|
353
|
|
|
$this->assertEquals( 200, $status ); |
354
|
|
|
$this->assertEquals( 1, count( $header ) ); |
355
|
|
|
$this->assertEquals( 28, $result['meta']['total'] ); |
356
|
|
|
$this->assertEquals( 25, count( $result['data'] ) ); |
357
|
|
|
$this->assertEquals( 'product', $result['data'][0]['type'] ); |
358
|
|
|
$this->assertEquals( 2, count( $result['data'][0]['attributes'] ) ); |
359
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
|
363
|
|
|
public function testPatch() |
364
|
|
|
{ |
365
|
|
|
$productManagerStub = $this->getProductMock( array( 'getItem', 'saveItem' ) ); |
366
|
|
|
|
367
|
|
|
$item = $productManagerStub->createItem(); |
368
|
|
|
$item->setLabel( 'test' ); |
369
|
|
|
$item->setId( '-1' ); |
370
|
|
|
|
371
|
|
|
$productManagerStub->expects( $this->once() )->method( 'saveItem' ); |
372
|
|
|
$productManagerStub->expects( $this->exactly( 3 ) )->method( 'getItem' ) // 3x due to decorator |
373
|
|
|
->will( $this->returnValue( $item ) ); |
374
|
|
|
|
375
|
|
|
|
376
|
|
|
$params = array( 'id' => '-1' ); |
377
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
378
|
|
|
$this->view->addHelper( 'param', $helper ); |
379
|
|
|
|
380
|
|
|
$body = '{"data": {"type": "product", "attributes": {"product.label": "test"}}}'; |
381
|
|
|
$header = array(); |
382
|
|
|
$status = 500; |
383
|
|
|
|
384
|
|
|
$result = json_decode( $this->object->patch( $body, $header, $status ), true ); |
385
|
|
|
|
386
|
|
|
$this->assertEquals( 200, $status ); |
387
|
|
|
$this->assertEquals( 1, count( $header ) ); |
388
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
389
|
|
|
$this->assertArrayHasKey( 'data', $result ); |
390
|
|
|
$this->assertEquals( '-1', $result['data']['id'] ); |
391
|
|
|
$this->assertEquals( 'product', $result['data']['type'] ); |
392
|
|
|
$this->assertEquals( 'test', $result['data']['attributes']['product.label'] ); |
393
|
|
|
$this->assertArrayNotHasKey( 'included', $result ); |
394
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
|
398
|
|
|
public function testPatchBulk() |
399
|
|
|
{ |
400
|
|
|
$productManagerStub = $this->getProductMock( array( 'getItem', 'saveItem' ) ); |
401
|
|
|
|
402
|
|
|
$item = $productManagerStub->createItem(); |
403
|
|
|
$item->setLabel( 'test' ); |
404
|
|
|
$item->setId( '-1' ); |
405
|
|
|
|
406
|
|
|
$productManagerStub->expects( $this->exactly( 2 ) )->method( 'saveItem' ); |
407
|
|
|
$productManagerStub->expects( $this->exactly( 6 ) )->method( 'getItem' ) // 6x due to decorator |
408
|
|
|
->will( $this->returnValue( $item ) ); |
409
|
|
|
|
410
|
|
|
|
411
|
|
|
$body = '{"data": [{"id": "-1", "type": "product", "attributes": {"product.label": "test"}}, {"id": "-1", "type": "product", "attributes": {"product.label": "test"}}]}'; |
412
|
|
|
$header = array(); |
413
|
|
|
$status = 500; |
414
|
|
|
|
415
|
|
|
$result = json_decode( $this->object->patch( $body, $header, $status ), true ); |
416
|
|
|
|
417
|
|
|
$this->assertEquals( 200, $status ); |
418
|
|
|
$this->assertEquals( 1, count( $header ) ); |
419
|
|
|
$this->assertEquals( 2, $result['meta']['total'] ); |
420
|
|
|
$this->assertArrayHasKey( 'data', $result ); |
421
|
|
|
$this->assertEquals( 2, count( $result['data'] ) ); |
422
|
|
|
$this->assertEquals( '-1', $result['data'][0]['id'] ); |
423
|
|
|
$this->assertEquals( 'product', $result['data'][0]['type'] ); |
424
|
|
|
$this->assertEquals( 'test', $result['data'][0]['attributes']['product.label'] ); |
425
|
|
|
$this->assertArrayNotHasKey( 'included', $result ); |
426
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
|
430
|
|
|
public function testPatchInvalid() |
431
|
|
|
{ |
432
|
|
|
$body = '{"data":null}'; |
433
|
|
|
$header = array(); |
434
|
|
|
$status = 500; |
435
|
|
|
|
436
|
|
|
$result = json_decode( $this->object->patch( $body, $header, $status ), true ); |
437
|
|
|
|
438
|
|
|
$this->assertEquals( 400, $status ); |
439
|
|
|
$this->assertEquals( 1, count( $header ) ); |
440
|
|
|
$this->assertEquals( 0, $result['meta']['total'] ); |
441
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
442
|
|
|
$this->assertArrayNotHasKey( 'included', $result ); |
443
|
|
|
$this->assertArrayNotHasKey( 'data', $result ); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
|
447
|
|
|
public function testPatchInvalidId() |
448
|
|
|
{ |
449
|
|
|
$body = '{"data":{"id":-1}}'; |
450
|
|
|
$header = array(); |
451
|
|
|
$status = 500; |
452
|
|
|
|
453
|
|
|
$result = json_decode( $this->object->patch( $body, $header, $status ), true ); |
454
|
|
|
|
455
|
|
|
$this->assertEquals( 400, $status ); |
456
|
|
|
$this->assertEquals( 1, count( $header ) ); |
457
|
|
|
$this->assertEquals( 0, $result['meta']['total'] ); |
458
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
|
462
|
|
|
public function testPatchException() |
463
|
|
|
{ |
464
|
|
|
$this->getProductMock( array( 'getItem' ) )->expects( $this->once() )->method( 'getItem' ) |
465
|
|
|
->will( $this->throwException( new \Exception( 'test exception' ) ) ); |
466
|
|
|
|
467
|
|
|
$header = array(); |
468
|
|
|
$status = 500; |
469
|
|
|
|
470
|
|
|
$result = json_decode( $this->object->patch( '{"data":[{"id":-1}]}', $header, $status ), true ); |
471
|
|
|
|
472
|
|
|
$this->assertEquals( 500, $status ); |
473
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
|
477
|
|
|
public function testPatchMShopException() |
478
|
|
|
{ |
479
|
|
|
$this->getProductMock( array( 'getItem' ) )->expects( $this->once() )->method( 'getItem' ) |
480
|
|
|
->will( $this->throwException( new \Aimeos\MShop\Exception( 'test exception' ) ) ); |
481
|
|
|
|
482
|
|
|
$header = array(); |
483
|
|
|
$status = 500; |
484
|
|
|
|
485
|
|
|
$result = json_decode( $this->object->patch( '{"data":[{"id":-1}]}', $header, $status ), true ); |
486
|
|
|
|
487
|
|
|
$this->assertEquals( 404, $status ); |
488
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
|
492
|
|
|
public function testPost() |
493
|
|
|
{ |
494
|
|
|
$productManagerStub = $this->getProductMock( array( 'createItem', 'getItem', 'saveItem' ) ); |
495
|
|
|
|
496
|
|
|
$item = new \Aimeos\MShop\Product\Item\Standard(); |
497
|
|
|
$item->setId( '-1' ); |
498
|
|
|
|
499
|
|
|
$productManagerStub->expects( $this->once() )->method( 'createItem' ) |
500
|
|
|
->will( $this->returnValue( $item ) ); |
501
|
|
|
$productManagerStub->expects( $this->any() )->method( 'getItem' ) |
502
|
|
|
->will( $this->returnValue( $item ) ); |
503
|
|
|
$productManagerStub->expects( $this->once() )->method( 'saveItem' ); |
504
|
|
|
|
505
|
|
|
|
506
|
|
|
$body = '{"data": {"type": "product", "attributes": {"product.type": "default", "product.label": "test"}}}'; |
507
|
|
|
$header = array(); |
508
|
|
|
$status = 500; |
509
|
|
|
|
510
|
|
|
$result = json_decode( $this->object->post( $body, $header, $status ), true ); |
511
|
|
|
|
512
|
|
|
$this->assertEquals( 201, $status ); |
513
|
|
|
$this->assertEquals( 1, count( $header ) ); |
514
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
515
|
|
|
$this->assertArrayHasKey( 'data', $result ); |
516
|
|
|
$this->assertEquals( '-1', $result['data']['id'] ); |
517
|
|
|
$this->assertEquals( 'product', $result['data']['type'] ); |
518
|
|
|
$this->assertGreaterThan( 0, $result['data']['attributes']['product.typeid'] ); |
519
|
|
|
$this->assertEquals( 'test', $result['data']['attributes']['product.label'] ); |
520
|
|
|
$this->assertArrayNotHasKey( 'included', $result ); |
521
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
|
525
|
|
|
public function testPostBulk() |
526
|
|
|
{ |
527
|
|
|
$productManagerStub = $this->getProductMock( array( 'getItem', 'saveItem' ) ); |
528
|
|
|
|
529
|
|
|
$item = $productManagerStub->createItem(); |
530
|
|
|
$item->setLabel( 'test' ); |
531
|
|
|
$item->setId( '-1' ); |
532
|
|
|
|
533
|
|
|
$productManagerStub->expects( $this->exactly( 2 ) )->method( 'saveItem' ); |
534
|
|
|
$productManagerStub->expects( $this->exactly( 2 ) )->method( 'getItem' ) |
535
|
|
|
->will( $this->returnValue( $item ) ); |
536
|
|
|
|
537
|
|
|
|
538
|
|
|
$body = '{"data": [{"type": "product", "attributes": {"product.label": "test"}}, {"type": "product", "attributes": {"product.label": "test"}}]}'; |
539
|
|
|
$header = array(); |
540
|
|
|
$status = 500; |
541
|
|
|
|
542
|
|
|
$result = json_decode( $this->object->post( $body, $header, $status ), true ); |
543
|
|
|
|
544
|
|
|
$this->assertEquals( 201, $status ); |
545
|
|
|
$this->assertEquals( 1, count( $header ) ); |
546
|
|
|
$this->assertEquals( 2, $result['meta']['total'] ); |
547
|
|
|
$this->assertArrayHasKey( 'data', $result ); |
548
|
|
|
$this->assertEquals( 2, count( $result['data'] ) ); |
549
|
|
|
$this->assertEquals( '-1', $result['data'][0]['id'] ); |
550
|
|
|
$this->assertEquals( 'product', $result['data'][0]['type'] ); |
551
|
|
|
$this->assertEquals( 'test', $result['data'][0]['attributes']['product.label'] ); |
552
|
|
|
$this->assertArrayNotHasKey( 'included', $result ); |
553
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
|
557
|
|
|
public function testPostRelationships() |
558
|
|
|
{ |
559
|
|
|
$productManagerStub = $this->getProductMock( array( 'getSubManager', 'createItem', 'getItem', 'saveItem' ) ); |
560
|
|
|
$productManagerListsStub = $this->getProductListsMock( array( 'saveItem' ) ); |
561
|
|
|
|
562
|
|
|
$item = new \Aimeos\MShop\Product\Item\Standard(); |
563
|
|
|
$item->setId( '-1' ); |
564
|
|
|
|
565
|
|
|
$productManagerStub->expects( $this->once() )->method( 'createItem' ) |
566
|
|
|
->will( $this->returnValue( $item ) ); |
567
|
|
|
$productManagerStub->expects( $this->any() )->method( 'getItem' ) |
568
|
|
|
->will( $this->returnValue( $item ) ); |
569
|
|
|
$productManagerStub->expects( $this->once() )->method( 'getSubManager' ) |
570
|
|
|
->will( $this->returnValue( $productManagerListsStub ) ); |
571
|
|
|
$productManagerStub->expects( $this->once() )->method( 'saveItem' ); |
572
|
|
|
|
573
|
|
|
$productManagerListsStub->expects( $this->once() )->method( 'saveItem' ); |
574
|
|
|
|
575
|
|
|
$body = '{"data": {"type": "product", |
576
|
|
|
"attributes": {"product.label": "test"}, |
577
|
|
|
"relationships": {"text": {"data": [ |
578
|
|
|
{"type": "text", "id": "-2", "attributes": {"product.lists.type": "default"}} |
579
|
|
|
]}} |
580
|
|
|
}}'; |
581
|
|
|
$header = array(); |
582
|
|
|
$status = 500; |
583
|
|
|
|
584
|
|
|
$result = json_decode( $this->object->post( $body, $header, $status ), true ); |
585
|
|
|
|
586
|
|
|
$this->assertEquals( 201, $status ); |
587
|
|
|
$this->assertEquals( 1, count( $header ) ); |
588
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
589
|
|
|
$this->assertArrayHasKey( 'data', $result ); |
590
|
|
|
$this->assertEquals( '-1', $result['data']['id'] ); |
591
|
|
|
$this->assertEquals( 'product', $result['data']['type'] ); |
592
|
|
|
$this->assertEquals( 'test', $result['data']['attributes']['product.label'] ); |
593
|
|
|
$this->assertArrayNotHasKey( 'included', $result ); |
594
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
|
598
|
|
|
public function testPostInvalid() |
599
|
|
|
{ |
600
|
|
|
$body = '{"data":null}'; |
601
|
|
|
$header = array(); |
602
|
|
|
$status = 500; |
603
|
|
|
|
604
|
|
|
$result = json_decode( $this->object->post( $body, $header, $status ), true ); |
605
|
|
|
|
606
|
|
|
$this->assertEquals( 400, $status ); |
607
|
|
|
$this->assertEquals( 1, count( $header ) ); |
608
|
|
|
$this->assertEquals( 0, $result['meta']['total'] ); |
609
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
610
|
|
|
$this->assertArrayNotHasKey( 'included', $result ); |
611
|
|
|
$this->assertArrayNotHasKey( 'data', $result ); |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
|
615
|
|
|
public function testPostInvalidId() |
616
|
|
|
{ |
617
|
|
|
$body = '{"data":{"id":-1}}'; |
618
|
|
|
$header = array(); |
619
|
|
|
$status = 500; |
620
|
|
|
|
621
|
|
|
$result = json_decode( $this->object->post( $body, $header, $status ), true ); |
622
|
|
|
|
623
|
|
|
$this->assertEquals( 403, $status ); |
624
|
|
|
$this->assertEquals( 1, count( $header ) ); |
625
|
|
|
$this->assertEquals( 0, $result['meta']['total'] ); |
626
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
|
630
|
|
|
public function testPostException() |
631
|
|
|
{ |
632
|
|
|
$this->getProductMock( array( 'saveItem' ) )->expects( $this->once() )->method( 'saveItem' ) |
633
|
|
|
->will( $this->throwException( new \Exception( 'test exception' ) ) ); |
634
|
|
|
|
635
|
|
|
$header = array(); |
636
|
|
|
$status = 500; |
637
|
|
|
|
638
|
|
|
$result = json_decode( $this->object->post( '{"data":{}}', $header, $status ), true ); |
639
|
|
|
|
640
|
|
|
$this->assertEquals( 500, $status ); |
641
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
|
645
|
|
|
public function testPostMShopException() |
646
|
|
|
{ |
647
|
|
|
$this->getProductMock( array( 'saveItem' ) )->expects( $this->once() )->method( 'saveItem' ) |
648
|
|
|
->will( $this->throwException( new \Aimeos\MShop\Exception( 'test exception' ) ) ); |
649
|
|
|
|
650
|
|
|
$header = array(); |
651
|
|
|
$status = 500; |
652
|
|
|
|
653
|
|
|
$result = json_decode( $this->object->post( '{"data":{}}', $header, $status ), true ); |
654
|
|
|
|
655
|
|
|
$this->assertEquals( 404, $status ); |
656
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
657
|
|
|
} |
658
|
|
|
|
659
|
|
|
|
660
|
|
|
public function testPut() |
661
|
|
|
{ |
662
|
|
|
$body = ''; |
663
|
|
|
$header = array(); |
664
|
|
|
$status = 500; |
665
|
|
|
|
666
|
|
|
$result = json_decode( $this->object->put( $body, $header, $status ), true ); |
667
|
|
|
|
668
|
|
|
$this->assertEquals( 501, $status ); |
669
|
|
|
$this->assertEquals( 1, count( $header ) ); |
670
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
|
674
|
|
|
public function testOptions() |
675
|
|
|
{ |
676
|
|
|
$header = array(); |
677
|
|
|
$status = 500; |
678
|
|
|
|
679
|
|
|
$result = json_decode( $this->object->options( '', $header, $status ), true ); |
680
|
|
|
|
681
|
|
|
$this->assertEquals( 200, $status ); |
682
|
|
|
$this->assertEquals( 2, count( $header ) ); |
683
|
|
|
$this->assertEquals( 59, count( $result['meta']['resources'] ) ); |
684
|
|
|
$this->assertGreaterThan( 0, count( $result['meta']['attributes'] ) ); |
685
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
|
689
|
|
|
public function testOptionsException() |
690
|
|
|
{ |
691
|
|
|
$this->getProductMock( array( 'getResourceType' ) )->expects( $this->once() )->method( 'getResourceType' ) |
692
|
|
|
->will( $this->throwException( new \Exception( 'test exception' ) ) ); |
693
|
|
|
|
694
|
|
|
$header = array(); |
695
|
|
|
$status = 500; |
696
|
|
|
|
697
|
|
|
$result = json_decode( $this->object->options( '', $header, $status ), true ); |
698
|
|
|
|
699
|
|
|
$this->assertEquals( 500, $status ); |
700
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
|
704
|
|
|
public function testOptionsMShopException() |
705
|
|
|
{ |
706
|
|
|
$this->getProductMock( array( 'getResourceType' ) )->expects( $this->once() )->method( 'getResourceType' ) |
707
|
|
|
->will( $this->throwException( new \Aimeos\MShop\Exception( 'test exception' ) ) ); |
708
|
|
|
|
709
|
|
|
$header = array(); |
710
|
|
|
$status = 500; |
711
|
|
|
|
712
|
|
|
$result = json_decode( $this->object->options( '', $header, $status ), true ); |
713
|
|
|
|
714
|
|
|
$this->assertEquals( 404, $status ); |
715
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
|
719
|
|
|
protected function getProductMock( array $methods ) |
720
|
|
|
{ |
721
|
|
|
$name = 'ClientJsonAdmStandard'; |
722
|
|
|
$this->context->getConfig()->set( 'mshop/product/manager/name', $name ); |
723
|
|
|
|
724
|
|
|
$stub = $this->getMockBuilder( '\\Aimeos\\MShop\\Product\\Manager\\Standard' ) |
725
|
|
|
->setConstructorArgs( array( $this->context ) ) |
726
|
|
|
->setMethods( $methods ) |
727
|
|
|
->getMock(); |
728
|
|
|
|
729
|
|
|
\Aimeos\MShop\Product\Manager\Factory::injectManager( '\\Aimeos\\MShop\\Product\\Manager\\' . $name, $stub ); |
730
|
|
|
|
731
|
|
|
return $stub; |
732
|
|
|
} |
733
|
|
|
|
734
|
|
|
|
735
|
|
|
protected function getProductListsMock( array $methods ) |
736
|
|
|
{ |
737
|
|
|
$name = 'ClientJsonAdmStandard'; |
738
|
|
|
$this->context->getConfig()->set( 'mshop/product/manager/lists/name', $name ); |
739
|
|
|
|
740
|
|
|
$stub = $this->getMockBuilder( '\\Aimeos\\MShop\\Product\\Manager\\Lists\\Standard' ) |
741
|
|
|
->setConstructorArgs( array( $this->context ) ) |
742
|
|
|
->setMethods( $methods ) |
743
|
|
|
->getMock(); |
744
|
|
|
|
745
|
|
|
\Aimeos\MShop\Product\Manager\Factory::injectManager( '\\Aimeos\\MShop\\Product\\Manager\\Lists\\' . $name, $stub ); |
746
|
|
|
|
747
|
|
|
return $stub; |
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
|
751
|
|
|
protected function getProductItem( $code = 'CNC' ) |
752
|
|
|
{ |
753
|
|
|
$manager = \Aimeos\MShop\Product\Manager\Factory::createManager( $this->context ); |
754
|
|
|
$search = $manager->createSearch(); |
755
|
|
|
$search->setConditions( $search->compare( '==', 'product.code', $code ) ); |
756
|
|
|
$items = $manager->searchItems( $search ); |
757
|
|
|
|
758
|
|
|
if( ( $item = reset( $items ) ) === false ) { |
759
|
|
|
throw new \Exception( sprintf( 'No product item with code "%1$s" found', $code ) ); |
760
|
|
|
} |
761
|
|
|
|
762
|
|
|
return $item; |
763
|
|
|
} |
764
|
|
|
} |