Completed
Push — master ( ce11d5...dfd1f7 )
by Aimeos
02:17
created

StandardTest::testGetPaymentFormNoPayment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 18
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 12
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\Order;
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\Order\Standard( $this->context, $this->view, $templatePaths, 'order' );
26
	}
27
28
29
	protected function tearDown()
30
	{
31
		\Aimeos\Controller\Frontend\Order\Factory::injectController( '\Aimeos\Controller\Frontend\Order\Standard', null );
32
		unset( $this->context, $this->object, $this->view );
33
	}
34
35
36
	public function testGet()
37
	{
38
		$user = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' )->findItem( 'UTC001' );
39
		$this->context->setUserId( $user->getId() );
40
41
		$params = array( 'fields' => array( 'order' => 'order.id,order.type' ) );
42
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
43
		$this->view->addHelper( 'param', $helper );
44
45
46
		$response = $this->object->get( $this->view->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( 4, $result['meta']['total'] );
54
		$this->assertEquals( 'order', $result['data'][0]['type'] );
55
		$this->assertEquals( 2, count( $result['data'][0]['attributes'] ) );
56
		$this->assertArrayNotHasKey( 'errors', $result );
57
	}
58
59
60
	public function testGetById()
61
	{
62
		$this->context->setEditor( 'core:unittest' );
63
64
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'order' );
65
		$search = $manager->createSearch()->setSlice( 0, 1 );
66
		$search->setConditions( $search->compare( '==', 'order.type', 'phone' ) );
67
		$items = $manager->searchItems( $search );
68
69
		if( ( $item = reset( $items ) ) === false ) {
70
			throw new \RuntimeException( 'No order item found' );
71
		}
72
73
		$params = array( 'id' => $item->getId() );
74
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
75
		$this->view->addHelper( 'param', $helper );
76
77
78
		$response = $this->object->get( $this->view->request(), $this->view->response() );
79
		$result = json_decode( (string) $response->getBody(), true );
80
81
		$this->assertEquals( 200, $response->getStatusCode() );
82
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
83
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
84
85
		$this->assertEquals( 1, $result['meta']['total'] );
86
		$this->assertEquals( 'order', $result['data']['type'] );
87
		$this->assertGreaterThan( 7, count( $result['data']['attributes'] ) );
88
		$this->assertArrayNotHasKey( 'errors', $result );
89
	}
90
91
92
	public function testGetControllerException()
93
	{
94
		$object = $this->getObject( 'createFilter', $this->throwException( new \Aimeos\Controller\Frontend\Exception() ) );
95
96
		$response = $object->get( $this->view->request(), $this->view->response() );
97
		$result = json_decode( (string) $response->getBody(), true );
98
99
		$this->assertEquals( 403, $response->getStatusCode() );
100
		$this->assertArrayHasKey( 'errors', $result );
101
	}
102
103
104
	public function testGetMShopException()
105
	{
106
		$object = $this->getObject( 'createFilter', $this->throwException( new \Aimeos\MShop\Exception() ) );
107
108
		$response = $object->get( $this->view->request(), $this->view->response() );
109
		$result = json_decode( (string) $response->getBody(), true );
110
111
		$this->assertEquals( 404, $response->getStatusCode() );
112
		$this->assertArrayHasKey( 'errors', $result );
113
	}
114
115
116
	public function testGetException()
117
	{
118
		$object = $this->getObject( 'createFilter', $this->throwException( new \Exception() ) );
119
120
		$response = $object->get( $this->view->request(), $this->view->response() );
121
		$result = json_decode( (string) $response->getBody(), true );
122
123
		$this->assertEquals( 500, $response->getStatusCode() );
124
		$this->assertArrayHasKey( 'errors', $result );
125
	}
126
127
128
	public function testPost()
129
	{
130
		$basket = \Aimeos\MShop\Factory::createManager( $this->context, 'order/base' )->createItem();
131
		$order = \Aimeos\MShop\Factory::createManager( $this->context, 'order' )->createItem();
132
		$form = new \Aimeos\MShop\Common\Item\Helper\Form\Standard();
133
		$templatePaths = \TestHelperJapi::getTemplatePaths();
134
135
		$object = $this->getMockBuilder( '\Aimeos\Client\JsonApi\Order\Standard' )
136
			->setConstructorArgs( [$this->context, $this->view, $templatePaths, 'order'] )
137
			->setMethods( ['createOrder', 'getBasket', 'getPaymentForm'] )
138
			->getMock();
139
140
		$object->expects( $this->once() )->method( 'getBasket' )->will( $this->returnValue( $basket ) );
141
		$object->expects( $this->once() )->method( 'createOrder' )->will( $this->returnValue( $order ) );
142
		$object->expects( $this->once() )->method( 'getPaymentForm' )->will( $this->returnValue( $form ) );
143
144
		$body = '{"data": {"attributes": {"order.baseid": -1}}}';
145
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
146
147
148
		$response = $object->post( $request, $this->view->response() );
149
		$result = json_decode( (string) $response->getBody(), true );
150
151
		$this->assertEquals( 201, $response->getStatusCode() );
152
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
153
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
154
155
		$this->assertEquals( 1, $result['meta']['total'] );
156
		$this->assertEquals( 'order', $result['data']['type'] );
157
		$this->assertEquals( 8, count( $result['data']['attributes'] ) );
158
		$this->assertArrayNotHasKey( 'errors', $result );
159
	}
