Passed
Push — master ( a6b03a...955296 )
by Aimeos
02:30
created

StandardTest::testPostPluginException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
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-2018
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Basket;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase 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...
13
{
14
	private $context;
15
	private $object;
16
	private $view;
17
18
19
	protected function setUp()
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()
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 = array(
122
			'id' => $this->getOrderBaseItem()->getId(),
123
			'fields' => array(
124
				'basket' => 'order.base.comment'
125
			),
126
		);
127
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
128
		$this->view->addHelper( 'param', $helper );
129
130
		$response = $this->object->get( $this->view->request(), $this->view->response() );
131
		$result = json_decode( (string) $response->getBody(), true );
132
133
		$this->assertEquals( 200, $response->getStatusCode() );
134
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
135
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
136
137
		$this->assertEquals( 1, $result['meta']['total'] );
138
		$this->assertEquals( 'basket', $result['data']['type'] );
139
		$this->assertNotNull( $result['data']['id'] );
140
		$this->assertEquals( 1, count( $result['data']['attributes'] ) );
141
		$this->assertEquals( 'This is another comment.', $result['data']['attributes']['order.base.comment'] );
142
		$this->assertEquals( 8, count( $result['included'] ) );
143
144
		$this->assertArrayNotHasKey( 'errors', $result );
145
	}
146
147
148
	public function testGetNoAccess()
149
	{
150
		$this->context->setUserId( null );
151
152
		$params = array(
153
			'id' => $this->getOrderBaseItem()->getId(),
154
		);
155
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
156
		$this->view->addHelper( 'param', $helper );
157
158
		$response = $this->object->get( $this->view->request(), $this->view->response() );
159
		$result = json_decode( (string) $response->getBody(), true );
160
161
		$this->assertEquals( 200, $response->getStatusCode() );
162
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
163
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
164
165
		$this->assertEquals( 1, $result['meta']['total'] );
166
		$this->assertEquals( 'basket', $result['data']['type'] );
167
		$this->assertEquals( '', $result['data']['attributes']['order.base.customerid'] );
168
169
		$this->assertArrayNotHasKey( 'errors', $result );
170
	}
171
172
173
	public function testGetIncluded()
174
	{
175
		$user = \Aimeos\MShop::create( $this->context, 'customer' )->findItem( 'UTC001' );
176
		$this->context->setUserId( $user->getId() );
177
178
		$params = array(
179
			'id' => $this->getOrderBaseItem()->getId(),
180
			'included' => 'basket/product',
181
		);
182
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
183
		$this->view->addHelper( 'param', $helper );
184
185
		$response = $this->object->get( $this->view->request(), $this->view->response() );
186
		$result = json_decode( (string) $response->getBody(), true );
187
188
189
		$this->assertEquals( 200, $response->getStatusCode() );
190
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
191
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
192
193
		$this->assertEquals( 1, $result['meta']['total'] );
194
		$this->assertEquals( 'basket', $result['data']['type'] );
195
		$this->assertEquals( 1, count( $result['data']['relationships'] ) );
196
		$this->assertArrayHasKey( 'basket/product', $result['data']['relationships'] );
197
		$this->assertEquals( 2, count( $result['data']['relationships']['basket/product']['data'] ) );
198
		$this->assertEquals( 2, count( $result['included'] ) );
199
200
		$this->assertArrayNotHasKey( 'errors', $result );
201
	}
202
203
204
	public function testGetIncludedNone()
205
	{
206
		$user = \Aimeos\MShop::create( $this->context, 'customer' )->findItem( 'UTC001' );
207
		$this->context->setUserId( $user->getId() );
208
209
		$params = array(
210
			'id' => $this->getOrderBaseItem()->getId(),
211
			'included' => '',
212
		);
213
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
214
		$this->view->addHelper( 'param', $helper );
215
216
		$response = $this->object->get( $this->view->request(), $this->view->response() );
217
		$result = json_decode( (string) $response->getBody(), true );
218
219
		$this->assertEquals( 200, $response->getStatusCode() );
220
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
221
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
222
223
		$this->assertEquals( 1, $result['meta']['total'] );
224
		$this->assertEquals( 'basket', $result['data']['type'] );
225
		$this->assertEquals( 0, count( $result['data']['relationships'] ) );
226
		$this->assertEquals( 0, count( $result['included'] ) );
227
228
		$this->assertArrayNotHasKey( 'errors', $result );
229
	}
230
231
232
	public function testGetMShopException()
233
	{
234
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
235
236
		$response = $object->get( $this->view->request(), $this->view->response() );
237
		$result = json_decode( (string) $response->getBody(), true );
238
239
240
		$this->assertEquals( 404, $response->getStatusCode() );
241
		$this->assertArrayHasKey( 'errors', $result );
242
	}
243
244
245
	public function testGetException()
246
	{
247
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
248
249
		$response = $object->get( $this->view->request(), $this->view->response() );
250
		$result = json_decode( (string) $response->getBody(), true );
251
252
253
		$this->assertEquals( 500, $response->getStatusCode() );
254
		$this->assertArrayHasKey( 'errors', $result );
255
	}
256
257
258
	public function testPatch()
259
	{
260
		$body = '{"data": {"attributes": {"order.base.comment": "test"}}}	';
261
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
262
263
		$response = $this->object->patch( $request, $this->view->response() );
264
		$result = json_decode( (string) $response->getBody(), true );
265
266
267
		$this->assertEquals( 200, $response->getStatusCode() );
268
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
269
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
270
271
		$this->assertEquals( 1, $result['meta']['total'] );
272
		$this->assertEquals( 'basket', $result['data']['type'] );
273
		$this->assertGreaterThan( 9, count( $result['data']['attributes'] ) );
274
		$this->assertEquals( 'test', $result['data']['attributes']['order.base.comment'] );
275
276
		$this->assertArrayNotHasKey( 'errors', $result );
277
	}
