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