StandardTest::object()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 15
rs 9.9666
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2025
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() : void
20
	{
21
		\Aimeos\Controller\Frontend::cache( true );
22
23
		$this->context = \TestHelper::context();
24
		$this->view = $this->context->view();
25
26
		$this->object = new \Aimeos\Client\JsonApi\Customer\Relationships\Standard( $this->context );
27
		$this->object->setView( $this->view );
28
	}
29
30
31
	protected function tearDown() : void
32
	{
33
		\Aimeos\Controller\Frontend::cache( false );
34
		unset( $this->view, $this->object, $this->context );
35
	}
36
37
38
	public function testDelete()
39
	{
40
		$custManager = \Aimeos\MShop::create( $this->context, 'customer' );
41
		$customer = $custManager->find( '[email protected]', ['product'] )->setCode( 'unittest-jsonapi' );
42
		$customer = $custManager->save( $customer->setId( null ) );
43
		$this->context->setUser( $customer );
0 ignored issues
show
Bug introduced by
It seems like $customer can also be of type Aimeos\Map; however, parameter $user of Aimeos\MShop\ContextIface::setUser() does only seem to accept Closure|null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
		$this->context->setUser( /** @scrutinizer ignore-type */ $customer );
Loading history...
44
45
46
		$params = array( 'id' => $customer->getId() );
47
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
48
		$this->view->addHelper( 'param', $helper );
49
50
		$body = '{"data": {"type": "product", "id": ' . $customer->getListItems()->first()->getId() . '}}';
51
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
52
53
54
		$response = $this->object->delete( $request, $this->view->response() );
55
		$result = json_decode( (string) $response->getBody(), true );
56
57
		$custManager->delete( $customer->getId() );
58
59
60
		$this->assertEquals( 200, $response->getStatusCode() );
61
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
62
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
63
64
		$this->assertEquals( 0, $result['meta']['total'] );
65
		$this->assertArrayNotHasKey( 'errors', $result );
66
	}
67
68
69
	public function testDeleteById()
70
	{
71
		$custManager = \Aimeos\MShop::create( $this->context, 'customer' );
72
		$customer = $custManager->find( '[email protected]', ['product'] )->setCode( 'unittest-jsonapi' );
73
		$customer = $custManager->save( $customer->setId( null ) );
74
		$this->context->setUser( $customer );
0 ignored issues
show
Bug introduced by
It seems like $customer can also be of type Aimeos\Map; however, parameter $user of Aimeos\MShop\ContextIface::setUser() does only seem to accept Closure|null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
		$this->context->setUser( /** @scrutinizer ignore-type */ $customer );
Loading history...
75
76
77
		$params = ['id' => $customer->getId(), 'relatedid' => $customer->getListItems()->first()->getId()];
78
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
79
		$this->view->addHelper( 'param', $helper );
80
81
82
		$response = $this->object->delete( $this->view->request(), $this->view->response() );
83
		$result = json_decode( (string) $response->getBody(), true );
84
85
		$custManager->delete( $customer->getId() );
86
87
88
		$this->assertEquals( 200, $response->getStatusCode() );
89
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
90
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
91
92
		$this->assertEquals( 0, $result['meta']['total'] );
93
		$this->assertArrayNotHasKey( 'errors', $result );
94
	}
95
96
97
	public function testDeleteControllerException()
