Passed
Push — master ( f17e4f...350b1a )
by Aimeos
16:03
created

StandardTest::testPost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 0
dl 0
loc 33
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2024
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Basket;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $view;
16
17
18
	protected function setUp() : void
19
	{
20
		\Aimeos\Controller\Frontend::cache( true );
21
22
		$this->context = \TestHelper::context();
23
		$this->view = $this->context->view();
24
	}
25
26
27
	protected function tearDown() : void
28
	{
29
		\Aimeos\Controller\Frontend::cache( false );
30
		unset( $this->view, $this->context );
31
	}
32
33
34
	public function testDelete()
35
	{
36
		$body = '{"data": {"attributes": {"order.comment": "test"}}}';
37
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
38
39
		$response = $this->object()->patch( $request, $this->view->response() );
40
		$result = json_decode( (string) $response->getBody(), true );
41
42
		$this->assertEquals( 'test', $result['data']['attributes']['order.comment'] );
43
44
45
		$response = $this->object()->delete( $request, $this->view->response() );
46
		$result = json_decode( (string) $response->getBody(), true );
47
48
		$this->assertEquals( 200, $response->getStatusCode() );
49
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
50
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
51
52
		$this->assertEquals( 1, $result['meta']['total'] );
53
		$this->assertEquals( 'basket', $result['data']['type'] );
54
		$this->assertGreaterThan( 9, count( $result['data']['attributes'] ) );
55
		$this->assertEquals( '', $result['data']['attributes']['order.comment'] );
56
57
		$this->assertArrayNotHasKey( 'errors', $result );
58
	}
59
60
61
	public function testDeletePluginException()
62
	{
63
		$this->controller( 'setType' )->expects( $this->once() )->method( 'setType' )
64
			->will( $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );
65
66
		$response = $this->object()->delete( $this->view->request(), $this->view->response() );
67
		$result = json_decode( (string) $response->getBody(), true );
68
69
		$this->assertEquals( 409, $response->getStatusCode() );
70
		$this->assertArrayHasKey( 'errors', $result );
71
	}
72
73
74
	public function testDeleteMShopException()
75
	{
76
		$this->controller( 'setType' )->expects( $this->once() )->method( 'setType' )
77
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
78
79
		$response = $this->object()->delete( $this->view->request(), $this->view->response() );
80
		$result = json_decode( (string) $response->getBody(), true );
81
82
		$this->assertEquals( 404, $response->getStatusCode() );
83
		$this->assertArrayHasKey( 'errors', $result );
84
	}
85
86
87
	public function testDeleteException()
88
	{
89
		$this->controller( 'setType' )->expects( $this->once() )->method( 'setType' )
90
			->will( $this->throwException( new \Exception() ) );
91
92
		$response = $this->object()->delete( $this->view->request(), $this->view->response() );
93
		$result = json_decode( (string) $response->getBody(), true );
94
95
96
		$this->assertEquals( 500, $response->getStatusCode() );
97
		$this->assertArrayHasKey( 'errors', $result );
98
	}
99
100
101
	public function testGet()
102
	{
103
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
104
		$result = json_decode( (string) $response->getBody(), true );
105
106
		$this->assertEquals( 200, $response->getStatusCode() );
107
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
108
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
109
110
		$this->assertEquals( 1, $result['meta']['total'] );
111
		$this->assertEquals( 'basket', $result['data']['type'] );
112
		$this->assertGreaterThan( 9, count( $result['data']['attributes'] ) );
113
114
		$this->assertArrayNotHasKey( 'errors', $result );
115
	}
116
117
118
	public function testGetById()
119
	{
120
		$user = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' );
121
		$this->context->setUser( $user );
122
123
		$params = ['id' => $this->getOrderItem()->getId()];
124
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
125
		$this->view->addHelper( 'param', $helper );
126
127
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
128
		$result = json_decode( (string) $response->getBody(), true );
129
130
		$this->assertEquals( 200, $response->getStatusCode() );
131
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
132
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
133
134
		$this->assertEquals( 1, $result['meta']['total'] );
135
		$this->assertEquals( 'basket', $result['data']['type'] );
136
		$this->assertNotNull( $result['data']['id'] );
137
		$this->assertEquals( 19, count( $result['data']['attributes'] ) );
138
		$this->assertEquals( 'This is another comment.', $result['data']['attributes']['order.comment'] );
139
		$this->assertEquals( 8, count( $result['included'] ) );
140
141
		$this->assertArrayNotHasKey( 'errors', $result );
142
	}
