Passed
Push — master ( 413d3b...72c491 )
by Aimeos
06:13 queued 03:36
created

StandardTest::object()   A

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), 2019-2021
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Customer\Property;
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 = \TestHelperJapi::getContext();
24
		$this->view = $this->context->view();
25
26
		$this->object = new \Aimeos\Client\JsonApi\Customer\Property\Standard( $this->context, 'customer/property' );
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]', ['customer/property'] )->setCode( 'unittest-jsonapi' );
42
		$customer = $custManager->save( $customer->setId( null ) );
43
		$this->context->setUserId( $customer->getId() );
0 ignored issues
show
Bug introduced by
It seems like $customer->getId() can also be of type Aimeos\Map; however, parameter $user of Aimeos\MShop\Context\Item\Iface::setUserId() 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->setUserId( /** @scrutinizer ignore-type */ $customer->getId() );
Loading history...
44
45
46
		$params = ['id' => $customer->getId()];
47
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
48
		$this->view->addHelper( 'param', $helper );
49
50
		$body = '{"data": {"type": "customer/property", "id": ' . $customer->getPropertyItems()->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]', ['customer/property'] )->setCode( 'unittest-jsonapi' );
73
		$customer = $custManager->save( $customer->setId( null ) );
74
		$this->context->setUserId( $customer->getId() );
0 ignored issues
show
Bug introduced by
It seems like $customer->getId() can also be of type Aimeos\Map; however, parameter $user of Aimeos\MShop\Context\Item\Iface::setUserId() 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->setUserId( /** @scrutinizer ignore-type */ $customer->getId() );
Loading history...
75
76
77
		$params = ['id' => $customer->getId(), 'relatedid' => $customer->getPropertyItems()->first()->getId()];
78
		$helper = new \Aimeos\MW\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
		$response = $object->delete( $this->view->request(), $this->view->response() );
101
		$result = json_decode( (string) $response->getBody(), true );
102
103
104
		$this->assertEquals( 403, $response->getStatusCode() );
105
		$this->assertArrayHasKey( 'errors', $result );
106
	}
107
108
109
	public function testDeleteMShopException()
110
	{
111
		$object = $this->object( 'uses', $this->throwException( new \Aimeos\MShop\Exception() ) );
112
		$response = $object->delete( $this->view->request(), $this->view->response() );
113
		$result = json_decode( (string) $response->getBody(), true );
114
115
116
		$this->assertEquals( 404, $response->getStatusCode() );
117
		$this->assertArrayHasKey( 'errors', $result );
118
	}
119
120
121
	public function testDeleteException()
122
	{
123
		$object = $this->object( 'uses', $this->throwException( new \Exception() ) );
124
		$response = $object->delete( $this->view->request(), $this->view->response() );
125
		$result = json_decode( (string) $response->getBody(), true );
126
127
128
		$this->assertEquals( 500, $response->getStatusCode() );
129
		$this->assertArrayHasKey( 'errors', $result );
130
	}
131
132
133
	public function testGet()
134
	{
135
		$customer = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' );
136
		$this->context->setUserId( $customer->getId() );
137
138
		$params = ['id' => $customer->getId()];
139
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
140
		$this->view->addHelper( 'param', $helper );
141
142
143
		$response = $this->object->get( $this->view->request(), $this->view->response() );
144
		$result = json_decode( (string) $response->getBody(), true );
145
146
		$this->assertEquals( 200, $response->getStatusCode() );
147
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
148
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
149
150
		$this->assertEquals( 1, $result['meta']['total'] );
151
		$this->assertEquals( 'customer/property', $result['data'][0]['type'] );
152
		$this->assertNotNull( $result['data'][0]['id'] );
153
		$this->assertGreaterThan( 3, count( $result['data'][0]['attributes'] ) );
154
155
		$this->assertArrayNotHasKey( 'errors', $result );
156
	}
157
158
159
	public function testGetById()
160
	{
161
		$customer = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]', ['customer/property'] );
