Completed
Push — master ( e8440a...f62092 )
by Aimeos
02:50
created

StandardTest::testGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017
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()
20
	{
21
		$this->context = \TestHelperJapi::getContext();
22
		$templatePaths = \TestHelperJapi::getTemplatePaths();
23
		$this->view = $this->context->getView();
24
25
		$this->object = new \Aimeos\Client\JsonApi\Basket\Standard( $this->context, $this->view, $templatePaths, 'basket' );
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 testDeleteMShopException()
63
	{
64
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
65
66
		$response = $object->delete( $this->view->request(), $this->view->response() );
67
		$result = json_decode( (string) $response->getBody(), true );
68
69
		$this->assertEquals( 404, $response->getStatusCode() );
70
		$this->assertArrayHasKey( 'errors', $result );
71
	}
72
73
74
	public function testDeleteException()
75
	{
76
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
77
78
		$response = $object->delete( $this->view->request(), $this->view->response() );
79
		$result = json_decode( (string) $response->getBody(), true );
80
81
82
		$this->assertEquals( 500, $response->getStatusCode() );
83
		$this->assertArrayHasKey( 'errors', $result );
84
	}
85
86
87
	public function testGet()
88
	{
89
		$response = $this->object->get( $this->view->request(), $this->view->response() );
90
		$result = json_decode( (string) $response->getBody(), true );
91
92
		$this->assertEquals( 200, $response->getStatusCode() );
93
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
94
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
95
96
		$this->assertEquals( 1, $result['meta']['total'] );
97
		$this->assertEquals( 'basket', $result['data']['type'] );
98
		$this->assertGreaterThan( 9, count( $result['data']['attributes'] ) );
99
100
		$this->assertArrayNotHasKey( 'errors', $result );
101
	}
102
103
104
	public function testGetById()
105
	{
106
		$user = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' )->findItem( 'UTC001' );
107
		$this->context->setUserId( $user->getId() );
108
109
		$params = array(
110
			'id' => $this->getOrderBaseItem()->getId(),
111
			'fields' => array(
112
				'basket' => 'order.base.id,order.base.comment'
113
			),
114
		);
115
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
116
		$this->view->addHelper( 'param', $helper );
117
118
		$response = $this->object->get( $this->view->request(), $this->view->response() );
119
		$result = json_decode( (string) $response->getBody(), true );
120
121
		$this->assertEquals( 200, $response->getStatusCode() );
122
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
123
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
124
125
		$this->assertEquals( 1, $result['meta']['total'] );
126
		$this->assertEquals( 'basket', $result['data']['type'] );
127
		$this->assertNotNull( $result['data']['id'] );
128
		$this->assertGreaterThan( 1, count( $result['data']['attributes'] ) );
129
		$this->assertEquals( 'This is another comment.', $result['data']['attributes']['order.base.comment'] );
130
		$this->assertEquals( 8, count( $result['included'] ) );
131
132
		$this->assertArrayNotHasKey( 'errors', $result );
133
	}
134
135
136
	public function testGetNoAccess()
137
	{
138
		$this->context->setUserId( null );
139
140
		$params = array(
141
			'id' => $this->getOrderBaseItem()->getId(),
142
		);
143
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
144
		$this->view->addHelper( 'param', $helper );
145
146
		$response = $this->object->get( $this->view->request(), $this->view->response() );
147
		$result = json_decode( (string) $response->getBody(), true );
148
149
		$this->assertEquals( 200, $response->getStatusCode() );
150
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
151
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
152
153
		$this->assertEquals( 1, $result['meta']['total'] );
154
		$this->assertEquals( 'basket', $result['data']['type'] );
155
		$this->assertNull( $result['data']['attributes']['order.base.id'] );
156
157
		$this->assertArrayNotHasKey( 'errors', $result );
158
	}
159
160
161
	public function testGetIncluded()
162
	{
163
		$user = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' )->findItem( 'UTC001' );
164
		$this->context->setUserId( $user->getId() );
165
166
		$params = array(
167
			'id' => $this->getOrderBaseItem()->getId(),
168
			'included' => 'basket/product',
169
		);
170
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
171
		$this->view->addHelper( 'param', $helper );
172
173
		$response = $this->object->get( $this->view->request(), $this->view->response() );
174
		$result = json_decode( (string) $response->getBody(), true );
175
176
177
		$this->assertEquals( 200, $response->getStatusCode() );
178
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
179
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
180
181
		$this->assertEquals( 1, $result['meta']['total'] );
182
		$this->assertEquals( 'basket', $result['data']['type'] );
183
		$this->assertEquals( 1, count( $result['data']['relationships'] ) );
184
		$this->assertArrayHasKey( 'basket/product', $result['data']['relationships'] );
185
		$this->assertEquals( 2, count( $result['data']['relationships']['basket/product']['data'] ) );
186
		$this->assertEquals( 2, count( $result['included'] ) );
187
188
		$this->assertArrayNotHasKey( 'errors', $result );
189
	}
190
191
192
	public function testGetIncludedNone()
193
	{
194
		$user = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' )->findItem( 'UTC001' );
195
		$this->context->setUserId( $user->getId() );
196
197
		$params = array(
198
			'id' => $this->getOrderBaseItem()->getId(),
199
			'included' => '',
200
		);
201
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
202
		$this->view->addHelper( 'param', $helper );
203
204
		$response = $this->object->get( $this->view->request(), $this->view->response() );
205
		$result = json_decode( (string) $response->getBody(), true );
206
207
		$this->assertEquals( 200, $response->getStatusCode() );
208
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
209
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
210
211
		$this->assertEquals( 1, $result['meta']['total'] );
212
		$this->assertEquals( 'basket', $result['data']['type'] );
213
		$this->assertEquals( 0, count( $result['data']['relationships'] ) );
214
		$this->assertEquals( 0, count( $result['included'] ) );
215
216
		$this->assertArrayNotHasKey( 'errors', $result );
217
	}
218
219
220
	public function testGetMShopException()
221
	{
222
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
223
224
		$response = $object->get( $this->view->request(), $this->view->response() );
225
		$result = json_decode( (string) $response->getBody(), true );
226
227
228
		$this->assertEquals( 404, $response->getStatusCode() );
229
		$this->assertArrayHasKey( 'errors', $result );
230
	}
231
232
233
	public function testGetException()
234
	{
235
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
236
237
		$response = $object->get( $this->view->request(), $this->view->response() );
238
		$result = json_decode( (string) $response->getBody(), true );
239
240
241
		$this->assertEquals( 500, $response->getStatusCode() );
242
		$this->assertArrayHasKey( 'errors', $result );
243
	}
244
245
246
	public function testPatch()
247
	{
248
		$body = '{"data": {"attributes": {"order.base.comment": "test"}}}	';
249
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
250
251
		$response = $this->object->patch( $request, $this->view->response() );
252
		$result = json_decode( (string) $response->getBody(), true );
253
254
255
		$this->assertEquals( 200, $response->getStatusCode() );
256
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
257
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
258
259
		$this->assertEquals( 1, $result['meta']['total'] );
260
		$this->assertEquals( 'basket', $result['data']['type'] );
261
		$this->assertGreaterThan( 9, count( $result['data']['attributes'] ) );
262
		$this->assertEquals( 'test', $result['data']['attributes']['order.base.comment'] );
263
264
		$this->assertArrayNotHasKey( 'errors', $result );
265
	}
266
267
268
	public function testPatchMShopException()
269
	{
270
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
271
272
		$body = '{"data": {"attributes": []}}';
273
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
274
275
		$response = $object->patch( $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 testPatchException()
285
	{
286
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
287
288
		$body = '{"data": {"attributes": []}}';
289
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
290
291
		$response = $object->patch( $request, $this->view->response() );
292
		$result = json_decode( (string) $response->getBody(), true );
293
294
295
		$this->assertEquals( 500, $response->getStatusCode() );
296
		$this->assertArrayHasKey( 'errors', $result );
297
	}
298
299
300
	public function testPost()
301
	{
302
		$orderBaseItem = $this->getOrderBaseItem();
303
		$object = $this->getObject( 'store', $this->returnValue( $orderBaseItem ) );
304
305
		$body = '{"data": {"attributes": {"order.base.comment": "test"}}}';
306
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
307
308
		$response = $object->post( $request, $this->view->response() );
309
		$result = json_decode( (string) $response->getBody(), true );
310
311
		$this->assertEquals( 200, $response->getStatusCode() );
312
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
313
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
314
315
		$this->assertEquals( 1, $result['meta']['total'] );
316
		$this->assertNotNull( $result['data']['id'] );
317
		$this->assertEquals( 'basket', $result['data']['type'] );
318
		$this->assertGreaterThan( 9, count( $result['data']['attributes'] ) );
319
320
		$this->assertArrayNotHasKey( 'errors', $result );
321
	}
322
323
324
	public function testPostMShopException()
325
	{
326
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
327
328
		$response = $object->post( $this->view->request(), $this->view->response() );
329
		$result = json_decode( (string) $response->getBody(), true );
330
331
332
		$this->assertEquals( 404, $response->getStatusCode() );
333
		$this->assertArrayHasKey( 'errors', $result );
334
	}
335
336
337
	public function testPostException()
338
	{
339
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
340
341
		$response = $object->post( $this->view->request(), $this->view->response() );
342
		$result = json_decode( (string) $response->getBody(), true );
343
344
345
		$this->assertEquals( 500, $response->getStatusCode() );
346
		$this->assertArrayHasKey( 'errors', $result );
347
	}
348
349
350
	public function testOptions()
351
	{
352
		$response = $this->object->options( $this->view->request(), $this->view->response() );
353
		$result = json_decode( (string) $response->getBody(), true );
354
355
		$this->assertEquals( 200, $response->getStatusCode() );
356
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
357
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
358
359
		$this->assertEquals( null, $result['meta']['prefix'] );
360
		$this->assertEquals( 1, count( $result['meta']['attributes'] ) );
361
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
362
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
363
		$this->assertArrayNotHasKey( 'errors', $result );
364
	}
365
366
367
	/**
368
	 * Returns a stored basket
369
	 *
370
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Basket object
371
	 */
372
	protected function getOrderBaseItem()
373
	{
374
		$baseManager = \Aimeos\MShop\Factory::createManager( $this->context, 'order/base' );
375
376
		$search = $baseManager->createSearch();
377
		$search->setConditions( $search->compare( '==', 'order.base.price', '672.00') );
378
379
		$items = $baseManager->searchItems( $search );
380
381
		if( ( $item = reset( $items ) ) === false ) {
382
			throw new \Exception( 'No order/base item with price "672.00" found' );
383
		}
384
385
		return $item;
386
	}
387
388
389
	/**
390
	 * Returns a test object with a mocked basket controller
391
	 *
392
	 * @param string $method Basket controller method name to mock
393
	 * @param mixed $result Return value of the mocked method
394
	 */
395
	protected function getObject( $method, $result )
396
	{
397
		$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' )
398
			->setConstructorArgs( [$this->context] )
399
			->setMethods( [$method] )
400
			->getMock();
401
402
		$cntl->expects( $this->once() )->method( $method )->will( $result );
403
404
		\Aimeos\Controller\Frontend\Basket\Factory::injectController( '\Aimeos\Controller\Frontend\Basket\Standard', $cntl );
405
406
		$templatePaths = \TestHelperJapi::getTemplatePaths();
407
		$object = new \Aimeos\Client\JsonApi\Basket\Standard( $this->context, $this->view, $templatePaths, 'basket' );
408
409
		return $object;
410
	}
411
}