Completed
Push — master ( 98c885...360704 )
by Aimeos
03:00
created

StandardTest::getProductListsMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
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 testDeleteMAdminException()
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
	public function testDeleteMShopException()
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
	public function testGetException()
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
	public function testGetMAdminException()
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
	public function testGetMShopException()
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
	public function testPatchBulk()
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
	public function testPatchInvalid()
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
	public function testPatchInvalidId()
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
	public function testPatchException()
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
	public function testPatchMAdminException()
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
	public function testPatchMShopException()
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
	public function testPostBulk()
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
	public function testPostRelationships()
609
	{
610
		$productManagerStub = $this->getProductMock( array( 'getSubManager', 'getItem', 'saveItem' ) );
611
		$productManagerListsStub = $this->getProductListsMock( array( 'saveItem' ) );
612
613
		$item = $productManagerStub->createItem();
614
		$item->setLabel( 'test' );
615
		$item->setId( '-1' );
616
617
		$productManagerStub->expects( $this->once() )->method( 'getItem' )
618
			->will( $this->returnValue( $item ) );
619
		$productManagerStub->expects( $this->once() )->method( 'getSubManager' )
620
			->will( $this->returnValue( $productManagerListsStub ) );
621
		$productManagerStub->expects( $this->once() )->method( 'saveItem' );
622
623
		$productManagerListsStub->expects( $this->once() )->method( 'saveItem' );
624
625
		$body = '{"data": {"type": "product",
626
			"attributes": {"product.label": "test"},
627
			"relationships": {"text": {"data": [
628
				{"type": "text", "id": "-2", "attributes": {"product.lists.typeid": "10"}}
629
			]}}
630
		}}';
631
		$header = array();
632
		$status = 500;
633
634
		$result = json_decode( $this->object->post( $body, $header, $status ), true );
635
636
		$this->assertEquals( 201, $status );
637
		$this->assertEquals( 1, count( $header ) );
638
		$this->assertEquals( 1, $result['meta']['total'] );
639
		$this->assertArrayHasKey( 'data', $result );
640
		$this->assertEquals( '-1', $result['data']['id'] );
641
		$this->assertEquals( 'product', $result['data']['type'] );
642
		$this->assertEquals( 'test', $result['data']['attributes']['product.label'] );
643
		$this->assertArrayNotHasKey( 'included', $result );
644
		$this->assertArrayNotHasKey( 'errors', $result );
645
	}
646
647
648
	public function testPostInvalid()
649
	{
650
		$body = '{"data":null}';
651
		$header = array();
652
		$status = 500;
653
654
		$result = json_decode( $this->object->post( $body, $header, $status ), true );
655
656
		$this->assertEquals( 400, $status );
657
		$this->assertEquals( 1, count( $header ) );
658
		$this->assertEquals( 0, $result['meta']['total'] );
659
		$this->assertArrayHasKey( 'errors', $result );
660
		$this->assertArrayNotHasKey( 'included', $result );
661
		$this->assertArrayNotHasKey( 'data', $result );
662
	}
663
664
665
	public function testPostInvalidId()
666
	{
667
		$body = '{"data":{"id":-1}}';
668
		$header = array();
669
		$status = 500;
670
671
		$result = json_decode( $this->object->post( $body, $header, $status ), true );
672
673
		$this->assertEquals( 403, $status );
674
		$this->assertEquals( 1, count( $header ) );
675
		$this->assertEquals( 0, $result['meta']['total'] );
676
		$this->assertArrayHasKey( 'errors', $result );
677
	}
678
679
680
	public function testPostException()
681
	{
682
		$this->getProductMock( array( 'saveItem' ) )->expects( $this->once() )->method( 'saveItem' )
683
			->will( $this->throwException( new \Exception( 'test exception' ) ) );
684
685
		$header = array();
686
		$status = 500;
687
688
		$result = json_decode( $this->object->post( '{"data":{}}', $header, $status ), true );
689
690
		$this->assertEquals( 500, $status );
691
		$this->assertArrayHasKey( 'errors', $result );
692
	}
693
694
695
	public function testPostMAdminException()
696
	{
697
		$this->getProductMock( array( 'saveItem' ) )->expects( $this->once() )->method( 'saveItem' )
698
			->will( $this->throwException( new \Aimeos\MAdmin\Exception( 'test exception' ) ) );
699
700
		$header = array();
701
		$status = 500;
702
703
		$result = json_decode( $this->object->post( '{"data":{}}', $header, $status ), true );
704
705
		$this->assertEquals( 404, $status );
706
		$this->assertArrayHasKey( 'errors', $result );
707
	}
708
709
710
	public function testPostMShopException()
