Completed
Push — master ( 222791...8272bb )
by Aimeos
02:51
created

StandardTest::testPatch()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 26
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Customer\Relationships;
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 = \TestHelperJapi::getContext();
22
		$templatePaths = \TestHelperJapi::getTemplatePaths();
23
		$this->view = $this->context->getView();
24
25
		$this->object = new \Aimeos\Client\JsonApi\Customer\Relationships\Standard( $this->context, $this->view, $templatePaths, 'customer/relationships' );
26
	}
27
28
29
	protected function tearDown()
30
	{
31
		\Aimeos\Controller\Frontend\Customer\Factory::injectController( '\Aimeos\Controller\Frontend\Customer\Standard', null );
32
	}
33
34
35
	public function testDelete()
36
	{
37
		$custManager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' );
38
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer/lists' );
39
		$typeManager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer/lists/type' );
40
41
		$userId = $custManager->findItem( 'UTC001' )->getId();
42
		$typeId = $typeManager->findItem( 'favorite', [], 'product' )->getId();;
43
44
		$this->context->setUserId( $userId );
45
		$item = $manager->createItem()->setParentId( $userId );
46
		$item->setDomain( 'product' )->setRefId( -1 )->setTypeId( $typeId );
47
		$manager->saveItem( $item );
48
49
50
		$params = array( 'id' => $userId );
51
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
52
		$this->view->addHelper( 'param', $helper );
53
54
		$body = '{"data": {"type": "product", "id": ' . $item->getId() . '}}';
55
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
56
57
58
		$response = $this->object->delete( $request, $this->view->response() );
59
		$result = json_decode( (string) $response->getBody(), true );
60
61
		$this->assertEquals( 200, $response->getStatusCode() );
62
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
63
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
64
65
		$this->assertEquals( 0, $result['meta']['total'] );
66
		$this->assertArrayNotHasKey( 'errors', $result );
67
	}
68
69
70
	public function testDeleteById()
71
	{
72
		$custManager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' );
73
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer/lists' );
74
		$typeManager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer/lists/type' );
75
76
		$userId = $custManager->findItem( 'UTC001' )->getId();
77
		$typeId = $typeManager->findItem( 'favorite', [], 'product' )->getId();;
78
79
		$this->context->setUserId( $userId );
80
		$item = $manager->createItem()->setParentId( $userId );
81
		$item->setDomain( 'product' )->setRefId( -1 )->setTypeId( $typeId );
82
		$manager->saveItem( $item );
83
84
85
		$params = array( 'id' => $userId, 'relatedid' => $item->getId() );
86
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
87
		$this->view->addHelper( 'param', $helper );
88
89
90
		$response = $this->object->delete( $this->view->request(), $this->view->response() );
91
		$result = json_decode( (string) $response->getBody(), true );
92
93
		$this->assertEquals( 200, $response->getStatusCode() );
94
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
95
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
96
97
		$this->assertEquals( 0, $result['meta']['total'] );
98
		$this->assertArrayNotHasKey( 'errors', $result );
99
	}
100
101
102
	public function testDeleteControllerException()