278
279
280
	public function testPatchPluginException()
281
	{
282
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );
283
284
		$body = '{"data": {"attributes": []}}';
285
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
286
287
		$response = $object->patch( $request, $this->view->response() );
288
		$result = json_decode( (string) $response->getBody(), true );
289
290
291
		$this->assertEquals( 409, $response->getStatusCode() );
292
		$this->assertArrayHasKey( 'errors', $result );
293
	}
294
295
296
	public function testPatchMShopException()
297
	{
298
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
299
300
		$body = '{"data": {"attributes": []}}';
301
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
302
303
		$response = $object->patch( $request, $this->view->response() );
304
		$result = json_decode( (string) $response->getBody(), true );
305
306
307
		$this->assertEquals( 404, $response->getStatusCode() );
308
		$this->assertArrayHasKey( 'errors', $result );
309
	}
310
311
312
	public function testPatchException()
313
	{
314
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
315
316
		$body = '{"data": {"attributes": []}}';
317
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
318
319
		$response = $object->patch( $request, $this->view->response() );
320
		$result = json_decode( (string) $response->getBody(), true );
321
322
323
		$this->assertEquals( 500, $response->getStatusCode() );
324
		$this->assertArrayHasKey( 'errors', $result );
325
	}
326
327
328
	public function testPost()
329
	{
330
		$orderBaseItem = $this->getOrderBaseItem();
331
		$object = $this->getObject( 'store', $this->returnValue( $orderBaseItem ) );
332
333
		$body = '{"data": {"attributes": {"order.base.comment": "test"}}}';
334
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
335
336
		$response = $object->post( $request, $this->view->response() );
337
		$result = json_decode( (string) $response->getBody(), true );
338
339
		$this->assertEquals( 200, $response->getStatusCode() );
340
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
341
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
342
343
		$this->assertEquals( 1, $result['meta']['total'] );
344
		$this->assertNotNull( $result['data']['id'] );
345
		$this->assertEquals( 'basket', $result['data']['type'] );
346
		$this->assertGreaterThan( 9, count( $result['data']['attributes'] ) );
347
348
		$this->assertArrayNotHasKey( 'errors', $result );
349
	}
350
351
352
	public function testPostPluginException()
353
	{
354
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );
355
356
		$response = $object->post( $this->view->request(), $this->view->response() );
357
		$result = json_decode( (string) $response->getBody(), true );
358
359
360
		$this->assertEquals( 409, $response->getStatusCode() );
361
		$this->assertArrayHasKey( 'errors', $result );
362
	}
363
364
365
	public function testPostMShopException()
366
	{
367
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
368
369
		$response = $object->post( $this->view->request(), $this->view->response() );
370
		$result = json_decode( (string) $response->getBody(), true );
371
372
373
		$this->assertEquals( 404, $response->getStatusCode() );
374
		$this->assertArrayHasKey( 'errors', $result );
375
	}
376
377
378
	public function testPostException()
379
	{
380
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
381
382
		$response = $object->post( $this->view->request(), $this->view->response() );
383
		$result = json_decode( (string) $response->getBody(), true );
384
385
386
		$this->assertEquals( 500, $response->getStatusCode() );
387
		$this->assertArrayHasKey( 'errors', $result );
388
	}
389
390
391
	public function testOptions()
392
	{
393
		$response = $this->object->options( $this->view->request(), $this->view->response() );
394
		$result = json_decode( (string) $response->getBody(), true );
395
396
		$this->assertEquals( 200, $response->getStatusCode() );
397
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
398
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
399
400
		$this->assertEquals( null, $result['meta']['prefix'] );
401
		$this->assertEquals( 1, count( $result['meta']['attributes'] ) );
402
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
403
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
404
		$this->assertArrayNotHasKey( 'errors', $result );
405
	}
406
407
408
	/**
409
	 * Returns a stored basket
410
	 *
411
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Basket object
412
	 */
413
	protected function getOrderBaseItem()
414
	{
415
		$baseManager = \Aimeos\MShop::create( $this->context, 'order/base' );
416
417
		$search = $baseManager->createSearch();
418
		$search->setConditions( $search->compare( '==', 'order.base.price', '672.00') );
419
420
		$items = $baseManager->searchItems( $search );
421
422
		if( ( $item = reset( $items ) ) === false ) {
423
			throw new \Exception( 'No order/base item with price "672.00" found' );
424
		}
425
426
		return $item;
427
	}
428
429
430
	/**
431
	 * Returns a test object with a mocked basket controller
432
	 *
433
	 * @param string $method Basket controller method name to mock
434
	 * @param mixed $result Return value of the mocked method
435
	 */
436
	protected function getObject( $method, $result )
437
	{
438
		$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
439
			->setConstructorArgs( [$this->context] )
440
			->setMethods( [$method] )
441
			->getMock();
442
443
		$cntl->expects( $this->once() )->method( $method )->will( $result );
444
445
		\Aimeos\Controller\Frontend\Basket\Factory::injectController( '\Aimeos\Controller\Frontend\Basket\Standard', $cntl );
446
447
		$object = new \Aimeos\Client\JsonApi\Basket\Standard( $this->context, 'basket' );
448
		$object->setView( $this->view );
449
450
		return $object;
451
	}
452
}
453