Completed
Push — master ( 21be3d...6dd20d )
by Aimeos
04:36
created

StandardTest::getOrderBaseItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2020
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Basket;
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
		$this->context = \TestHelperJapi::getContext();
22
		$this->view = $this->context->getView();
23
24
		$this->object = new \Aimeos\Client\JsonApi\Basket\Standard( $this->context, 'basket' );
25
		$this->object->setView( $this->view );
26
	}
27
28
29
	protected function tearDown() : void
30
	{
31
		\Aimeos\Controller\Frontend\Basket\Factory::injectController( '\Aimeos\Controller\Frontend\Basket\Standard', null );
32
	}
33
34
35
	public function testDelete()
36
	{
37
		$body = '{"data": {"attributes": {"order.base.comment": "test"}}}';
38
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
39
40
		$response = $this->object->patch( $request, $this->view->response() );
41
		$result = json_decode( (string) $response->getBody(), true );
42
43
		$this->assertEquals( 'test', $result['data']['attributes']['order.base.comment'] );
44
45
46
		$response = $this->object->delete( $request, $this->view->response() );
47
		$result = json_decode( (string) $response->getBody(), true );
48
49
		$this->assertEquals( 200, $response->getStatusCode() );
50
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
51
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
52
53
		$this->assertEquals( 1, $result['meta']['total'] );
54
		$this->assertEquals( 'basket', $result['data']['type'] );
55
		$this->assertGreaterThan( 9, count( $result['data']['attributes'] ) );
56
		$this->assertEquals( '', $result['data']['attributes']['order.base.comment'] );
57
58
		$this->assertArrayNotHasKey( 'errors', $result );
59
	}
60
61
62
	public function testDeletePluginException()
63
	{
64
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );
65
66
		$response = $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
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
77
78
		$response = $object->delete( $this->view->request(), $this->view->response() );
79
		$result = json_decode( (string) $response->getBody(), true );
80
81
		$this->assertEquals( 404, $response->getStatusCode() );
82
		$this->assertArrayHasKey( 'errors', $result );
83
	}
84
85
86
	public function testDeleteException()
87
	{
88
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
89
90
		$response = $object->delete( $this->view->request(), $this->view->response() );
91
		$result = json_decode( (string) $response->getBody(), true );
92
93
94
		$this->assertEquals( 500, $response->getStatusCode() );
95
		$this->assertArrayHasKey( 'errors', $result );
96
	}
97
98
99
	public function testGet()
100
	{
101
		$response = $this->object->get( $this->view->request(), $this->view->response() );
102
		$result = json_decode( (string) $response->getBody(), true );
103
104
		$this->assertEquals( 200, $response->getStatusCode() );
105
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
106
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
107
108
		$this->assertEquals( 1, $result['meta']['total'] );
109
		$this->assertEquals( 'basket', $result['data']['type'] );
110
		$this->assertGreaterThan( 9, count( $result['data']['attributes'] ) );
111
112
		$this->assertArrayNotHasKey( 'errors', $result );
113
	}
114
115
116
	public function testGetById()
117
	{
118
		$user = \Aimeos\MShop::create( $this->context, 'customer' )->findItem( 'UTC001' );
119
		$this->context->setUserId( $user->getId() );
120
121
		$params = ['id' => $this->getOrderBaseItem()->getId()];
122
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
123
		$this->view->addHelper( 'param', $helper );
124
125
		$response = $this->object->get( $this->view->request(), $this->view->response() );
126
		$result = json_decode( (string) $response->getBody(), true );
127
128
		$this->assertEquals( 200, $response->getStatusCode() );
129
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
130
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
131
132
		$this->assertEquals( 1, $result['meta']['total'] );
133
		$this->assertEquals( 'basket', $result['data']['type'] );
134
		$this->assertNotNull( $result['data']['id'] );
135
		$this->assertEquals( 12, count( $result['data']['attributes'] ) );
136
		$this->assertEquals( 'This is another comment.', $result['data']['attributes']['order.base.comment'] );
137
		$this->assertEquals( 8, count( $result['included'] ) );
138
139
		$this->assertArrayNotHasKey( 'errors', $result );
140
	}
141
142
143
	public function testGetNoAccess()