103
	{
104
		$object = $this->getObject( 'deleteListsItem', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
105
106
		$params = array( 'relatedid' => -1 );
107
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
108
		$this->view->addHelper( 'param', $helper );
109
110
		$response = $object->delete( $this->view->request(), $this->view->response() );
111
		$result = json_decode( (string) $response->getBody(), true );
112
113
114
		$this->assertEquals( 403, $response->getStatusCode() );
115
		$this->assertArrayHasKey( 'errors', $result );
116
	}
117
118
119
	public function testDeleteMShopException()
120
	{
121
		$object = $this->getObject( 'deleteListsItem', $this->throwException( new \Aimeos\MShop\Exception() ) );
122
123
		$params = array( 'relatedid' => -1 );
124
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
125
		$this->view->addHelper( 'param', $helper );
126
127
		$response = $object->delete( $this->view->request(), $this->view->response() );
128
		$result = json_decode( (string) $response->getBody(), true );
129
130
131
		$this->assertEquals( 404, $response->getStatusCode() );
132
		$this->assertArrayHasKey( 'errors', $result );
133
	}
134
135
136
	public function testDeleteException()
137
	{
138
		$object = $this->getObject( 'deleteListsItem', $this->throwException( new \Exception() ) );
139
140
		$params = array( 'relatedid' => -1 );
141
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
142
		$this->view->addHelper( 'param', $helper );
143
144
		$response = $object->delete( $this->view->request(), $this->view->response() );
145
		$result = json_decode( (string) $response->getBody(), true );
146
147
148
		$this->assertEquals( 500, $response->getStatusCode() );
149
		$this->assertArrayHasKey( 'errors', $result );
150
	}
151
152
153
	public function testGet()
154
	{
155
		$user = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' )->findItem( 'UTC001' );
156
		$this->context->setUserId( $user->getId() );
157
158
		$params = array( 'id' => $user->getId() );
159
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
160
		$this->view->addHelper( 'param', $helper );
161
162
163
		$response = $this->object->get( $this->view->request(), $this->view->response() );
164
		$result = json_decode( (string) $response->getBody(), true );
165
166
		$this->assertEquals( 200, $response->getStatusCode() );
167
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
168
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
169
170
		$this->assertEquals( 3, $result['meta']['total'] );
171
		$this->assertNotNull( $result['data'][0]['id'] );
172
		$this->assertEquals( 'customer/lists', $result['data'][0]['type'] );
173
		$this->assertGreaterThan( 8, count( $result['data'][0]['attributes'] ) );
174
175
		$this->assertArrayNotHasKey( 'errors', $result );
176
	}
177
178
179
	public function testGetById()
180
	{
181
		$user = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' )->findItem( 'UTC001', ['product'] );
182
		$this->context->setUserId( $user->getId() );
183
		$listsItems = $user->getListItems();
184
185
		if( ( $listsItem = reset( $listsItems ) ) === false ) {
186
			throw new \RuntimeException( 'No lists item found for "UTC001"' );
187
		}
188
189
		$params = array(
190
			'id' => $user->getId(),
191
			'related' => 'relationships',
192
			'relatedid' => $listsItem->getId(),
193
			'fields' => array( 'customer/lists' => 'customer.lists.id,customer.lists.refid' ),
194
		);
195
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
196
		$this->view->addHelper( 'param', $helper );
197
198
199
		$response = $this->object->get( $this->view->request(), $this->view->response() );
200
		$result = json_decode( (string) $response->getBody(), true );
201
202
		$this->assertEquals( 200, $response->getStatusCode() );
203
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
204
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
205
206
		$this->assertEquals( 1, $result['meta']['total'] );
207
		$this->assertEquals( 'customer/lists', $result['data']['type'] );
208
		$this->assertEquals( 2, count( $result['data']['attributes'] ) );
209
		$this->assertNotNull( $result['data']['id'] );
210
211
		$this->assertArrayNotHasKey( 'errors', $result );
212
	}
213
214
215
	public function testGetControllerException()
216
	{
217
		$object = $this->getObject( 'getListsItem', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
218
219
		$params = array( 'relatedid' => -1 );
220
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
221
		$this->view->addHelper( 'param', $helper );
222
223
		$response = $object->get( $this->view->request(), $this->view->response() );
224
		$result = json_decode( (string) $response->getBody(), true );
225
226
227
		$this->assertEquals( 403, $response->getStatusCode() );
228
		$this->assertArrayHasKey( 'errors', $result );
229
	}
230
231
232
	public function testGetMShopException()
233
	{
234
		$object = $this->getObject( 'getListsItem', $this->throwException( new \Aimeos\MShop\Exception() ) );
235
236
		$params = array( 'relatedid' => -1 );
237
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
238
		$this->view->addHelper( 'param', $helper );
239
240
		$response = $object->get( $this->view->request(), $this->view->response() );
241
		$result = json_decode( (string) $response->getBody(), true );
242
243
244
		$this->assertEquals( 404, $response->getStatusCode() );
245
		$this->assertArrayHasKey( 'errors', $result );
246
	}
247
248
249
	public function testGetException()
250
	{
251
		$object = $this->getObject( 'getListsItem', $this->throwException( new \Exception() ) );
252
253
		$params = array( 'relatedid' => -1 );
254
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
255
		$this->view->addHelper( 'param', $helper );
256
257
		$response = $object->get( $this->view->request(), $this->view->response() );
258
		$result = json_decode( (string) $response->getBody(), true );
259
260
261
		$this->assertEquals( 500, $response->getStatusCode() );
262
		$this->assertArrayHasKey( 'errors', $result );
263
	}
264
265
266
	public function testPatch()
267
	{
268
		$custManager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' );
269
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer/lists' );
270
		$typeManager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer/lists/type' );
271
272
		$userId = $custManager->findItem( 'UTC001' )->getId();
273
		$typeId = $typeManager->findItem( 'favorite', [], 'product' )->getId();;
274
275
		$this->context->setUserId( $userId );
276
		$item = $manager->createItem()->setParentId( $userId );
277
		$item->setDomain( 'product' )->setRefId( -1 )->setTypeId( $typeId );
278
		$manager->saveItem( $item );
279
280
		$params = array( 'id' => $item->getId(), 'relatedid' => $item->getId() );
281
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
282
		$this->view->addHelper( 'param', $helper );
283
284
		$body = '{"data": {"type": "product", "attributes": {
285
			"customer.lists.domain": "product", "customer.lists.type": "favorite", "customer.lists.refid": "-2"
286
		}}}';