160
161
162
	public function testPostNoData()
163
	{
164
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( '{"data": []}' ) );
165
166
		$response = $this->object->post( $request, $this->view->response() );
167
		$result = json_decode( (string) $response->getBody(), true );
168
169
		$this->assertEquals( 400, $response->getStatusCode() );
170
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
171
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
172
173
		$this->assertEquals( 0, $result['meta']['total'] );
174
		$this->assertArrayHasKey( 'errors', $result );
175
	}
176
177
178
	public function testPostNoBaseId()
179
	{
180
		$body = '{"data": {"attributes": {}}}';
181
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
182
183
		$response = $this->object->post( $request, $this->view->response() );
184
		$result = json_decode( (string) $response->getBody(), true );
185
186
		$this->assertEquals( 400, $response->getStatusCode() );
187
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
188
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
189
190
		$this->assertEquals( 0, $result['meta']['total'] );
191
		$this->assertArrayHasKey( 'errors', $result );
192
	}
193
194
195
	public function testPostClientException()
196
	{
197
		$request = $this->getMockBuilder( 'Psr\Http\Message\ServerRequestInterface' )->getMock();
198
199
		$request->expects( $this->once() )->method( 'getBody' )
200
			->will( $this->throwException( new \Aimeos\Client\JsonApi\Exception( '', 400 ) ) );
201
202
		$response = $this->object->post( $request, $this->view->response() );
203
		$result = json_decode( (string) $response->getBody(), true );
204
205
		$this->assertEquals( 400, $response->getStatusCode() );
206
		$this->assertArrayHasKey( 'errors', $result );
207
	}
208
209
210
	public function testPostControllerException()
211
	{
212
		$request = $this->getMockBuilder( 'Psr\Http\Message\ServerRequestInterface' )->getMock();
213
214
		$request->expects( $this->once() )->method( 'getBody' )
215
			->will( $this->throwException( new \Aimeos\Controller\Frontend\Exception() ) );
216
217
		$response = $this->object->post( $request, $this->view->response() );
218
		$result = json_decode( (string) $response->getBody(), true );
219
220
		$this->assertEquals( 403, $response->getStatusCode() );
221
		$this->assertArrayHasKey( 'errors', $result );
222
	}
223
224
225
	public function testPostMShopException()
226
	{
227
		$request = $this->getMockBuilder( 'Psr\Http\Message\ServerRequestInterface' )->getMock();
228
229
		$request->expects( $this->once() )->method( 'getBody' )
230
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
231
232
		$response = $this->object->post( $request, $this->view->response() );
233
		$result = json_decode( (string) $response->getBody(), true );
234
235
		$this->assertEquals( 404, $response->getStatusCode() );
236
		$this->assertArrayHasKey( 'errors', $result );
237
	}
238
239
240
	public function testPostException()
241
	{
242
		$request = $this->getMockBuilder( 'Psr\Http\Message\ServerRequestInterface' )->getMock();
243
244
		$request->expects( $this->once() )->method( 'getBody' )
245
			->will( $this->throwException( new \Exception() ) );
246
247
		$response = $this->object->post( $request, $this->view->response() );
248
		$result = json_decode( (string) $response->getBody(), true );
249
250
		$this->assertEquals( 500, $response->getStatusCode() );
251
		$this->assertArrayHasKey( 'errors', $result );
252
	}
253
254
255
	public function testOptions()
256
	{
257
		$response = $this->object->options( $this->view->request(), $this->view->response() );
258
		$result = json_decode( (string) $response->getBody(), true );
259
260
		$this->assertEquals( 200, $response->getStatusCode() );
261
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
262
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
263
264
		$this->assertEquals( null, $result['meta']['prefix'] );
265
		$this->assertEquals( 1, count( $result['meta']['attributes'] ) );
266
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
267
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
268
		$this->assertArrayNotHasKey( 'errors', $result );
269
	}
270
271
272
	public function testCreateOrder()
273
	{
274
		$order = \Aimeos\MShop\Factory::createManager( $this->context, 'order' )->createItem();
275
276
		$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Order\Standard' )
277
			->setConstructorArgs( [$this->context] )
278
			->setMethods( ['addItem', 'block'] )
279
			->getMock();
280
281
		$cntl->expects( $this->once() )->method( 'addItem' )->will( $this->returnValue( $order ) );
282
		$cntl->expects( $this->once() )->method( 'block' );
283
284
		\Aimeos\Controller\Frontend\Order\Factory::injectController( '\Aimeos\Controller\Frontend\Order\Standard', $cntl );
285
		$result = $this->access( 'createOrder' )->invokeArgs( $this->object, [-1] );
286
		\Aimeos\Controller\Frontend\Order\Factory::injectController( '\Aimeos\Controller\Frontend\Order\Standard', null );
287
288
		$this->assertInstanceOf( '\Aimeos\MShop\Order\Item\Iface', $result );
289
	}
