Completed
Push — master ( be403f...c1329d )
by Aimeos
02:00
created

StandardTest::testOptionsWithPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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