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
|
|
|
return $item; |
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( 13, 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( 12, 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 testCreateOrder() |
257
|
|
|
{ |
258
|
|
|
$order = \Aimeos\MShop\Factory::createManager( $this->context, 'order' )->createItem(); |
259
|
|
|
|
260
|
|
|
$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Order\Standard' ) |
261
|
|
|
->setConstructorArgs( [$this->context] ) |
262
|
|
|
->setMethods( ['addItem', 'block'] ) |
263
|
|
|
->getMock(); |
264
|
|
|
|
265
|
|
|
$cntl->expects( $this->once() )->method( 'addItem' )->will( $this->returnValue( $order ) ); |
266
|
|
|
$cntl->expects( $this->once() )->method( 'block' ); |
267
|
|
|
|
268
|
|
|
\Aimeos\Controller\Frontend\Order\Factory::injectController( '\Aimeos\Controller\Frontend\Order\Standard', $cntl ); |
269
|
|
|
$result = $this->access( 'createOrder' )->invokeArgs( $this->object, [-1] ); |
270
|
|
|
\Aimeos\Controller\Frontend\Order\Factory::injectController( '\Aimeos\Controller\Frontend\Order\Standard', null ); |
271
|
|
|
|
272
|
|
|
$this->assertInstanceOf( '\Aimeos\MShop\Order\Item\Iface', $result ); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
|
276
|
|
|
public function testGetBasket() |
277
|
|
|
{ |
278
|
|
|
$basketId = $this->getOrderBaseItem()->getId(); |
279
|
|
|
$this->context->getSession()->set( 'aimeos/order.baseid', $basketId ); |
280
|
|
|
|
281
|
|
|
$result = $this->access( 'getBasket' )->invokeArgs( $this->object, [$basketId] ); |
282
|
|
|
$this->assertInstanceOf( '\Aimeos\MShop\Order\Item\Base\Iface', $result ); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
|
286
|
|
|
public function testGetBasketException() |
287
|
|
|
{ |
288
|
|
|
$basketId = $this->getOrderBaseItem()->getId(); |
289
|
|
|
|
290
|
|
|
$this->setExpectedException( '\Aimeos\Client\JsonApi\Exception' ); |
291
|
|
|
$this->access( 'getBasket' )->invokeArgs( $this->object, [$basketId] ); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
|
295
|
|
|
public function testGetPaymentForm() |
296
|
|
|
{ |
297
|
|
|
$basket = $this->getOrderBaseItem(); |
298
|
|
|
$order = \Aimeos\MShop\Factory::createManager( $this->context, 'order' )->createItem(); |
299
|
|
|
|
300
|
|
|
$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Service\Standard' ) |
301
|
|
|
->setConstructorArgs( [$this->context] ) |
302
|
|
|
->setMethods( ['process'] ) |
303
|
|
|
->getMock(); |
304
|
|
|
|
305
|
|
|
$cntl->expects( $this->once() )->method( 'process' ) |
306
|
|
|
->will( $this->returnValue( new \Aimeos\MShop\Common\Item\Helper\Form\Standard() ) ); |
307
|
|
|
|
308
|
|
|
\Aimeos\Controller\Frontend\Order\Factory::injectController( '\Aimeos\Controller\Frontend\Service\Standard', $cntl ); |
309
|
|
|
$result = $this->access( 'getPaymentForm' )->invokeArgs( $this->object, [$basket, $order, []] ); |
310
|
|
|
\Aimeos\Controller\Frontend\Order\Factory::injectController( '\Aimeos\Controller\Frontend\Service\Standard', null ); |
311
|
|
|
|
312
|
|
|
$this->assertInstanceOf( '\Aimeos\MShop\Common\Item\Helper\Form\Iface', $result ); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
|
316
|
|
|
protected function access( $name ) |
317
|
|
|
{ |
318
|
|
|
$class = new \ReflectionClass( '\Aimeos\Client\JsonApi\Order\Standard' ); |
319
|
|
|
$method = $class->getMethod( $name ); |
320
|
|
|
$method->setAccessible( true ); |
321
|
|
|
|
322
|
|
|
return $method; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Returns a test object with a mocked order controller |
328
|
|
|
* |
329
|
|
|
* @param string $method Order controller method name to mock |
330
|
|
|
* @param mixed $result Return value of the mocked method |
331
|
|
|
*/ |
332
|
|
|
protected function getObject( $method, $result ) |
333
|
|
|
{ |
334
|
|
|
$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Order\Standard' ) |
335
|
|
|
->setConstructorArgs( [$this->context] ) |
336
|
|
|
->setMethods( [$method] ) |
337
|
|
|
->getMock(); |
338
|
|
|
|
339
|
|
|
$cntl->expects( $this->once() )->method( $method )->will( $result ); |
340
|
|
|
|
341
|
|
|
\Aimeos\Controller\Frontend\Order\Factory::injectController( '\Aimeos\Controller\Frontend\Order\Standard', $cntl ); |
342
|
|
|
|
343
|
|
|
$templatePaths = \TestHelperJapi::getTemplatePaths(); |
344
|
|
|
$object = new \Aimeos\Client\JsonApi\Order\Standard( $this->context, $this->view, $templatePaths, 'order' ); |
345
|
|
|
|
346
|
|
|
return $object; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Returns a stored basket |
352
|
|
|
* |
353
|
|
|
* @return \Aimeos\MShop\Order\Item\Base\Iface Basket object |
354
|
|
|
*/ |
355
|
|
|
protected function getOrderBaseItem() |
356
|
|
|
{ |
357
|
|
|
$baseManager = \Aimeos\MShop\Factory::createManager( $this->context, 'order/base' ); |
358
|
|
|
|
359
|
|
|
$search = $baseManager->createSearch(); |
360
|
|
|
$search->setConditions( $search->compare( '==', 'order.base.price', '672.00') ); |
361
|
|
|
|
362
|
|
|
$items = $baseManager->searchItems( $search ); |
363
|
|
|
|
364
|
|
|
if( ( $item = reset( $items ) ) === false ) { |
365
|
|
|
throw new \Exception( 'No order/base item with price "672.00" found' ); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
return $baseManager->load( $item->getId() ); |
369
|
|
|
} |
370
|
|
|
} |