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

StandardTest::testGetException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2021
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Customer\Address;
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\Address\Standard( $this->context, 'customer/address' );
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/address'] )->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/address", "id": ' . $customer->getAddressItems()->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/address'] )->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->getAddressItems()->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/address', $result['data'][0]['type'] );
152
		$this->assertNotNull( $result['data'][0]['id'] );
153
		$this->assertGreaterThan( 21, 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/address'] );
162
		$id = $customer->getAddressItems()->first()->getId();
163
		$this->context->setUserId( $customer->getId() );
164
165
		$params = array(
166
			'id' => $customer->getId(),
167
			'related' => 'address',
168
			'relatedid' => $id,
169
			'fields' => ['customer/address' => 'customer.address.id,customer.address.firstname'],
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/address', $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/address'] )->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->getAddressItems()->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.address.lastname": "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/address', $result['data']['type'] );
255
		$this->assertGreaterThan( 21, count( $result['data']['attributes'] ) );
256
		$this->assertEquals( 'test', $result['data']['attributes']['customer.address.lastname'] );
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/address", "attributes": {"customer.address.firstname": "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/address', $result['data'][0]['type'] );
338
		$this->assertEquals( 'test', $result['data'][0]['attributes']['customer.address.firstname'] );
339
340
		$this->assertArrayNotHasKey( 'errors', $result );
341
	}
342
343
344
	public function testPostMultiple()
345
	{
346
		$custManager = \Aimeos\MShop::create( $this->context, 'customer' );
347
		$customer = $custManager->create()->setCode( 'unittest-jsonapi' );
348
		$customer = $custManager->save( $customer );
349
		$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

349
		$this->context->setUserId( /** @scrutinizer ignore-type */ $customer->getId() );
Loading history...
350
351
352
		$params = ['id' => $customer->getId()];
353
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
354
		$this->view->addHelper( 'param', $helper );
355
356
		$body = '{"data": [
357
			{"type": "customer/address", "attributes": {"customer.address.firstname": "test"}},
358
			{"type": "customer/address", "attributes": {"customer.address.lastname": "test"}}
359
		]}';
360
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
361
362
363
		$response = $this->object->post( $request, $this->view->response() );
364
		$result = json_decode( (string) $response->getBody(), true );
365
366
		$custManager->delete( $customer->getId() );
367
368
369
		$this->assertEquals( 201, $response->getStatusCode() );
370
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
371
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
372
373
		$this->assertEquals( 2, $result['meta']['total'] );
374
		$this->assertNotNull( $result['data'][1]['id'] );
375
		$this->assertEquals( 'customer/address', $result['data'][1]['type'] );
376
		$this->assertEquals( 'test', $result['data'][0]['attributes']['customer.address.firstname'] );
377
		$this->assertEquals( 'test', $result['data'][1]['attributes']['customer.address.lastname'] );
378
379
		$this->assertArrayNotHasKey( 'errors', $result );
380
	}
381
382
383
	public function testPostControllerException()
384
	{
385
		$object = $this->object( 'uses', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
386
387
		$body = '{"data": {"attributes": []}}';
388
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
389
390
		$response = $object->post( $request, $this->view->response() );
391
		$result = json_decode( (string) $response->getBody(), true );
392
393
394
		$this->assertEquals( 403, $response->getStatusCode() );
395
		$this->assertArrayHasKey( 'errors', $result );
396
	}
397
398
399
	public function testPostMShopException()
400
	{
401
		$object = $this->object( 'uses', $this->throwException( new \Aimeos\MShop\Exception() ) );
402
403
		$body = '{"data": {"attributes": []}}';
404
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
405
406
		$response = $object->post( $request, $this->view->response() );
407
		$result = json_decode( (string) $response->getBody(), true );
408
409
410
		$this->assertEquals( 404, $response->getStatusCode() );
411
		$this->assertArrayHasKey( 'errors', $result );
412
	}
413
414
415
	public function testPostException()
416
	{
417
		$object = $this->object( 'uses', $this->throwException( new \Exception() ) );
418
419
		$body = '{"data": {"attributes": []}}';
420
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
421
422
		$response = $object->post( $request, $this->view->response() );
423
		$result = json_decode( (string) $response->getBody(), true );
424
425
426
		$this->assertEquals( 500, $response->getStatusCode() );
427
		$this->assertArrayHasKey( 'errors', $result );
428
	}
429
430
431
	public function testOptions()
432
	{
433
		$response = $this->object->options( $this->view->request(), $this->view->response() );
434
		$result = json_decode( (string) $response->getBody(), true );
435
436
		$this->assertEquals( 200, $response->getStatusCode() );
437
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
438
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
439
440
		$this->assertEquals( null, $result['meta']['prefix'] );
441
		$this->assertEquals( 21, count( $result['meta']['attributes'] ) );
442
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
443
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
444
		$this->assertArrayNotHasKey( 'errors', $result );
445
	}
446
447
448
	/**
449
	 * Returns a test object with a mocked customer controller
450
	 *
451
	 * @param string $method Customer controller method name to mock
452
	 * @param mixed $result Return value of the mocked method
453
	 */
454
	protected function object( $method, $result )
455
	{
456
		$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Customer\Standard::class )
457
			->setConstructorArgs( [$this->context] )
458
			->setMethods( [$method] )
459
			->getMock();
460
461
		$cntl->expects( $this->once() )->method( $method )->will( $result );
462
463
		\Aimeos\Controller\Frontend::inject( 'customer', $cntl );
464
465
		$object = new \Aimeos\Client\JsonApi\Customer\Address\Standard( $this->context, 'customer/address' );
466
		$object->setView( $this->view );
467
468
		return $object;
469
	}
470
}
471