162
		$id = $customer->getPropertyItems()->first()->getId();
163
		$this->context->setUserId( $customer->getId() );
164
165
		$params = array(
166
			'id' => $customer->getId(),
167
			'related' => 'property',
168
			'relatedid' => $id,
169
			'fields' => ['customer/property' => 'customer.property.id,customer.property.value'],
170
		);
171
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
172
		$this->view->addHelper( 'param', $helper );
173
174
175
		$response = $this->object->get( $this->view->request(), $this->view->response() );
176
		$result = json_decode( (string) $response->getBody(), true );
177
178
		$this->assertEquals( 200, $response->getStatusCode() );
179
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
180
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
181
182
		$this->assertEquals( 1, $result['meta']['total'] );
183
		$this->assertEquals( 'customer/property', $result['data']['type'] );
184
		$this->assertEquals( 2, count( $result['data']['attributes'] ) );
185
		$this->assertNotNull( $result['data']['id'] );
186
187
		$this->assertArrayNotHasKey( 'errors', $result );
188
	}
189
190
191
	public function testGetControllerException()
192
	{
193
		$object = $this->object( 'uses', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
194
		$response = $object->get( $this->view->request(), $this->view->response() );
195
		$result = json_decode( (string) $response->getBody(), true );
196
197
198
		$this->assertEquals( 403, $response->getStatusCode() );
199
		$this->assertArrayHasKey( 'errors', $result );
200
	}
201
202
203
	public function testGetMShopException()
204
	{
205
		$object = $this->object( 'uses', $this->throwException( new \Aimeos\MShop\Exception() ) );
206
		$response = $object->get( $this->view->request(), $this->view->response() );
207
		$result = json_decode( (string) $response->getBody(), true );
208
209
210
		$this->assertEquals( 404, $response->getStatusCode() );
211
		$this->assertArrayHasKey( 'errors', $result );
212
	}
213
214
215
	public function testGetException()
216
	{
217
		$object = $this->object( 'uses', $this->throwException( new \Exception() ) );
218
		$response = $object->get( $this->view->request(), $this->view->response() );
219
		$result = json_decode( (string) $response->getBody(), true );
220
221
222
		$this->assertEquals( 500, $response->getStatusCode() );
223
		$this->assertArrayHasKey( 'errors', $result );
224
	}
225
226
227
	public function testPatch()
228
	{
229
		$custManager = \Aimeos\MShop::create( $this->context, 'customer' );
230
		$customer = $custManager->find( '[email protected]', ['customer/property'] )->setCode( 'unittest-jsonapi' );
231
		$customer = $custManager->save( $customer->setId( null ) );
232
		$this->context->setUserId( $customer->getId() );
0 ignored issues
show
Bug introduced by
It seems like $customer->getId() can also be of type Aimeos\Map; however, parameter $user of Aimeos\MShop\Context\Item\Iface::setUserId() 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

232
		$this->context->setUserId( /** @scrutinizer ignore-type */ $customer->getId() );
Loading history...
233
234
235
		$params = ['id' => $customer->getId(), 'relatedid' => $customer->getPropertyItems()->first()->getId()];
236
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
237
		$this->view->addHelper( 'param', $helper );
238
239
		$body = '{"data": {"attributes": {"customer.property.value": "test"}}}	';
240
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
241
242
243
		$response = $this->object->patch( $request, $this->view->response() );
244
		$result = json_decode( (string) $response->getBody(), true );
245
246
		$custManager->delete( $customer->getId() );
247
248
249
		$this->assertEquals( 200, $response->getStatusCode() );
250
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
251
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
252
253
		$this->assertEquals( 1, $result['meta']['total'] );
254
		$this->assertEquals( 'customer/property', $result['data']['type'] );
255
		$this->assertGreaterThan( 3, count( $result['data']['attributes'] ) );
256
		$this->assertEquals( 'test', $result['data']['attributes']['customer.property.value'] );
257
258
		$this->assertArrayNotHasKey( 'errors', $result );
259
	}
260
261
262
	public function testPatchControllerException()
263
	{
264
		$object = $this->object( 'uses', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
265
266
		$body = '{"data": {"attributes": []}}';
267
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
268
269
		$response = $object->patch( $request, $this->view->response() );
270
		$result = json_decode( (string) $response->getBody(), true );
271
272
273
		$this->assertEquals( 403, $response->getStatusCode() );
274
		$this->assertArrayHasKey( 'errors', $result );
275
	}
276
277
278
	public function testPatchMShopException()
279
	{
280
		$object = $this->object( 'uses', $this->throwException( new \Aimeos\MShop\Exception() ) );
281
282
		$body = '{"data": {"attributes": []}}';
283
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
284
285
		$response = $object->patch( $request, $this->view->response() );
286
		$result = json_decode( (string) $response->getBody(), true );
287
288
289
		$this->assertEquals( 404, $response->getStatusCode() );
290
		$this->assertArrayHasKey( 'errors', $result );
291
	}
292
293
294
	public function testPatchException()
295
	{
296
		$object = $this->object( 'uses', $this->throwException( new \Exception() ) );
297
298
		$body = '{"data": {"attributes": []}}';
299
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
300
301
		$response = $object->patch( $request, $this->view->response() );
302
		$result = json_decode( (string) $response->getBody(), true );
303
304
305
		$this->assertEquals( 500, $response->getStatusCode() );
306
		$this->assertArrayHasKey( 'errors', $result );
307
	}
308
309
310
	public function testPost()
311
	{
312
		$custManager = \Aimeos\MShop::create( $this->context, 'customer' );
313
		$customer = $custManager->create()->setCode( 'unittest-jsonapi' );
314
		$customer = $custManager->save( $customer );
315
		$this->context->setUserId( $customer->getId() );
0 ignored issues
show
Bug introduced by
It seems like $customer->getId() can also be of type Aimeos\Map; however, parameter $user of Aimeos\MShop\Context\Item\Iface::setUserId() 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

315
		$this->context->setUserId( /** @scrutinizer ignore-type */ $customer->getId() );
Loading history...
316
317
318
		$params = ['id' => $customer->getId()];
319
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
320
		$this->view->addHelper( 'param', $helper );
321
322
		$body = '{"data": {"type": "customer/property", "attributes": {"customer.property.type": "testtype", "customer.property.value": "test"}}}';
323
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
324
325
326
		$response = $this->object->post( $request, $this->view->response() );
327
		$result = json_decode( (string) $response->getBody(), true );
328
329
		$custManager->delete( $customer->getId() );
330
331
332
		$this->assertEquals( 201, $response->getStatusCode() );
333
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
334
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
335
336
		$this->assertEquals( 1, $result['meta']['total'] );
337
		$this->assertEquals( 'customer/property', $result['data'][0]['type'] );
338
		$this->assertEquals( 'testtype', $result['data'][0]['attributes']['customer.property.type'] );
339
		$this->assertEquals( 'test', $result['data'][0]['attributes']['customer.property.value'] );
340
341
		$this->assertArrayNotHasKey( 'errors', $result );
342
	}
343
344
345
	public function testPostMultiple()
346
	{
347
		$custManager = \Aimeos\MShop::create( $this->context, 'customer' );
348
		$customer = $custManager->create()->setCode( 'unittest-jsonapi' );
349
		$customer = $custManager->save( $customer );
350
		$this->context->setUserId( $customer->getId() );
0 ignored issues
show
Bug introduced by
It seems like $customer->getId() can also be of type Aimeos\Map; however, parameter $user of Aimeos\MShop\Context\Item\Iface::setUserId() 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->setUserId( /** @scrutinizer ignore-type */ $customer->getId() );
Loading history...
351
352
353
		$params = ['id' => $customer->getId()];
354
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
355
		$this->view->addHelper( 'param', $helper );
356
357
		$body = '{"data": [
358
			{"type": "customer/property", "attributes": {"customer.property.type": "testtype", "customer.property.value": "test"}},
359
			{"type": "customer/property", "attributes": {"customer.property.type": "testtype", "customer.property.value": "test2"}}
360
		]}';
361
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
362
363
364
		$response = $this->object->post( $request, $this->view->response() );
365
		$result = json_decode( (string) $response->getBody(), true );
366
367
		$custManager->delete( $customer->getId() );
368
369
370
		$this->assertEquals( 201, $response->getStatusCode() );
371
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
372
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
373
374
		$this->assertEquals( 2, $result['meta']['total'] );
375
		$this->assertNotNull( $result['data'][1]['id'] );
376
		$this->assertEquals( 'customer/property', $result['data'][1]['type'] );
377
		$this->assertEquals( 'testtype', $result['data'][0]['attributes']['customer.property.type'] );
378
		$this->assertEquals( 'test', $result['data'][0]['attributes']['customer.property.value'] );
379
		$this->assertEquals( 'testtype', $result['data'][1]['attributes']['customer.property.type'] );
380
		$this->assertEquals( 'test2', $result['data'][1]['attributes']['customer.property.value'] );
381
382
		$this->assertArrayNotHasKey( 'errors', $result );
383
	}
