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

StandardTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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