287
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
288
289
		$response = $this->object->patch( $request, $this->view->response() );
290
		$result = json_decode( (string) $response->getBody(), true );
291
292
		$manager->deleteItem( $item->getId() );
293
294
295
		$this->assertEquals( 200, $response->getStatusCode() );
296
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
297
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
298
299
		$this->assertEquals( 1, $result['meta']['total'] );
300
		$this->assertEquals( 'customer/lists', $result['data']['type'] );
301
		$this->assertGreaterThan( 8, count( $result['data']['attributes'] ) );
302
		$this->assertEquals( '-2', $result['data']['attributes']['customer.lists.refid'] );
303
304
		$this->assertArrayNotHasKey( 'errors', $result );
305
	}
306
307
308
	public function testPatchControllerException()
309
	{
310
		$object = $this->getObject( 'editListsItem', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
311
312
		$body = '{"data": {"attributes": []}}';
313
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
314
315
		$response = $object->patch( $request, $this->view->response() );
316
		$result = json_decode( (string) $response->getBody(), true );
317
318
319
		$this->assertEquals( 403, $response->getStatusCode() );
320
		$this->assertArrayHasKey( 'errors', $result );
321
	}
322
323
324
	public function testPatchMShopException()
325
	{
326
		$object = $this->getObject( 'editListsItem', $this->throwException( new \Aimeos\MShop\Exception() ) );
327
328
		$body = '{"data": {"attributes": []}}';
329
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
330
331
		$response = $object->patch( $request, $this->view->response() );
332
		$result = json_decode( (string) $response->getBody(), true );
333
334
335
		$this->assertEquals( 404, $response->getStatusCode() );
336
		$this->assertArrayHasKey( 'errors', $result );
337
	}
338
339
340
	public function testPatchException()
341
	{
342
		$object = $this->getObject( 'editListsItem', $this->throwException( new \Exception() ) );
343
344
		$body = '{"data": {"attributes": []}}';
345
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
346
347
		$response = $object->patch( $request, $this->view->response() );
348
		$result = json_decode( (string) $response->getBody(), true );
349
350
351
		$this->assertEquals( 500, $response->getStatusCode() );
352
		$this->assertArrayHasKey( 'errors', $result );
353
	}
354
355
356
	public function testPost()
357
	{
358
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' );
359
		$userId = $manager->findItem( 'UTC001' )->getId();
360
		$this->context->setUserId( $userId );
361
362
		$params = array( 'id' => $userId );
363
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
364
		$this->view->addHelper( 'param', $helper );
365
366
		$body = '{"data": {"type": "product", "attributes": {
367
			"customer.lists.domain": "product", "customer.lists.type": "favorite", "customer.lists.refid": "-1"
368
		}}}';
369
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
370
371
		$response = $this->object->post( $request, $this->view->response() );
372
		$result = json_decode( (string) $response->getBody(), true );
373
374
375
		$this->assertEquals( 201, $response->getStatusCode() );
376
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
377
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
378
379
		$this->assertEquals( 1, $result['meta']['total'] );
380
		$this->assertEquals( 'customer/lists', $result['data'][0]['type'] );
381
		$this->assertEquals( '-1', $result['data'][0]['attributes']['customer.lists.refid'] );
382
383
		$this->assertArrayNotHasKey( 'errors', $result );
384
385
386
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer/lists' );
387
		$manager->deleteItem( $result['data'][0]['id'] );
388
	}
389
390
391
	public function testPostMultiple()