144
	{
145
		$this->context->setUserId( null );
146
147
		$params = array(
148
			'id' => $this->getOrderBaseItem()->getId(),
149
		);
150
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
151
		$this->view->addHelper( 'param', $helper );
152
153
		$response = $this->object->get( $this->view->request(), $this->view->response() );
154
		$result = json_decode( (string) $response->getBody(), true );
155
156
		$this->assertEquals( 200, $response->getStatusCode() );
157
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
158
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
159
160
		$this->assertEquals( 1, $result['meta']['total'] );
161
		$this->assertEquals( 'basket', $result['data']['type'] );
162
		$this->assertEquals( '', $result['data']['attributes']['order.base.customerid'] );
163
164
		$this->assertArrayNotHasKey( 'errors', $result );
165
	}
166
167
168
	public function testGetIncluded()
169
	{
170
		$user = \Aimeos\MShop::create( $this->context, 'customer' )->findItem( 'UTC001' );
171
		$this->context->setUserId( $user->getId() );
172
173
		$params = array(
174
			'id' => $this->getOrderBaseItem()->getId(),
175
			'included' => 'basket/product',
176
		);
177
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
178
		$this->view->addHelper( 'param', $helper );
179
180
		$response = $this->object->get( $this->view->request(), $this->view->response() );
181
		$result = json_decode( (string) $response->getBody(), true );
182
183
184
		$this->assertEquals( 200, $response->getStatusCode() );
185
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
186
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
187
188
		$this->assertEquals( 1, $result['meta']['total'] );
189
		$this->assertEquals( 'basket', $result['data']['type'] );
190
		$this->assertEquals( 1, count( $result['data']['relationships'] ) );
191
		$this->assertArrayHasKey( 'basket/product', $result['data']['relationships'] );
192
		$this->assertEquals( 2, count( $result['data']['relationships']['basket/product']['data'] ) );
193
		$this->assertEquals( 2, count( $result['included'] ) );
194
195
		$this->assertArrayNotHasKey( 'errors', $result );
196
	}
197
198
199
	public function testGetFieldsIncluded()
200
	{
201
		$user = \Aimeos\MShop::create( $this->context, 'customer' )->findItem( 'UTC001' );
202
		$this->context->setUserId( $user->getId() );
203
204
		$params = array(
205
			'id' => $this->getOrderBaseItem()->getId(),
206
			'fields' => array(
207
				'basket' => 'order.base.comment',
208
				'basket/address' => 'order.base.address.firstname,order.base.address.lastname',
209
				'basket/product' => 'order.base.product.name,order.base.product.price',
210
				'basket/service' => 'order.base.service.name,order.base.service.price'
211
			),
212
			'included' => 'basket/address,basket/product,basket/service'
213
		);
214
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
215
		$this->view->addHelper( 'param', $helper );
216
217
		$response = $this->object->get( $this->view->request(), $this->view->response() );
218
		$result = json_decode( (string) $response->getBody(), true );
219
220
		$this->assertEquals( 200, $response->getStatusCode() );
221
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
222
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
223
224
		$this->assertEquals( 1, $result['meta']['total'] );
225
		$this->assertEquals( 'basket', $result['data']['type'] );
226
		$this->assertEquals( 1, count( $result['data']['attributes'] ) );
227
		$this->assertEquals( 6, count( $result['included'] ) );
228
229
		foreach( $result['included'] as $entry ) {
230
			$this->assertCount( 2, $entry['attributes'] );
231
		}
232
233
		$this->assertArrayNotHasKey( 'errors', $result );
234
	}
235
236
237
	public function testGetIncludedNone()
238
	{
239
		$user = \Aimeos\MShop::create( $this->context, 'customer' )->findItem( 'UTC001' );
240
		$this->context->setUserId( $user->getId() );
241
242
		$params = array(
243
			'id' => $this->getOrderBaseItem()->getId(),
244
			'included' => '',
245
		);
246
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
247
		$this->view->addHelper( 'param', $helper );
248
249
		$response = $this->object->get( $this->view->request(), $this->view->response() );
250
		$result = json_decode( (string) $response->getBody(), true );
251
252
		$this->assertEquals( 200, $response->getStatusCode() );
253
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
254
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
255
256
		$this->assertEquals( 1, $result['meta']['total'] );
257
		$this->assertEquals( 'basket', $result['data']['type'] );
258
		$this->assertEquals( 0, count( $result['data']['relationships'] ) );
259
		$this->assertEquals( 0, count( $result['included'] ) );
260
261
		$this->assertArrayNotHasKey( 'errors', $result );
262
	}
