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