392
	{
393
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' );
394
		$userId = $manager->findItem( 'UTC001' )->getId();
395
		$this->context->setUserId( $userId );
396
397
		$params = array( 'id' => $userId );
398
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
399
		$this->view->addHelper( 'param', $helper );
400
401
		$body = '{"data": [{"type": "product", "attributes": {
402
			"customer.lists.domain": "product", "customer.lists.type": "favorite", "customer.lists.refid": "-1"
403
		}}, {"type": "product", "attributes": {
404
			"customer.lists.domain": "product", "customer.lists.type": "favorite", "customer.lists.refid": "-2"
405
		}}]}';
406
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
407
408
		$response = $this->object->post( $request, $this->view->response() );
409
		$result = json_decode( (string) $response->getBody(), true );
410
411
412
		$this->assertEquals( 201, $response->getStatusCode() );
413
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
414
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
415
416
		$this->assertEquals( 2, $result['meta']['total'] );
417
		$this->assertNotNull( $result['data'][0]['id'] );
418
		$this->assertEquals( 'customer/lists', $result['data'][0]['type'] );
419
		$this->assertEquals( '-1', $result['data'][0]['attributes']['customer.lists.refid'] );
420
		$this->assertEquals( '-2', $result['data'][1]['attributes']['customer.lists.refid'] );
421
422
		$this->assertArrayNotHasKey( 'errors', $result );
423
424
425
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer/lists' );
426
		$manager->deleteItems( [$result['data'][0]['id'], $result['data'][1]['id']] );
427
	}
428
429
430
	public function testPostControllerException()
431
	{
432
		$object = $this->getObject( 'addListsItem', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
433
434
		$body = '{"data": {"attributes": []}}';
435
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
436
437
		$response = $object->post( $request, $this->view->response() );
438
		$result = json_decode( (string) $response->getBody(), true );
439
440
441
		$this->assertEquals( 403, $response->getStatusCode() );
442
		$this->assertArrayHasKey( 'errors', $result );
443
	}
444
445
446
	public function testPostMShopException()
447
	{
448
		$object = $this->getObject( 'addListsItem', $this->throwException( new \Aimeos\MShop\Exception() ) );
449
450
		$body = '{"data": {"attributes": []}}';
451
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
452
453
		$response = $object->post( $request, $this->view->response() );
454
		$result = json_decode( (string) $response->getBody(), true );
455
456
457
		$this->assertEquals( 404, $response->getStatusCode() );
458
		$this->assertArrayHasKey( 'errors', $result );
459
	}
460
461
462
	public function testPostException()
463
	{
464
		$object = $this->getObject( 'addListsItem', $this->throwException( new \Exception() ) );
465
466
		$body = '{"data": {"attributes": []}}';
467
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
468
469
		$response = $object->post( $request, $this->view->response() );
470
		$result = json_decode( (string) $response->getBody(), true );
471
472
473
		$this->assertEquals( 500, $response->getStatusCode() );
474
		$this->assertArrayHasKey( 'errors', $result );
475
	}
476
477
478
	public function testOptions()
479
	{
480
		$response = $this->object->options( $this->view->request(), $this->view->response() );
481
		$result = json_decode( (string) $response->getBody(), true );
482
483
		$this->assertEquals( 200, $response->getStatusCode() );
484
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
485
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
486
487
		$this->assertEquals( null, $result['meta']['prefix'] );
488
		$this->assertEquals( 7, count( $result['meta']['attributes'] ) );
489
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
490
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
491
		$this->assertArrayNotHasKey( 'errors', $result );
492
	}
493
494
495
	/**
496
	 * Returns a test object with a mocked customer controller
497
	 *
498
	 * @param string $method Customer controller method name to mock
499
	 * @param mixed $result Return value of the mocked method
500
	 */
501
	protected function getObject( $method, $result )
502
	{
503
		$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Customer\Standard' )
504
			->setConstructorArgs( [$this->context] )
505
			->setMethods( [$method] )
506
			->getMock();
507
508
		$cntl->expects( $this->once() )->method( $method )->will( $result );
509
510
		\Aimeos\Controller\Frontend\Customer\Factory::injectController( '\Aimeos\Controller\Frontend\Customer\Standard', $cntl );
511
512
		$templatePaths = \TestHelperJapi::getTemplatePaths();
513
		$object = new \Aimeos\Client\JsonApi\Customer\Relationships\Standard( $this->context, $this->view, $templatePaths, 'customer/relationships' );
514
515
		return $object;
516
	}
517
}
518