263
264
265
	public function testGetMShopException()
266
	{
267
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
268
269
		$response = $object->get( $this->view->request(), $this->view->response() );
270
		$result = json_decode( (string) $response->getBody(), true );
271
272
273
		$this->assertEquals( 404, $response->getStatusCode() );
274
		$this->assertArrayHasKey( 'errors', $result );
275
	}
276
277
278
	public function testGetException()
279
	{
280
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
281
282
		$response = $object->get( $this->view->request(), $this->view->response() );
283
		$result = json_decode( (string) $response->getBody(), true );
284
285
286
		$this->assertEquals( 500, $response->getStatusCode() );
287
		$this->assertArrayHasKey( 'errors', $result );
288
	}
289
290
291
	public function testPatch()
292
	{
293
		$body = '{"data": {"attributes": {"order.base.comment": "test", "order.base.customerref": "abc"}}}	';
294
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
295
296
		$response = $this->object->patch( $request, $this->view->response() );
297
		$result = json_decode( (string) $response->getBody(), true );
298
299
300
		$this->assertEquals( 200, $response->getStatusCode() );
301
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
302
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
303
304
		$this->assertEquals( 1, $result['meta']['total'] );
305
		$this->assertEquals( 'basket', $result['data']['type'] );
306
		$this->assertGreaterThan( 10, count( $result['data']['attributes'] ) );
307
		$this->assertEquals( 'test', $result['data']['attributes']['order.base.comment'] );
308
		$this->assertEquals( 'abc', $result['data']['attributes']['order.base.customerref'] );
309
310
		$this->assertArrayNotHasKey( 'errors', $result );
311
	}
312
313
314
	public function testPatchPluginException()
315
	{
316
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );
317
318
		$body = '{"data": {"attributes": []}}';
319
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
320
321
		$response = $object->patch( $request, $this->view->response() );
322
		$result = json_decode( (string) $response->getBody(), true );
323
324
325
		$this->assertEquals( 409, $response->getStatusCode() );
326
		$this->assertArrayHasKey( 'errors', $result );
327
	}
328
329
330
	public function testPatchMShopException()
331
	{
332
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
333
334
		$body = '{"data": {"attributes": []}}';
335
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
336
337
		$response = $object->patch( $request, $this->view->response() );
338
		$result = json_decode( (string) $response->getBody(), true );
339
340
341
		$this->assertEquals( 404, $response->getStatusCode() );
342
		$this->assertArrayHasKey( 'errors', $result );
343
	}
344
345
346
	public function testPatchException()
347
	{
348
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
349
350
		$body = '{"data": {"attributes": []}}';
351
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
352
353
		$response = $object->patch( $request, $this->view->response() );
354
		$result = json_decode( (string) $response->getBody(), true );
355
356
357
		$this->assertEquals( 500, $response->getStatusCode() );
358
		$this->assertArrayHasKey( 'errors', $result );
359
	}
360
361
362
	public function testPost()
363
	{
364
		$price = \Aimeos\MShop::create( $this->context, 'price' )->createItem();
365
		$locale = \Aimeos\MShop::create( $this->context, 'locale' )->createItem();
366
367
		$basket = $this->getMockBuilder( \Aimeos\MShop\Order\Item\Base\Standard::class )
368
			->setConstructorArgs( [$price, $locale] )
369
			->setMethods( ['check'] )
370
			->getMock();
371
372
		$basket->expects( $this->once() )->method( 'check' )->will( $this->returnSelf() );
373
374
		$object = $this->getObject( ['get', 'store'], $this->returnValue( $basket ) );
0 ignored issues
show
Bug introduced by
array('get', 'store') of type array<integer,string> is incompatible with the type string expected by parameter $method of Aimeos\Client\JsonApi\Ba...andardTest::getObject(). ( Ignorable by Annotation )

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

374
		$object = $this->getObject( /** @scrutinizer ignore-type */ ['get', 'store'], $this->returnValue( $basket ) );
Loading history...
375
376
377
		$body = '{"data": {"attributes": {"order.base.comment": "test"}}}';
378
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
379
380
		$response = $object->post( $request, $this->view->response() );
381
		$result = json_decode( (string) $response->getBody(), true );
382
383
		$this->assertEquals( 200, $response->getStatusCode() );
384
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
385
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
386
387
		$this->assertEquals( 1, $result['meta']['total'] );
388
		$this->assertNotNull( $result['data']['id'] );
389
		$this->assertEquals( 'basket', $result['data']['type'] );
390
		$this->assertGreaterThan( 9, count( $result['data']['attributes'] ) );
391
392
		$this->assertArrayNotHasKey( 'errors', $result );
393
	}