143
144
145
	public function testGetNoAccess()
146
	{
147
		$this->context->setUser( null );
148
149
		$params = array(
150
			'id' => $this->getOrderItem()->getId(),
151
		);
152
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
153
		$this->view->addHelper( 'param', $helper );
154
155
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
156
		$result = json_decode( (string) $response->getBody(), true );
157
158
		$this->assertEquals( 200, $response->getStatusCode() );
159
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
160
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
161
162
		$this->assertEquals( 1, $result['meta']['total'] );
163
		$this->assertEquals( 'basket', $result['data']['type'] );
164
		$this->assertEquals( '', $result['data']['attributes']['order.customerid'] );
165
166
		$this->assertArrayNotHasKey( 'errors', $result );
167
	}
168
169
170
	public function testGetIncluded()
171
	{
172
		$user = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' );
173
		$this->context->setUser( $user );
174
175
		$params = array(
176
			'id' => $this->getOrderItem()->getId(),
177
			'include' => 'basket.product,customer',
178
		);
179
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
180
		$this->view->addHelper( 'param', $helper );
181
182
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
183
		$result = json_decode( (string) $response->getBody(), true );
184
185
186
		$this->assertEquals( 200, $response->getStatusCode() );
187
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
188
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
189
190
		$this->assertEquals( 1, $result['meta']['total'] );
191
		$this->assertEquals( 'basket', $result['data']['type'] );
192
		$this->assertEquals( 2, count( $result['data']['relationships'] ) );
193
		$this->assertArrayHasKey( 'customer', $result['data']['relationships'] );
194
		$this->assertArrayHasKey( 'basket.product', $result['data']['relationships'] );
195
		$this->assertEquals( 2, count( $result['data']['relationships']['basket.product']['data'] ) );
196
		$this->assertEquals( 1, count( $result['data']['relationships']['customer']['data'] ) );
197
		$this->assertEquals( 3, count( $result['included'] ) );
198
199
		$this->assertArrayNotHasKey( 'errors', $result );
200
	}
201
202
203
	public function testGetFieldsIncluded()
204
	{
205
		$user = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' );
206
		$this->context->setUser( $user );
207
208
		$params = array(
209
			'id' => $this->getOrderItem()->getId(),
210
			'fields' => array(
211
				'basket' => 'order.comment',
212
				'basket.address' => 'order.address.firstname,order.address.lastname',
213
				'basket.product' => 'order.product.name,order.product.price',
214
				'basket.service' => 'order.service.name,order.service.price',
215
				'customer' => 'customer.id,customer.email'
216
			),
217
			'include' => 'basket.address,basket.product,basket.service,customer'
218
		);
219
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
220
		$this->view->addHelper( 'param', $helper );
221
222
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
223
		$result = json_decode( (string) $response->getBody(), true );
224
225
		$this->assertEquals( 200, $response->getStatusCode() );
226
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
227
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
228
229
		$this->assertEquals( 1, $result['meta']['total'] );
230
		$this->assertEquals( 'basket', $result['data']['type'] );
231
		$this->assertEquals( 1, count( $result['data']['attributes'] ) );
232
		$this->assertEquals( 7, count( $result['included'] ) );
233
234
		foreach( $result['included'] as $entry ) {
235
			$this->assertCount( 2, $entry['attributes'] );
236
		}
237
238
		$this->assertArrayNotHasKey( 'errors', $result );
239
	}
240
241
242
	public function testGetIncludedNone()
243
	{
244
		$user = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' );
245
		$this->context->setUser( $user );
246
247
		$params = array(
248
			'id' => $this->getOrderItem()->getId(),
249
			'include' => '',
250
		);
251
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
252
		$this->view->addHelper( 'param', $helper );
253
254
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
255
		$result = json_decode( (string) $response->getBody(), true );
256
257
		$this->assertEquals( 200, $response->getStatusCode() );
258
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
259
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
260
261
		$this->assertEquals( 1, $result['meta']['total'] );
262
		$this->assertEquals( 'basket', $result['data']['type'] );
263
		$this->assertEquals( 0, count( $result['data']['relationships'] ) );
264
		$this->assertEquals( 0, count( $result['included'] ) );
265
266
		$this->assertArrayNotHasKey( 'errors', $result );
267
	}