384
385
386
	public function testPostControllerException()
387
	{
388
		$object = $this->object( 'uses', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
389
390
		$body = '{"data": {"attributes": []}}';
391
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
392
393
		$response = $object->post( $request, $this->view->response() );
394
		$result = json_decode( (string) $response->getBody(), true );
395
396
397
		$this->assertEquals( 403, $response->getStatusCode() );
398
		$this->assertArrayHasKey( 'errors', $result );
399
	}
400
401
402
	public function testPostMShopException()
403
	{
404
		$object = $this->object( 'uses', $this->throwException( new \Aimeos\MShop\Exception() ) );
405
406
		$body = '{"data": {"attributes": []}}';
407
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
408
409
		$response = $object->post( $request, $this->view->response() );
410
		$result = json_decode( (string) $response->getBody(), true );
411
412
413
		$this->assertEquals( 404, $response->getStatusCode() );
414
		$this->assertArrayHasKey( 'errors', $result );
415
	}
416
417
418
	public function testPostException()
419
	{
420
		$object = $this->object( 'uses', $this->throwException( new \Exception() ) );
421
422
		$body = '{"data": {"attributes": []}}';
423
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
424
425
		$response = $object->post( $request, $this->view->response() );
426
		$result = json_decode( (string) $response->getBody(), true );
427
428
429
		$this->assertEquals( 500, $response->getStatusCode() );
430
		$this->assertArrayHasKey( 'errors', $result );
431
	}
432
433
434
	public function testOptions()
435
	{
436
		$response = $this->object->options( $this->view->request(), $this->view->response() );
437
		$result = json_decode( (string) $response->getBody(), true );
438
439
		$this->assertEquals( 200, $response->getStatusCode() );
440
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
441
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
442
443
		$this->assertEquals( null, $result['meta']['prefix'] );
444
		$this->assertEquals( 3, count( $result['meta']['attributes'] ) );
445
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
446
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
447
		$this->assertArrayNotHasKey( 'errors', $result );
448
	}
449
450
451
	/**
452
	 * Returns a test object with a mocked customer controller
453
	 *
454
	 * @param string $method Customer controller method name to mock
455
	 * @param mixed $result Return value of the mocked method
456
	 */
457
	protected function object( $method, $result )
458
	{
459
		$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Customer\Standard::class )
460
			->setConstructorArgs( [$this->context] )
461
			->setMethods( [$method] )
462
			->getMock();
463
464
		$cntl->expects( $this->once() )->method( $method )->will( $result );
465
466
		\Aimeos\Controller\Frontend::inject( 'customer', $cntl );
467
468
		$object = new \Aimeos\Client\JsonApi\Customer\Property\Standard( $this->context, 'customer/property' );
469
		$object->setView( $this->view );
470
471
		return $object;
472
	}
473
}
474