394
395
396
	public function testPostPluginException()
397
	{
398
		$object = $this->getObject( 'get', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );
399
400
		$response = $object->post( $this->view->request(), $this->view->response() );
401
		$result = json_decode( (string) $response->getBody(), true );
402
403
404
		$this->assertEquals( 409, $response->getStatusCode() );
405
		$this->assertArrayHasKey( 'errors', $result );
406
	}
407
408
409
	public function testPostMShopException()
410
	{
411
		$object = $this->getObject( 'get', $this->throwException( new \Aimeos\MShop\Exception() ) );
412
413
		$response = $object->post( $this->view->request(), $this->view->response() );
414
		$result = json_decode( (string) $response->getBody(), true );
415
416
417
		$this->assertEquals( 404, $response->getStatusCode() );
418
		$this->assertArrayHasKey( 'errors', $result );
419
	}
420
421
422
	public function testPostException()
423
	{
424
		$object = $this->getObject( 'get', $this->throwException( new \Exception() ) );
425
426
		$response = $object->post( $this->view->request(), $this->view->response() );
427
		$result = json_decode( (string) $response->getBody(), true );
428
429
430
		$this->assertEquals( 500, $response->getStatusCode() );
431
		$this->assertArrayHasKey( 'errors', $result );
432
	}
433
434
435
	public function testOptions()
436
	{
437
		$response = $this->object->options( $this->view->request(), $this->view->response() );
438
		$result = json_decode( (string) $response->getBody(), true );
439
440
		$this->assertEquals( 200, $response->getStatusCode() );
441
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
442
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
443
444
		$this->assertEquals( null, $result['meta']['prefix'] );
445
		$this->assertEquals( 2, count( $result['meta']['attributes'] ) );
446
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
447
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
448
		$this->assertArrayNotHasKey( 'errors', $result );
449
	}
450
451
452
	/**
453
	 * Returns a stored basket
454
	 *
455
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Basket object
456
	 */
457
	protected function getOrderBaseItem()
458
	{
459
		$manager = \Aimeos\MShop::create( $this->context, 'order/base' );
460
461
		$search = $manager->createSearch();
462
		$search->setConditions( $search->compare( '==', 'order.base.price', '672.00' ) );
463
464
		if( ( $item = $manager->searchItems( $search, ['order/base/product'] )->first() ) === null ) {
465
			throw new \Exception( 'No order/base item with price "672.00" found' );
466
		}
467
468
		return $item;
469
	}
470
471
472
	/**
473
	 * Returns a test object with a mocked basket controller
474
	 *
475
	 * @param string $method Basket controller method name to mock
476
	 * @param mixed $result Return value of the mocked method
477
	 */
478
	protected function getObject( $method, $result )
479
	{
480
		$methods = (array) $method;
481
482
		$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
483
			->setConstructorArgs( [$this->context] )
484
			->setMethods( $methods )
485
			->getMock();
486
487
		foreach( $methods as $method ) {
0 ignored issues
show
introduced by
$method is overwriting one of the parameters of this function.
Loading history...
488
			$cntl->expects( $this->once() )->method( $method )->will( $result );
489
		}
490
491
		\Aimeos\Controller\Frontend\Basket\Factory::injectController( '\Aimeos\Controller\Frontend\Basket\Standard', $cntl );
492
493
		$object = new \Aimeos\Client\JsonApi\Basket\Standard( $this->context, 'basket' );
494
		$object->setView( $this->view );
495
496
		return $object;
497
	}
498
}
499