268
269
270
	public function testGetMShopException()
271
	{
272
		$this->controller( 'setType' )->expects( $this->once() )->method( 'setType' )
273
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
274
275
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
276
		$result = json_decode( (string) $response->getBody(), true );
277
278
279
		$this->assertEquals( 404, $response->getStatusCode() );
280
		$this->assertArrayHasKey( 'errors', $result );
281
	}
282
283
284
	public function testGetException()
285
	{
286
		$this->controller( 'setType' )->expects( $this->once() )->method( 'setType' )
287
			->will( $this->throwException( new \Exception() ) );
288
289
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
290
		$result = json_decode( (string) $response->getBody(), true );
291
292
293
		$this->assertEquals( 500, $response->getStatusCode() );
294
		$this->assertArrayHasKey( 'errors', $result );
295
	}
296
297
298
	public function testPatch()
299
	{
300
		$body = '{"data": {"attributes": {"order.comment": "test", "order.customerref": "abc"}}}	';
301
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
302
303
		$response = $this->object()->patch( $request, $this->view->response() );
304
		$result = json_decode( (string) $response->getBody(), true );
305
306
307
		$this->assertEquals( 200, $response->getStatusCode() );
308
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
309
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
310
311
		$this->assertEquals( 1, $result['meta']['total'] );
312
		$this->assertEquals( 'basket', $result['data']['type'] );
313
		$this->assertGreaterThan( 10, count( $result['data']['attributes'] ) );
314
		$this->assertEquals( 'test', $result['data']['attributes']['order.comment'] );
315
		$this->assertEquals( 'abc', $result['data']['attributes']['order.customerref'] );
316
317
		$this->assertArrayNotHasKey( 'errors', $result );
318
	}
319
320
321
	public function testPatchPluginException()
322
	{
323
		$this->controller( 'setType' )->expects( $this->once() )->method( 'setType' )
324
			->will( $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );
325
326
		$body = '{"data": {"attributes": []}}';
327
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
328
329
		$response = $this->object()->patch( $request, $this->view->response() );
330
		$result = json_decode( (string) $response->getBody(), true );
331
332
333
		$this->assertEquals( 409, $response->getStatusCode() );
334
		$this->assertArrayHasKey( 'errors', $result );
335
	}
336
337
338
	public function testPatchMShopException()
339
	{
340
		$this->controller( 'setType' )->expects( $this->once() )->method( 'setType' )
341
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
342
343
		$body = '{"data": {"attributes": []}}';
344
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
345
346
		$response = $this->object()->patch( $request, $this->view->response() );
347
		$result = json_decode( (string) $response->getBody(), true );
348
349
350
		$this->assertEquals( 404, $response->getStatusCode() );
351
		$this->assertArrayHasKey( 'errors', $result );
352
	}
353
354
355
	public function testPatchException()
356
	{
357
		$this->controller( 'setType' )->expects( $this->once() )->method( 'setType' )
358
			->will( $this->throwException( new \Exception() ) );
359
360
		$body = '{"data": {"attributes": []}}';
361
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
362
363
		$response = $this->object()->patch( $request, $this->view->response() );
364
		$result = json_decode( (string) $response->getBody(), true );
365
366
367
		$this->assertEquals( 500, $response->getStatusCode() );
368
		$this->assertArrayHasKey( 'errors', $result );
369
	}
370
371
372
	public function testPost()
373
	{
374
		$price = \Aimeos\MShop::create( $this->context, 'price' )->create();
375
		$locale = \Aimeos\MShop::create( $this->context, 'locale' )->create();
376
377
		$basket = $this->getMockBuilder( \Aimeos\MShop\Order\Item\Standard::class )
378
			->setConstructorArgs( [$price, $locale] )
379
			->onlyMethods( ['check'] )
380
			->getMock();
381
382
		$basket->expects( $this->once() )->method( 'check' )->willReturnSelf();
383
384
		$cntl = $this->controller( ['get', 'store'] );
385
		$cntl->expects( $this->once() )->method( 'get' )->willReturn( $basket );
386
		$cntl->expects( $this->once() )->method( 'store' )->willReturn( $basket );
387
388
389
		$body = '{"data": {"attributes": {"order.comment": "test"}}}';
390
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
391
392
		$response = $this->object()->post( $request, $this->view->response() );
393
		$result = json_decode( (string) $response->getBody(), true );
394
395
		$this->assertEquals( 200, $response->getStatusCode() );
396
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
397
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
398
399
		$this->assertEquals( 1, $result['meta']['total'] );
400
		$this->assertNotNull( $result['data']['id'] );
401
		$this->assertEquals( 'basket', $result['data']['type'] );
402
		$this->assertGreaterThan( 9, count( $result['data']['attributes'] ) );
403
404
		$this->assertArrayNotHasKey( 'errors', $result );
405
	}