98
	{
99
		$object = $this->object( 'uses', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
100
101
		$params = array( 'relatedid' => -1 );
102
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
103
		$this->view->addHelper( 'param', $helper );
104
105
		$response = $object->delete( $this->view->request(), $this->view->response() );
106
		$result = json_decode( (string) $response->getBody(), true );
107
108
109
		$this->assertEquals( 403, $response->getStatusCode() );
110
		$this->assertArrayHasKey( 'errors', $result );
111
	}
112
113
114
	public function testDeleteMShopException()
115
	{
116
		$object = $this->object( 'uses', $this->throwException( new \Aimeos\MShop\Exception() ) );
117
118
		$params = array( 'relatedid' => -1 );
119
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
120
		$this->view->addHelper( 'param', $helper );
121
122
		$response = $object->delete( $this->view->request(), $this->view->response() );
123
		$result = json_decode( (string) $response->getBody(), true );
124
125
126
		$this->assertEquals( 404, $response->getStatusCode() );
127
		$this->assertArrayHasKey( 'errors', $result );
128
	}
129
130
131
	public function testDeleteException()
132
	{
133
		$object = $this->object( 'uses', $this->throwException( new \Exception() ) );
134
135
		$params = array( 'relatedid' => -1 );
136
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
137
		$this->view->addHelper( 'param', $helper );
138
139
		$response = $object->delete( $this->view->request(), $this->view->response() );
140
		$result = json_decode( (string) $response->getBody(), true );
141
142
143
		$this->assertEquals( 500, $response->getStatusCode() );
144
		$this->assertArrayHasKey( 'errors', $result );
145
	}
146
147
148
	public function testGet()
149
	{
150
		$customer = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' );
151
		$this->context->setUser( $customer );
152
153
		$params = ['id' => $customer->getId(), 'include' => 'product'];
154
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
155
		$this->view->addHelper( 'param', $helper );
156
157
158
		$response = $this->object->get( $this->view->request(), $this->view->response() );
159
		$result = json_decode( (string) $response->getBody(), true );
160
161
162
		$this->assertEquals( 200, $response->getStatusCode() );
163
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
164
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
165
166
		$this->assertEquals( 2, $result['meta']['total'] );
167
		$this->assertNotNull( $result['data'][0]['id'] );
168
		$this->assertEquals( 'customer.lists', $result['data'][0]['type'] );
169
		$this->assertGreaterThan( 8, count( $result['data'][0]['attributes'] ) );
170
171
		$this->assertArrayNotHasKey( 'errors', $result );
172
	}
173
174
175
	public function testGetById()
176
	{
177
		$customer = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]', ['product'] );
178
		$this->context->setUser( $customer );
179
180
		$params = array(
181
			'id' => $customer->getId(),
182
			'related' => 'relationships',
183
			'relatedid' => $customer->getListItems()->first()->getId(),
184
			'fields' => array( 'customer.lists' => 'customer.lists.id,customer.lists.refid' ),
185
			'include' => 'product',
186
		);
187
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
188
		$this->view->addHelper( 'param', $helper );
189
190
191
		$response = $this->object->get( $this->view->request(), $this->view->response() );
192
		$result = json_decode( (string) $response->getBody(), true );
193
194
195
		$this->assertEquals( 200, $response->getStatusCode() );
196
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
197
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
198
199
		$this->assertEquals( 1, $result['meta']['total'] );
200
		$this->assertEquals( 'customer.lists', $result['data']['type'] );
201
		$this->assertEquals( 2, count( $result['data']['attributes'] ) );
202
		$this->assertNotNull( $result['data']['id'] );
203
204
		$this->assertArrayNotHasKey( 'errors', $result );
205
	}
206
207
208
	public function testGetControllerException()
209
	{
210
		$object = $this->object( 'uses', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
211
212
		$params = array( 'relatedid' => -1 );
213
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
214
		$this->view->addHelper( 'param', $helper );
215
216
		$response = $object->get( $this->view->request(), $this->view->response() );
217
		$result = json_decode( (string) $response->getBody(), true );
218
219
220
		$this->assertEquals( 403, $response->getStatusCode() );
221
		$this->assertArrayHasKey( 'errors', $result );
222
	}
223
224
225
	public function testGetMShopException()
226
	{
227
		$object = $this->object( 'uses', $this->throwException( new \Aimeos\MShop\Exception() ) );
228
229
		$params = array( 'relatedid' => -1 );
230
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
231
		$this->view->addHelper( 'param', $helper );
232
233
		$response = $object->get( $this->view->request(), $this->view->response() );
234
		$result = json_decode( (string) $response->getBody(), true );
235
236
237
		$this->assertEquals( 404, $response->getStatusCode() );
238
		$this->assertArrayHasKey( 'errors', $result );
239
	}
240
241
242
	public function testGetException()
243
	{
244
		$object = $this->object( 'uses', $this->throwException( new \Exception() ) );
245
246
		$params = array( 'relatedid' => -1 );
247
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
248
		$this->view->addHelper( 'param', $helper );
249
250
		$response = $object->get( $this->view->request(), $this->view->response() );
251
		$result = json_decode( (string) $response->getBody(), true );
252
253
254
		$this->assertEquals( 500, $response->getStatusCode() );
255
		$this->assertArrayHasKey( 'errors', $result );
256
	}
257
258
259
	public function testPatch()
260
	{
261
		$custManager = \Aimeos\MShop::create( $this->context, 'customer' );
262
		$customer = $custManager->find( '[email protected]', ['product'] )->setCode( 'unittest-jsonapi' );
263
		$customer = $custManager->save( $customer->setId( null ) );
264
		$id = $customer->getListItems( 'product', 'favorite' )->first()->getId();
265
		$this->context->setUser( $customer );
0 ignored issues
show
Bug introduced by
It seems like $customer can also be of type Aimeos\Map; however, parameter $user of Aimeos\MShop\ContextIface::setUser() does only seem to accept Closure|null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

265
		$this->context->setUser( /** @scrutinizer ignore-type */ $customer );
Loading history...
266
267
268
		$params = ['id' => $customer->getId(), 'relatedid' => $id, 'include' => 'product'];
269
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
270
		$this->view->addHelper( 'param', $helper );
271
272
		$body = '{"data": {"type": "product", "attributes": {
273
			"customer.lists.config": {"key": "value"}
274
		}}}';
