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