711
	{
712
		$this->getProductMock( array( 'saveItem' ) )->expects( $this->once() )->method( 'saveItem' )
713
			->will( $this->throwException( new \Aimeos\MShop\Exception( 'test exception' ) ) );
714
715
		$header = array();
716
		$status = 500;
717
718
		$result = json_decode( $this->object->post( '{"data":{}}', $header, $status ), true );
719
720
		$this->assertEquals( 404, $status );
721
		$this->assertArrayHasKey( 'errors', $result );
722
	}
723
724
725
	public function testPut()
726
	{
727
		$body = '';
728
		$header = array();
729
		$status = 500;
730
731
		$result = json_decode( $this->object->put( $body, $header, $status ), true );
732
733
		$this->assertEquals( 501, $status );
734
		$this->assertEquals( 1, count( $header ) );
735
		$this->assertArrayHasKey( 'errors', $result );
736
	}
737
738
739
	public function testOptions()
740
	{
741
		$header = array();
742
		$status = 500;
743
744
		$result = json_decode( $this->object->options( '', $header, $status ), true );
745
746
		$this->assertEquals( 200, $status );
747
		$this->assertEquals( 2, count( $header ) );
748
		$this->assertEquals( 59, count( $result['meta']['resources'] ) );
749
		$this->assertGreaterThan( 0, count( $result['meta']['attributes'] ) );
750
		$this->assertArrayNotHasKey( 'errors', $result );
751
	}
752
753
754
	public function testOptionsException()
755
	{
756
		$this->getProductMock( array( 'getResourceType' ) )->expects( $this->once() )->method( 'getResourceType' )
757
			->will( $this->throwException( new \Exception( 'test exception' ) ) );
758
759
		$header = array();
760
		$status = 500;
761
762
		$result = json_decode( $this->object->options( '', $header, $status ), true );
763
764
		$this->assertEquals( 500, $status );
765
		$this->assertArrayHasKey( 'errors', $result );
766
	}
767
768
769
	public function testOptionsMAdminException()
770
	{
771
		$this->getProductMock( array( 'getResourceType' ) )->expects( $this->once() )->method( 'getResourceType' )
772
			->will( $this->throwException( new \Aimeos\MAdmin\Exception( 'test exception' ) ) );
773
774
		$header = array();
775
		$status = 500;
776
777
		$result = json_decode( $this->object->options( '', $header, $status ), true );
778
779
		$this->assertEquals( 404, $status );
780
		$this->assertArrayHasKey( 'errors', $result );
781
	}
782
783
784
	public function testOptionsMShopException()
785
	{
786
		$this->getProductMock( array( 'getResourceType' ) )->expects( $this->once() )->method( 'getResourceType' )
787
			->will( $this->throwException( new \Aimeos\MShop\Exception( 'test exception' ) ) );
788
789
		$header = array();
790
		$status = 500;
791
792
		$result = json_decode( $this->object->options( '', $header, $status ), true );
793
794
		$this->assertEquals( 404, $status );
795
		$this->assertArrayHasKey( 'errors', $result );
796
	}
797
798
799
	protected function getProductMock( array $methods )
800
	{
801
		$name = 'ClientJsonAdmStandard';
802
		$this->context->getConfig()->set( 'mshop/product/manager/name', $name );
803
804
		$stub = $this->getMockBuilder( '\\Aimeos\\MShop\\Product\\Manager\\Standard' )
805
			->setConstructorArgs( array( $this->context ) )
806
			->setMethods( $methods )
807
			->getMock();
808
809
		\Aimeos\MShop\Product\Manager\Factory::injectManager( '\\Aimeos\\MShop\\Product\\Manager\\' . $name, $stub );
810
811
		return $stub;
812
	}
813
814
815
	protected function getProductListsMock( array $methods )
816
	{
817
		$name = 'ClientJsonAdmStandard';
818
		$this->context->getConfig()->set( 'mshop/product/manager/lists/name', $name );
819
820
		$stub = $this->getMockBuilder( '\\Aimeos\\MShop\\Product\\Manager\\Lists\\Standard' )
821
			->setConstructorArgs( array( $this->context ) )
822
			->setMethods( $methods )
823
			->getMock();
824
825
		\Aimeos\MShop\Product\Manager\Factory::injectManager( '\\Aimeos\\MShop\\Product\\Manager\\Lists\\' . $name, $stub );
826
827
		return $stub;
828
	}
829
830
831
	protected function getProductItem( $code = 'CNC' )
832
	{
833
		$manager = \Aimeos\MShop\Product\Manager\Factory::createManager( $this->context );
834
		$search = $manager->createSearch();
835
		$search->setConditions( $search->compare( '==', 'product.code', $code ) );
836
		$items = $manager->searchItems( $search );
837
838
		if( ( $item = reset( $items ) ) === false ) {
839
			throw new \Exception( sprintf( 'No product item with code "%1$s" found', $code ) );
840
		}
841
842
		return $item;
843
	}
844
}