275
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
276
277
278
		$response = $this->object->patch( $request, $this->view->response() );
279
		$result = json_decode( (string) $response->getBody(), true );
280
281
		$custManager->delete( $customer->getId() );
282
283
284
		$this->assertEquals( 200, $response->getStatusCode() );
285
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
286
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
287
288
		$this->assertEquals( 1, $result['meta']['total'] );
289
		$this->assertEquals( 'customer.lists', $result['data']['type'] );
290
		$this->assertGreaterThan( 8, count( $result['data']['attributes'] ) );
291
		$this->assertEquals( ['key' => 'value'], $result['data']['attributes']['customer.lists.config'] );
292
293
		$this->assertArrayNotHasKey( 'errors', $result );
294
	}
295
296
297
	public function testPatchControllerException()
298
	{
299
		$object = $this->object( 'uses', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
300
301
		$body = '{"data": {"attributes": []}}';
302
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
303
304
		$response = $object->patch( $request, $this->view->response() );
305
		$result = json_decode( (string) $response->getBody(), true );
306
307
308
		$this->assertEquals( 403, $response->getStatusCode() );
309
		$this->assertArrayHasKey( 'errors', $result );
310
	}
311
312
313
	public function testPatchMShopException()
314
	{
315
		$object = $this->object( 'uses', $this->throwException( new \Aimeos\MShop\Exception() ) );
316
317
		$body = '{"data": {"attributes": []}}';
318
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
319
320
		$response = $object->patch( $request, $this->view->response() );
321
		$result = json_decode( (string) $response->getBody(), true );
322
323
324
		$this->assertEquals( 404, $response->getStatusCode() );
325
		$this->assertArrayHasKey( 'errors', $result );
326
	}
327
328
329
	public function testPatchException()
330
	{
331
		$object = $this->object( 'uses', $this->throwException( new \Exception() ) );
332
333
		$body = '{"data": {"attributes": []}}';
334
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
335
336
		$response = $object->patch( $request, $this->view->response() );
337
		$result = json_decode( (string) $response->getBody(), true );
338
339
340
		$this->assertEquals( 500, $response->getStatusCode() );
341
		$this->assertArrayHasKey( 'errors', $result );
342
	}
343
344
345
	public function testPost()
346
	{
347
		$custManager = \Aimeos\MShop::create( $this->context, 'customer' );
348
		$customer = $custManager->create()->setCode( 'unittest-jsonapi' );
349
		$customer = $custManager->save( $customer->setId( null ) );
350
		$this->context->setUser( $customer );
0 ignored issues
show
Bug introduced by
It seems like $customer can also be of type Aimeos\Map; however, parameter $user of Aimeos\MShop\ContextIface::setUser() does only seem to accept Closure|null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

350
		$this->context->setUser( /** @scrutinizer ignore-type */ $customer );
Loading history...
351
352
353
		$params = ['id' => $customer->getId(), 'include' => 'product'];
354
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
355
		$this->view->addHelper( 'param', $helper );
356
357
		$body = '{"data": {"type": "product", "attributes": {
358
			"customer.lists.domain": "product", "customer.lists.type": "favorite", "customer.lists.refid": "-1"
359
		}}}';
360
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
361
362
		$response = $this->object->post( $request, $this->view->response() );
363
		$result = json_decode( (string) $response->getBody(), true );
364
365
		$custManager->delete( $customer->getId() );
366
367
368
		$this->assertEquals( 201, $response->getStatusCode() );
369
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
370
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
371
372
		$this->assertEquals( 1, $result['meta']['total'] );
373
		$this->assertEquals( 'customer.lists', $result['data'][0]['type'] );
374
		$this->assertEquals( '-1', $result['data'][0]['attributes']['customer.lists.refid'] );
375
376
		$this->assertArrayNotHasKey( 'errors', $result );
377
	}
378
379
380
	public function testPostMultiple()