290
291
292
	public function testGetBasket()
293
	{
294
		$basketId = $this->getOrderBaseItem()->getId();
295
		$this->context->getSession()->set( 'aimeos/order.baseid', $basketId );
296
297
		$result = $this->access( 'getBasket' )->invokeArgs( $this->object, [$basketId] );
298
		$this->assertInstanceOf( '\Aimeos\MShop\Order\Item\Base\Iface', $result );
299
	}
300
301
302
	public function testGetBasketException()
303
	{
304
		$basketId = $this->getOrderBaseItem()->getId();
305
306
		$this->setExpectedException( '\Aimeos\Client\JsonApi\Exception' );
307
		$this->access( 'getBasket' )->invokeArgs( $this->object, [$basketId] );
308
	}
309
310
311
	public function testGetPaymentForm()
312
	{
313
		$basket = $this->getOrderBaseItem();
314
		$order = \Aimeos\MShop\Factory::createManager( $this->context, 'order' )->createItem();
315
316
		$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Service\Standard' )
317
			->setConstructorArgs( [$this->context] )
318
			->setMethods( ['process'] )
319
			->getMock();
320
321
		$cntl->expects( $this->once() )->method( 'process' )
322
			->will( $this->returnValue( new \Aimeos\MShop\Common\Item\Helper\Form\Standard() ) );
323
324
		\Aimeos\Controller\Frontend\Service\Factory::injectController( '\Aimeos\Controller\Frontend\Service\Standard', $cntl );
325
		$result = $this->access( 'getPaymentForm' )->invokeArgs( $this->object, [$basket, $order, []] );
326
		\Aimeos\Controller\Frontend\Service\Factory::injectController( '\Aimeos\Controller\Frontend\Service\Standard', null );
327
328
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Helper\Form\Iface', $result );
329
	}
330
331
332
	public function testGetPaymentFormNoPayment()
333
	{
334
		$basket = \Aimeos\MShop\Factory::createManager( $this->context, 'order/base' )->createItem();
335
		$order = \Aimeos\MShop\Factory::createManager( $this->context, 'order' )->createItem();
336
337
		$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Order\Standard' )
338
			->setConstructorArgs( [$this->context] )
339
			->setMethods( ['saveItem'] )
340
			->getMock();
341
342
		$cntl->expects( $this->once() )->method( 'saveItem' );
343
344
		\Aimeos\Controller\Frontend\Order\Factory::injectController( '\Aimeos\Controller\Frontend\Order\Standard', $cntl );
345
		$result = $this->access( 'getPaymentForm' )->invokeArgs( $this->object, [$basket, $order, []] );
346
		\Aimeos\Controller\Frontend\Order\Factory::injectController( '\Aimeos\Controller\Frontend\Order\Standard', null );
347
348
		$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Helper\Form\Iface', $result );
349
	}
350
351
352
	protected function access( $name )
353
	{
354
		$class = new \ReflectionClass( '\Aimeos\Client\JsonApi\Order\Standard' );
355
		$method = $class->getMethod( $name );
356
		$method->setAccessible( true );
357
358
		return $method;
359
	}
360
361
362
	/**
363
	 * Returns a test object with a mocked order controller
364
	 *
365
	 * @param string $method Order controller method name to mock
366
	 * @param mixed $result Return value of the mocked method
367
	 */
368
	protected function getObject( $method, $result )
369
	{
370
		$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Order\Standard' )
371
			->setConstructorArgs( [$this->context] )
372
			->setMethods( [$method] )
373
			->getMock();
374
375
		$cntl->expects( $this->once() )->method( $method )->will( $result );
376
377
		\Aimeos\Controller\Frontend\Order\Factory::injectController( '\Aimeos\Controller\Frontend\Order\Standard', $cntl );
378
379
		$templatePaths = \TestHelperJapi::getTemplatePaths();
380
		$object = new \Aimeos\Client\JsonApi\Order\Standard( $this->context, $this->view, $templatePaths, 'order' );
381
382
		return $object;
383
	}
384
385
386
	/**
387
	 * Returns a stored basket
388
	 *
389
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Basket object
390
	 */
391
	protected function getOrderBaseItem()
392
	{
393
		$baseManager = \Aimeos\MShop\Factory::createManager( $this->context, 'order/base' );
394
395
		$search = $baseManager->createSearch();
396
		$search->setConditions( $search->compare( '==', 'order.base.price', '672.00') );
397
398
		$items = $baseManager->searchItems( $search );
399
400
		if( ( $item = reset( $items ) ) === false ) {
401
			throw new \Exception( 'No order/base item with price "672.00" found' );
402
		}
403
404
		return $baseManager->load( $item->getId() );
405
	}
406
}