Completed
Push — master ( f9d464...00af66 )
by Aimeos
02:40
created

StandardTest::testDeleteBulk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
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 View Code Duplication
	public function testDeleteInvalid()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
	public function testDeleteException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
	public function testDeleteMAdminException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
	{
114
		$this->getProductMock( array( 'deleteItem' ) )->expects( $this->once() )->method( 'deleteItem' )
115
			->will( $this->throwException( new \Aimeos\MAdmin\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 View Code Duplication
	public function testDeleteMShopException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
	{
133
		$this->getProductMock( array( 'deleteItem' ) )->expects( $this->once() )->method( 'deleteItem' )
134
			->will( $this->throwException( new \Aimeos\MShop\Exception( 'test exception' ) ) );
135
136
		$params = array( 'id' => $this->getProductItem()->getId() );
137
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
138
		$this->view->addHelper( 'param', $helper );
139
140
		$header = array();
141
		$status = 500;
142
143
		$result = json_decode( $this->object->delete( '', $header, $status ), true );
144
145
		$this->assertEquals( 404, $status );
146
		$this->assertArrayHasKey( 'errors', $result );
147
	}
148
149
150
	public function testGet()
151
	{
152
		$header = array();
153
		$status = 500;
154
155
		$result = json_decode( $this->object->get( '', $header, $status ), true );
156
157
		$this->assertEquals( 200, $status );
158
		$this->assertEquals( 1, count( $header ) );
159
		$this->assertEquals( 28, $result['meta']['total'] );
160
		$this->assertEquals( 25, count( $result['data'] ) );
161
		$this->assertEquals( 'product', $result['data'][0]['type'] );
162
		$this->assertEquals( 0, count( $result['included'] ) );
163
		$this->assertArrayHasKey( 'next', $result['links'] );
164
		$this->assertArrayHasKey( 'last', $result['links'] );
165
		$this->assertArrayHasKey( 'self', $result['links'] );
166
		$this->assertArrayNotHasKey( 'errors', $result );
167
	}
168
169
170
	public function testGetWarehouse()
171
	{
172
		$header = array();
173
		$status = 500;
174
175
		$templatePaths = \TestHelperJadm::getJsonadmPaths();
176
		$object = new \Aimeos\Admin\JsonAdm\Standard( $this->context, $this->view, $templatePaths, 'product/stock/warehouse' );
177
178
		$result = json_decode( $object->get( '', $header, $status ), true );
179
180
		$this->assertEquals( 200, $status );
181
		$this->assertEquals( 1, count( $header ) );
182
		$this->assertEquals( 6, $result['meta']['total'] );
183
		$this->assertEquals( 6, count( $result['data'] ) );
184
		$this->assertEquals( 'product/stock/warehouse', $result['data'][0]['type'] );
185
		$this->assertEquals( 0, count( $result['included'] ) );
186
		$this->assertArrayNotHasKey( 'errors', $result );
187
	}
188
189
190
	public function testGetInvalid()
191
	{
192
		$header = array();
193
		$status = 500;
194
195
		$templatePaths = \TestHelperJadm::getJsonadmPaths();
196
		$object = new \Aimeos\Admin\JsonAdm\Standard( $this->context, $this->view, $templatePaths, 'invalid' );
197
198
		$result = json_decode( $object->get( '', $header, $status ), true );
199
200
		$this->assertEquals( 404, $status );
201
		$this->assertEquals( 1, count( $header ) );
202
		$this->assertEquals( 1, count( $result['errors'] ) );
203
		$this->assertArrayHasKey( 'title', $result['errors'][0] );
204
		$this->assertArrayHasKey( 'detail', $result['errors'][0] );
205
		$this->assertArrayNotHasKey( 'data', $result );
206
		$this->assertArrayNotHasKey( 'indluded', $result );
207
	}
208
209
210 View Code Duplication
	public function testGetException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
211
	{
212
		$this->getProductMock( array( 'getItem' ) )->expects( $this->once() )->method( 'getItem' )
213
			->will( $this->throwException( new \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( 500, $status );
225
		$this->assertArrayHasKey( 'errors', $result );
226
	}
227
228
229 View Code Duplication
	public function testGetMAdminException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
230
	{
231
		$this->getProductMock( array( 'getItem' ) )->expects( $this->once() )->method( 'getItem' )
232
			->will( $this->throwException( new \Aimeos\MAdmin\Exception( 'test exception' ) ) );
233
234
		$params = array( 'id' => -1 );
235
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
236
		$this->view->addHelper( 'param', $helper );
237
238
		$header = array();
239
		$status = 500;
240
241
		$result = json_decode( $this->object->get( '', $header, $status ), true );
242
243
		$this->assertEquals( 404, $status );
244
		$this->assertArrayHasKey( 'errors', $result );
245
	}
246
247
248 View Code Duplication
	public function testGetMShopException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
249
	{
250
		$this->getProductMock( array( 'getItem' ) )->expects( $this->once() )->method( 'getItem' )
251
			->will( $this->throwException( new \Aimeos\MShop\Exception( 'test exception' ) ) );
252
253
		$params = array( 'id' => -1 );
254
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
255
		$this->view->addHelper( 'param', $helper );
256
257
		$header = array();
258
		$status = 500;
259
260
		$result = json_decode( $this->object->get( '', $header, $status ), true );
261
262
		$this->assertEquals( 404, $status );
263
		$this->assertArrayHasKey( 'errors', $result );
264
	}
265
266
267
	public function testGetFilter()
268
	{
269
		$params = array(
270
			'filter' => array(
271
				'==' => array( 'product.type.code' => 'select' )
272
			)
273
		);
274
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
275
		$this->view->addHelper( 'param', $helper );
276
277
		$header = array();
278
		$status = 500;
279
280
		$result = json_decode( $this->object->get( '', $header, $status ), true );
281
282
		$this->assertEquals( 200, $status );
283
		$this->assertEquals( 1, count( $header ) );
284
		$this->assertEquals( 3, $result['meta']['total'] );
285
		$this->assertEquals( 3, count( $result['data'] ) );
286
		$this->assertEquals( 'product', $result['data'][0]['type'] );
287
		$this->assertEquals( 0, count( $result['included'] ) );
288
		$this->assertArrayNotHasKey( 'errors', $result );
289
	}
290
291
292
	public function testGetFilterCombine()
293
	{
294
		$params = array(
295
			'filter' => array(
296
				'&&' => array(
297
					array( '=~' => array( 'product.label' => 'Unittest: Test' ) ),
298
					array( '==' => array( 'product.type.code' => 'select' ) ),
299
				)
300
			)
301
		);
302
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
303
		$this->view->addHelper( 'param', $helper );
304
305
		$header = array();
306
		$status = 500;
307
308
		$result = json_decode( $this->object->get( '', $header, $status ), true );
309
310
		$this->assertEquals( 200, $status );
311
		$this->assertEquals( 1, count( $header ) );
312
		$this->assertEquals( 2, $result['meta']['total'] );
313
		$this->assertEquals( 2, count( $result['data'] ) );
314
		$this->assertEquals( 'product', $result['data'][0]['type'] );
315
		$this->assertEquals( 0, count( $result['included'] ) );
316
		$this->assertArrayNotHasKey( 'errors', $result );
317
	}
318
319
320
	public function testGetPage()
321
	{
322
		$params = array(
323
			'page' => array(
324
				'offset' => 25,
325
				'limit' => 25
326
			)
327
		);
328
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
329
		$this->view->addHelper( 'param', $helper );
330
331
		$header = array();
332
		$status = 500;
333
334
		$result = json_decode( $this->object->get( '', $header, $status ), true );
335
336
		$this->assertEquals( 200, $status );
337
		$this->assertEquals( 1, count( $header ) );
338
		$this->assertEquals( 28, $result['meta']['total'] );
339
		$this->assertEquals( 3, count( $result['data'] ) );
340
		$this->assertEquals( 'product', $result['data'][0]['type'] );
341
		$this->assertEquals( 0, count( $result['included'] ) );
342
		$this->assertArrayHasKey( 'first', $result['links'] );
343
		$this->assertArrayHasKey( 'prev', $result['links'] );
344
		$this->assertArrayHasKey( 'self', $result['links'] );
345
		$this->assertArrayNotHasKey( 'errors', $result );
346
	}
347
348
349
	public function testGetSort()
350
	{
351
		$params = array(
352
			'sort' => 'product.label,-product.code'
353
		);
354
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
355
		$this->view->addHelper( 'param', $helper );
356
357
		$header = array();
358
		$status = 500;
359
360
		$result = json_decode( $this->object->get( '', $header, $status ), true );
361
362
		$this->assertEquals( 200, $status );
363
		$this->assertEquals( 1, count( $header ) );
364
		$this->assertEquals( 28, $result['meta']['total'] );
365
		$this->assertEquals( 25, count( $result['data'] ) );
366
		$this->assertEquals( 'product', $result['data'][0]['type'] );
367
		$this->assertEquals( 'ABCD', $result['data'][0]['attributes']['product.code'] );
368
		$this->assertEquals( '16 discs', $result['data'][0]['attributes']['product.label'] );
369
		$this->assertEquals( 0, count( $result['included'] ) );
370
		$this->assertArrayNotHasKey( 'errors', $result );
371
	}
372
373
374
	public function testGetFields()
375
	{
376
		$params = array(
377
			'fields' => array(
378
				'product' => 'product.id,product.label'
379
			),
380
			'sort' => 'product.id',
381
			'include' => 'product'
382
		);
383
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
384
		$this->view->addHelper( 'param', $helper );
385
386
		$header = array();
387
		$status = 500;
388
389
		$result = json_decode( $this->object->get( '', $header, $status ), true );
390
391
		$this->assertEquals( 200, $status );
392
		$this->assertEquals( 1, count( $header ) );
393
		$this->assertEquals( 28, $result['meta']['total'] );
394
		$this->assertEquals( 25, count( $result['data'] ) );
395
		$this->assertEquals( 'product', $result['data'][0]['type'] );
396
		$this->assertEquals( 2, count( $result['data'][0]['attributes'] ) );
397
		$this->assertArrayNotHasKey( 'errors', $result );
398
	}
399
400
401
	public function testPatch()
402
	{
403
		$productManagerStub = $this->getProductMock( array( 'getItem', 'saveItem' ) );
404
405
		$item = $productManagerStub->createItem();
406
		$item->setLabel( 'test' );
407
		$item->setId( '-1' );
408
409
		$productManagerStub->expects( $this->once() )->method( 'saveItem' );
410
		$productManagerStub->expects( $this->exactly( 3 ) )->method( 'getItem' ) // 3x due to decorator
411
			->will( $this->returnValue( $item ) );
412
413
414
		$params = array( 'id' => '-1' );
415
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
416
		$this->view->addHelper( 'param', $helper );
417
418
		$body = '{"data": {"type": "product", "attributes": {"product.label": "test"}}}';
419
		$header = array();
420
		$status = 500;
421
422
		$result = json_decode( $this->object->patch( $body, $header, $status ), true );
423
424
		$this->assertEquals( 200, $status );
425
		$this->assertEquals( 1, count( $header ) );
426
		$this->assertEquals( 1, $result['meta']['total'] );
427
		$this->assertArrayHasKey( 'data', $result );
428
		$this->assertEquals( '-1', $result['data']['id'] );
429
		$this->assertEquals( 'product', $result['data']['type'] );
430
		$this->assertEquals( 'test', $result['data']['attributes']['product.label'] );
431
		$this->assertArrayNotHasKey( 'included', $result );
432
		$this->assertArrayNotHasKey( 'errors', $result );
433
	}
434
435
436 View Code Duplication
	public function testPatchBulk()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
437
	{
438
		$productManagerStub = $this->getProductMock( array( 'getItem', 'saveItem' ) );
439
440
		$item = $productManagerStub->createItem();
441
		$item->setLabel( 'test' );
442
		$item->setId( '-1' );
443
444
		$productManagerStub->expects( $this->exactly( 2 ) )->method( 'saveItem' );
445
		$productManagerStub->expects( $this->exactly( 6 ) )->method( 'getItem' ) // 6x due to decorator
446
			->will( $this->returnValue( $item ) );
447
448
449
		$body = '{"data": [{"id": "-1", "type": "product", "attributes": {"product.label": "test"}}, {"id": "-1", "type": "product", "attributes": {"product.label": "test"}}]}';
450
		$header = array();
451
		$status = 500;
452
453
		$result = json_decode( $this->object->patch( $body, $header, $status ), true );
454
455
		$this->assertEquals( 200, $status );
456
		$this->assertEquals( 1, count( $header ) );
457
		$this->assertEquals( 2, $result['meta']['total'] );
458
		$this->assertArrayHasKey( 'data', $result );
459
		$this->assertEquals( 2, count( $result['data'] ) );
460
		$this->assertEquals( '-1', $result['data'][0]['id'] );
461
		$this->assertEquals( 'product', $result['data'][0]['type'] );
462
		$this->assertEquals( 'test', $result['data'][0]['attributes']['product.label'] );
463
		$this->assertArrayNotHasKey( 'included', $result );
464
		$this->assertArrayNotHasKey( 'errors', $result );
465
	}
466
467
468 View Code Duplication
	public function testPatchInvalid()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
469
	{
470
		$body = '{"data":null}';
471
		$header = array();
472
		$status = 500;
473
474
		$result = json_decode( $this->object->patch( $body, $header, $status ), true );
475
476
		$this->assertEquals( 400, $status );
477
		$this->assertEquals( 1, count( $header ) );
478
		$this->assertEquals( 0, $result['meta']['total'] );
479
		$this->assertArrayHasKey( 'errors', $result );
480
		$this->assertArrayNotHasKey( 'included', $result );
481
		$this->assertArrayNotHasKey( 'data', $result );
482
	}
483
484
485 View Code Duplication
	public function testPatchInvalidId()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
486
	{
487
		$body = '{"data":{"id":-1}}';
488
		$header = array();
489
		$status = 500;
490
491
		$result = json_decode( $this->object->patch( $body, $header, $status ), true );
492
493
		$this->assertEquals( 400, $status );
494
		$this->assertEquals( 1, count( $header ) );
495
		$this->assertEquals( 0, $result['meta']['total'] );
496
		$this->assertArrayHasKey( 'errors', $result );
497
	}
498
499
500 View Code Duplication
	public function testPatchException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
501
	{
502
		$this->getProductMock( array( 'getItem' ) )->expects( $this->once() )->method( 'getItem' )
503
			->will( $this->throwException( new \Exception( 'test exception' ) ) );
504
505
		$header = array();
506
		$status = 500;
507
508
		$result = json_decode( $this->object->patch( '{"data":[{"id":-1}]}', $header, $status ), true );
509
510
		$this->assertEquals( 500, $status );
511
		$this->assertArrayHasKey( 'errors', $result );
512
	}
513
514
515 View Code Duplication
	public function testPatchMAdminException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
516
	{
517
		$this->getProductMock( array( 'getItem' ) )->expects( $this->once() )->method( 'getItem' )
518
			->will( $this->throwException( new \Aimeos\MAdmin\Exception( 'test exception' ) ) );
519
520
		$header = array();
521
		$status = 500;
522
523
		$result = json_decode( $this->object->patch( '{"data":[{"id":-1}]}', $header, $status ), true );
524
525
		$this->assertEquals( 404, $status );
526
		$this->assertArrayHasKey( 'errors', $result );
527
	}
528
529
530 View Code Duplication
	public function testPatchMShopException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
531
	{
532
		$this->getProductMock( array( 'getItem' ) )->expects( $this->once() )->method( 'getItem' )
533
			->will( $this->throwException( new \Aimeos\MShop\Exception( 'test exception' ) ) );
534
535
		$header = array();
536
		$status = 500;
537
538
		$result = json_decode( $this->object->patch( '{"data":[{"id":-1}]}', $header, $status ), true );
539
540
		$this->assertEquals( 404, $status );
541
		$this->assertArrayHasKey( 'errors', $result );
542
	}
543
544
545
	public function testPost()
546
	{
547
		$productManagerStub = $this->getProductMock( array( 'getItem', 'saveItem' ) );
548
549
		$item = $productManagerStub->createItem();
550
		$item->setLabel( 'test' );
551
		$item->setId( '-1' );
552
553
		$productManagerStub->expects( $this->once() )->method( 'saveItem' );
554
		$productManagerStub->expects( $this->once() )->method( 'getItem' )
555
			->will( $this->returnValue( $item ) );
556
557
558
		$body = '{"data": {"type": "product", "attributes": {"product.label": "test"}}}';
559
		$header = array();
560
		$status = 500;
561
562
		$result = json_decode( $this->object->post( $body, $header, $status ), true );
563
564
		$this->assertEquals( 201, $status );
565
		$this->assertEquals( 1, count( $header ) );
566
		$this->assertEquals( 1, $result['meta']['total'] );
567
		$this->assertArrayHasKey( 'data', $result );
568
		$this->assertEquals( '-1', $result['data']['id'] );
569
		$this->assertEquals( 'product', $result['data']['type'] );
570
		$this->assertEquals( 'test', $result['data']['attributes']['product.label'] );
571
		$this->assertArrayNotHasKey( 'included', $result );
572
		$this->assertArrayNotHasKey( 'errors', $result );
573
	}
574
575
576 View Code Duplication
	public function testPostBulk()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
577
	{
578
		$productManagerStub = $this->getProductMock( array( 'getItem', 'saveItem' ) );
579
580
		$item = $productManagerStub->createItem();
581
		$item->setLabel( 'test' );
582
		$item->setId( '-1' );
583
584
		$productManagerStub->expects( $this->exactly( 2 ) )->method( 'saveItem' );
585
		$productManagerStub->expects( $this->exactly( 2 ) )->method( 'getItem' )
586
			->will( $this->returnValue( $item ) );
587
588
589
		$body = '{"data": [{"type": "product", "attributes": {"product.label": "test"}}, {"type": "product", "attributes": {"product.label": "test"}}]}';
590
		$header = array();
591
		$status = 500;
592
593
		$result = json_decode( $this->object->post( $body, $header, $status ), true );
594
595
		$this->assertEquals( 201, $status );
596
		$this->assertEquals( 1, count( $header ) );
597
		$this->assertEquals( 2, $result['meta']['total'] );
598
		$this->assertArrayHasKey( 'data', $result );
599
		$this->assertEquals( 2, count( $result['data'] ) );
600
		$this->assertEquals( '-1', $result['data'][0]['id'] );
601
		$this->assertEquals( 'product', $result['data'][0]['type'] );
602
		$this->assertEquals( 'test', $result['data'][0]['attributes']['product.label'] );
603
		$this->assertArrayNotHasKey( 'included', $result );
604
		$this->assertArrayNotHasKey( 'errors', $result );
605
	}
606
607
608 View Code Duplication
	public function testPostInvalid()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
609
	{
610
		$body = '{"data":null}';
611
		$header = array();
612
		$status = 500;
613
614
		$result = json_decode( $this->object->post( $body, $header, $status ), true );
615
616
		$this->assertEquals( 400, $status );
617
		$this->assertEquals( 1, count( $header ) );
618
		$this->assertEquals( 0, $result['meta']['total'] );
619
		$this->assertArrayHasKey( 'errors', $result );
620
		$this->assertArrayNotHasKey( 'included', $result );
621
		$this->assertArrayNotHasKey( 'data', $result );
622
	}
623
624
625 View Code Duplication
	public function testPostInvalidId()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
626
	{
627
		$body = '{"data":{"id":-1}}';
628
		$header = array();
629
		$status = 500;
630
631
		$result = json_decode( $this->object->post( $body, $header, $status ), true );
632
633
		$this->assertEquals( 403, $status );
634
		$this->assertEquals( 1, count( $header ) );
635
		$this->assertEquals( 0, $result['meta']['total'] );
636
		$this->assertArrayHasKey( 'errors', $result );
637
	}
638
639
640 View Code Duplication
	public function testPostException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
641
	{
642
		$this->getProductMock( array( 'saveItem' ) )->expects( $this->once() )->method( 'saveItem' )
643
			->will( $this->throwException( new \Exception( 'test exception' ) ) );
644
645
		$header = array();
646
		$status = 500;
647
648
		$result = json_decode( $this->object->post( '{"data":{}}', $header, $status ), true );
649
650
		$this->assertEquals( 500, $status );
651
		$this->assertArrayHasKey( 'errors', $result );
652
	}
653
654
655 View Code Duplication
	public function testPostMAdminException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
656
	{
657
		$this->getProductMock( array( 'saveItem' ) )->expects( $this->once() )->method( 'saveItem' )
658
			->will( $this->throwException( new \Aimeos\MAdmin\Exception( 'test exception' ) ) );
659
660
		$header = array();
661
		$status = 500;
662
663
		$result = json_decode( $this->object->post( '{"data":{}}', $header, $status ), true );
664
665
		$this->assertEquals( 404, $status );
666
		$this->assertArrayHasKey( 'errors', $result );
667
	}
668
669
670 View Code Duplication
	public function testPostMShopException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
671
	{
672
		$this->getProductMock( array( 'saveItem' ) )->expects( $this->once() )->method( 'saveItem' )
673
			->will( $this->throwException( new \Aimeos\MShop\Exception( 'test exception' ) ) );
674
675
		$header = array();
676
		$status = 500;
677
678
		$result = json_decode( $this->object->post( '{"data":{}}', $header, $status ), true );
679
680
		$this->assertEquals( 404, $status );
681
		$this->assertArrayHasKey( 'errors', $result );
682
	}
683
684
685
	public function testPut()
686
	{
687
		$body = '';
688
		$header = array();
689
		$status = 500;
690
691
		$result = json_decode( $this->object->put( $body, $header, $status ), true );
692
693
		$this->assertEquals( 501, $status );
694
		$this->assertEquals( 1, count( $header ) );
695
		$this->assertArrayHasKey( 'errors', $result );
696
	}
697
698
699
	public function testOptions()
700
	{
701
		$header = array();
702
		$status = 500;
703
704
		$result = json_decode( $this->object->options( '', $header, $status ), true );
705
706
		$this->assertEquals( 200, $status );
707
		$this->assertEquals( 2, count( $header ) );
708
		$this->assertEquals( 59, count( $result['meta']['resources'] ) );
709
		$this->assertGreaterThan( 0, count( $result['meta']['attributes'] ) );
710
		$this->assertArrayNotHasKey( 'errors', $result );
711
	}
712
713
714 View Code Duplication
	public function testOptionsException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
715
	{
716
		$this->getProductMock( array( 'getResourceType' ) )->expects( $this->once() )->method( 'getResourceType' )
717
			->will( $this->throwException( new \Exception( 'test exception' ) ) );
718
719
		$header = array();
720
		$status = 500;
721
722
		$result = json_decode( $this->object->options( '', $header, $status ), true );
723
724
		$this->assertEquals( 500, $status );
725
		$this->assertArrayHasKey( 'errors', $result );
726
	}
727
728
729 View Code Duplication
	public function testOptionsMAdminException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
730
	{
731
		$this->getProductMock( array( 'getResourceType' ) )->expects( $this->once() )->method( 'getResourceType' )
732
			->will( $this->throwException( new \Aimeos\MAdmin\Exception( 'test exception' ) ) );
733
734
		$header = array();
735
		$status = 500;
736
737
		$result = json_decode( $this->object->options( '', $header, $status ), true );
738
739
		$this->assertEquals( 404, $status );
740
		$this->assertArrayHasKey( 'errors', $result );
741
	}
742
743
744 View Code Duplication
	public function testOptionsMShopException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
745
	{
746
		$this->getProductMock( array( 'getResourceType' ) )->expects( $this->once() )->method( 'getResourceType' )
747
			->will( $this->throwException( new \Aimeos\MShop\Exception( 'test exception' ) ) );
748
749
		$header = array();
750
		$status = 500;
751
752
		$result = json_decode( $this->object->options( '', $header, $status ), true );
753
754
		$this->assertEquals( 404, $status );
755
		$this->assertArrayHasKey( 'errors', $result );
756
	}
757
758
759
	protected function getProductMock( array $methods )
760
	{
761
		$name = 'ClientJsonAdmStandard';
762
		$this->context->getConfig()->set( 'mshop/product/manager/name', $name );
763
764
		$productManagerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Product\\Manager\\Standard' )
765
			->setConstructorArgs( array( $this->context ) )
766
			->setMethods( $methods )
767
			->getMock();
768
769
		\Aimeos\MShop\Product\Manager\Factory::injectManager( '\\Aimeos\\MShop\\Product\\Manager\\' . $name, $productManagerStub );
770
771
		return $productManagerStub;
772
	}
773
774
775
	protected function getProductItem( $code = 'CNC' )
776
	{
777
		$manager = \Aimeos\MShop\Product\Manager\Factory::createManager( $this->context );
778
		$search = $manager->createSearch();
779
		$search->setConditions( $search->compare( '==', 'product.code', $code ) );
780
		$items = $manager->searchItems( $search );
781
782
		if( ( $item = reset( $items ) ) === false ) {
783
			throw new \Exception( sprintf( 'No product item with code "%1$s" found', $code ) );
784
		}
785
786
		return $item;
787
	}
788
}