381
	{
382
		$custManager = \Aimeos\MShop::create( $this->context, 'customer' );
383
		$customer = $custManager->create()->setCode( 'unittest-jsonapi' );
384
		$customer = $custManager->save( $customer->setId( null ) );
385
		$this->context->setUser( $customer );
0 ignored issues
show
Bug introduced by
It seems like $customer can also be of type Aimeos\Map; however, parameter $user of Aimeos\MShop\ContextIface::setUser() does only seem to accept Closure|null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

385
		$this->context->setUser( /** @scrutinizer ignore-type */ $customer );
Loading history...
386
387
388
		$params = ['id' => $customer->getId(), 'include' => 'product'];
389
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
390
		$this->view->addHelper( 'param', $helper );
391
392
		$body = '{"data": [{"type": "product", "attributes": {
393
			"customer.lists.domain": "product", "customer.lists.type": "favorite", "customer.lists.refid": "-1"
394
		}}, {"type": "product", "attributes": {
395
			"customer.lists.domain": "product", "customer.lists.type": "favorite", "customer.lists.refid": "-2"
396
		}}]}';
397
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
398
399
400
		$response = $this->object->post( $request, $this->view->response() );
401
		$result = json_decode( (string) $response->getBody(), true );
402
403
		$custManager->delete( $customer->getId() );
404
405
406
		$this->assertEquals( 201, $response->getStatusCode() );
407
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
408
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
409
410
		$this->assertEquals( 2, $result['meta']['total'] );
411
		$this->assertNotNull( $result['data'][0]['id'] );
412
		$this->assertEquals( 'customer.lists', $result['data'][0]['type'] );
413
		$this->assertLessThan( 0, $result['data'][0]['attributes']['customer.lists.refid'] );
414
		$this->assertLessThan( 0, $result['data'][1]['attributes']['customer.lists.refid'] );
415
416
		$this->assertArrayNotHasKey( 'errors', $result );
417
	}
418
419
420
	public function testPostControllerException()
421
	{
422
		$object = $this->object( 'uses', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
423
424
		$body = '{"data": {"attributes": []}}';
425
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
426
427
		$response = $object->post( $request, $this->view->response() );
428
		$result = json_decode( (string) $response->getBody(), true );
429
430
431
		$this->assertEquals( 403, $response->getStatusCode() );
432
		$this->assertArrayHasKey( 'errors', $result );
433
	}
434
435
436
	public function testPostMShopException()
437
	{
438
		$object = $this->object( 'uses', $this->throwException( new \Aimeos\MShop\Exception() ) );
439
440
		$body = '{"data": {"attributes": []}}';
441
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
442
443
		$response = $object->post( $request, $this->view->response() );
444
		$result = json_decode( (string) $response->getBody(), true );
445
446
447
		$this->assertEquals( 404, $response->getStatusCode() );
448
		$this->assertArrayHasKey( 'errors', $result );
449
	}
450
451
452
	public function testPostException()
453
	{
454
		$object = $this->object( 'uses', $this->throwException( new \Exception() ) );
455
456
		$body = '{"data": {"attributes": []}}';
457
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
458
459
		$response = $object->post( $request, $this->view->response() );
460
		$result = json_decode( (string) $response->getBody(), true );
461
462
463
		$this->assertEquals( 500, $response->getStatusCode() );
464
		$this->assertArrayHasKey( 'errors', $result );
465
	}
466
467
468
	public function testOptions()
469
	{
470
		$response = $this->object->options( $this->view->request(), $this->view->response() );
471
		$result = json_decode( (string) $response->getBody(), true );
472
473
		$this->assertEquals( 200, $response->getStatusCode() );
474
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
475
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
476
477
		$this->assertEquals( null, $result['meta']['prefix'] );
478
		$this->assertEquals( 7, count( $result['meta']['attributes'] ) );
479
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
480
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
481
		$this->assertArrayNotHasKey( 'errors', $result );
482
	}
483
484
485
	/**
486
	 * Returns a test object with a mocked customer controller
487
	 *
488
	 * @param string $method Customer controller method name to mock
489
	 * @param mixed $result Return value of the mocked method
490
	 */
491
	protected function object( $method, $result )
492
	{
493
		$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Customer\Standard::class )
494
			->setConstructorArgs( [$this->context] )
495
			->onlyMethods( [$method] )
496
			->getMock();
497
498
		$cntl->expects( $this->once() )->method( $method )->will( $result );
499
500
		\Aimeos\Controller\Frontend::inject( \Aimeos\Controller\Frontend\Customer\Standard::class, $cntl );
501
502
		$object = new \Aimeos\Client\JsonApi\Customer\Relationships\Standard( $this->context, 'customer/relationships' );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Client\JsonApi\Cu...Standard::__construct() has too many arguments starting with 'customer/relationships'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

502
		$object = /** @scrutinizer ignore-call */ new \Aimeos\Client\JsonApi\Customer\Relationships\Standard( $this->context, 'customer/relationships' );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
503
		$object->setView( $this->view );
504
505
		return $object;
506
	}
507
}
508