406
407
408
	public function testPostPluginException()
409
	{
410
		$this->controller( 'get' )->expects( $this->once() )->method( 'get' )
411
			->will( $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );
412
413
		$response = $this->object()->post( $this->view->request(), $this->view->response() );
414
		$result = json_decode( (string) $response->getBody(), true );
415
416
417
		$this->assertEquals( 409, $response->getStatusCode() );
418
		$this->assertArrayHasKey( 'errors', $result );
419
	}
420
421
422
	public function testPostMShopException()
423
	{
424
		$this->controller( 'get' )->expects( $this->once() )->method( 'get' )
425
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
426
427
		$response = $this->object()->post( $this->view->request(), $this->view->response() );
428
		$result = json_decode( (string) $response->getBody(), true );
429
430
431
		$this->assertEquals( 404, $response->getStatusCode() );
432
		$this->assertArrayHasKey( 'errors', $result );
433
	}
434
435
436
	public function testPostException()
437
	{
438
		$this->controller( 'get' )->expects( $this->once() )->method( 'get' )
439
			->will( $this->throwException( new \Exception() ) );
440
441
		$response = $this->object()->post( $this->view->request(), $this->view->response() );
442
		$result = json_decode( (string) $response->getBody(), true );
443
444
445
		$this->assertEquals( 500, $response->getStatusCode() );
446
		$this->assertArrayHasKey( 'errors', $result );
447
	}
448
449
450
	public function testOptions()
451
	{
452
		$response = $this->object()->options( $this->view->request(), $this->view->response() );
453
		$result = json_decode( (string) $response->getBody(), true );
454
455
		$this->assertEquals( 200, $response->getStatusCode() );
456
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
457
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
458
459
		$this->assertEquals( null, $result['meta']['prefix'] );
460
		$this->assertEquals( 2, count( $result['meta']['attributes'] ) );
461
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
462
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
463
		$this->assertArrayNotHasKey( 'errors', $result );
464
	}
465
466
467
	/**
468
	 * Returns a stored order
469
	 *
470
	 * @return \Aimeos\MShop\Order\Item\Iface Order object
471
	 */
472
	protected function getOrderItem()
473
	{
474
		$manager = \Aimeos\MShop::create( $this->context, 'order' );
475
476
		$search = $manager->filter();
477
		$search->setConditions( $search->compare( '==', 'order.price', '672.00' ) );
478
479
		if( ( $item = $manager->search( $search, ['order/product'] )->first() ) === null ) {
480
			throw new \Exception( 'No order item with price "672.00" found' );
481
		}
482
483
		return $item;
484
	}
485
486
487
	/**
488
	 * Returns a mocked basket controller
489
	 *
490
	 * @param array|string $methods Basket controller method name to mock
491
	 * @return Mocked basket controller
0 ignored issues
show
Bug introduced by
The type Aimeos\Client\JsonApi\Basket\Mocked was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
492
	 */
493
	protected function controller( $methods )
494
	{
495
		$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
496
			->setConstructorArgs( [$this->context] )
497
			->onlyMethods( (array) $methods )
498
			->getMock();
499
500
		\Aimeos\Controller\Frontend::inject( \Aimeos\Controller\Frontend\Basket\Standard::class, $cntl );
501
502
		return $cntl;
503
	}
504
505
506
	/**
507
	 * Returns the JSON API client object
508
	 *
509
	 * @return \Aimeos\Client\JsonApi\Basket\Standard JSON API client object
510
	 */
511
	protected function object()
512
	{
513
		$object = new \Aimeos\Client\JsonApi\Basket\Standard( $this->context );
514
		$object->setView( $this->view );
515
516
		return $object;
517
	}
518
}
519