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\Basket\Service; |
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\Basket\Service\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 testDelete() |
39
|
|
|
{ |
40
|
|
|
$manager = \Aimeos\MShop::create( $this->context, 'service' ); |
41
|
|
|
$servId = $manager->find( 'unitdeliverycode', [], 'service', 'delivery' )->getId(); |
42
|
|
|
|
43
|
|
|
$body = '{"data": {"type": "basket/service", "id": "delivery", "attributes": {"service.id": ' . $servId . '}}}'; |
44
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
45
|
|
|
|
46
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
47
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
48
|
|
|
|
49
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/service']['data'] ) ); |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
$body = '{"data": {"type": "basket/service", "id": "delivery"}}'; |
53
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
54
|
|
|
|
55
|
|
|
$response = $this->object->delete( $request, $this->view->response() ); |
56
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
57
|
|
|
|
58
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
59
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
60
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
61
|
|
|
|
62
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
63
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
64
|
|
|
$this->assertArrayNotHasKey( 'basket/service', $result['data']['relationships'] ); |
65
|
|
|
|
66
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
public function testDeleteById() |
71
|
|
|
{ |
72
|
|
|
$manager = \Aimeos\MShop::create( $this->context, 'service' ); |
73
|
|
|
$servId = $manager->find( 'unitdeliverycode', [], 'service', 'delivery' )->getId(); |
74
|
|
|
|
75
|
|
|
$body = '{"data": {"type": "basket/service", "id": "delivery", "attributes": {"service.id": ' . $servId . '}}}'; |
76
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
77
|
|
|
|
78
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
79
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
80
|
|
|
|
81
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/service']['data'] ) ); |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
$params = array( 'id' => 'default', 'relatedid' => 'delivery' ); |
85
|
|
|
$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params ); |
86
|
|
|
$this->view->addHelper( 'param', $helper ); |
87
|
|
|
|
88
|
|
|
$response = $this->object->delete( $request, $this->view->response() ); |
89
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
90
|
|
|
|
91
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
92
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
93
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
94
|
|
|
|
95
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
96
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
97
|
|
|
$this->assertArrayNotHasKey( 'basket/service', $result['data']['relationships'] ); |
98
|
|
|
|
99
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
public function testDeletePluginException() |
104
|
|
|
{ |
105
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) ); |
106
|
|
|
|
107
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
108
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
$this->assertEquals( 409, $response->getStatusCode() ); |
112
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
public function testDeleteMShopException() |
117
|
|
|
{ |
118
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
119
|
|
|
|
120
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
121
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
125
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
public function testDeleteException() |
130
|
|
|
{ |
131
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Exception() ) ); |
132
|
|
|
|
133
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
134
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
138
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
public function testPatch() |
143
|
|
|
{ |
144
|
|
|
$manager = \Aimeos\MShop::create( $this->context, 'service' ); |
145
|
|
|
$servId = $manager->find( 'directdebit-test', [], 'service', 'payment' )->getId(); |
146
|
|
|
$servId2 = $manager->find( 'unitpaymentcode', [], 'service', 'payment' )->getId(); |
147
|
|
|
|
148
|
|
|
$body = '{"data": {"type": "basket/service", "id": "payment", "attributes": { |
149
|
|
|
"service.id": "' . $servId . '", |
150
|
|
|
"directdebit.accountowner": "test", |
151
|
|
|
"directdebit.accountno": "1234", |
152
|
|
|
"directdebit.bankcode": "5678", |
153
|
|
|
"directdebit.bankname": "test" |
154
|
|
|
}}}'; |
155
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
156
|
|
|
|
157
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
158
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
159
|
|
|
|
160
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
161
|
|
|
$this->assertEquals( 'directdebit-test', $result['included'][0]['attributes']['order.base.service.code'] ); |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
$body = '{"data": {"type": "basket/service", "id": "payment", "attributes": {"service.id": ' . $servId2 . '}}}'; |
165
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
166
|
|
|
|
167
|
|
|
$params = array( 'id' => 'default', 'relatedid' => 'payment' ); |
168
|
|
|
$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params ); |
169
|
|
|
$this->view->addHelper( 'param', $helper ); |
170
|
|
|
|
171
|
|
|
$response = $this->object->patch( $request, $this->view->response() ); |
172
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
173
|
|
|
|
174
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
175
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
176
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
177
|
|
|
|
178
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
179
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
180
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/service']['data'] ) ); |
181
|
|
|
$this->assertEquals( 'unitpaymentcode', $result['included'][0]['attributes']['order.base.service.code'] ); |
182
|
|
|
|
183
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
public function testPatchMultiple() |
188
|
|
|
{ |
189
|
|
|
$manager = \Aimeos\MShop::create( $this->context, 'service' ); |
190
|
|
|
$servId = $manager->find( 'directdebit-test', [], 'service', 'payment' )->getId(); |
191
|
|
|
$servId2 = $manager->find( 'unitpaymentcode', [], 'service', 'payment' )->getId(); |
192
|
|
|
|
193
|
|
|
$body = '{"data": [{ |
194
|
|
|
"type": "basket/service", "id": "payment", "attributes": { |
195
|
|
|
"service.id": "' . $servId . '", |
196
|
|
|
"directdebit.accountowner": "test", |
197
|
|
|
"directdebit.accountno": "1234", |
198
|
|
|
"directdebit.bankcode": "5678", |
199
|
|
|
"directdebit.bankname": "test" |
200
|
|
|
} |
201
|
|
|
}, { |
202
|
|
|
"type": "basket/service", "id": "payment", "attributes": {"service.id": ' . $servId2 . '} |
203
|
|
|
}]}'; |
204
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
205
|
|
|
|
206
|
|
|
$params = array( 'id' => 'default', 'relatedid' => 'payment' ); |
207
|
|
|
$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params ); |
208
|
|
|
$this->view->addHelper( 'param', $helper ); |
209
|
|
|
|
210
|
|
|
$response = $this->object->patch( $request, $this->view->response() ); |
211
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
212
|
|
|
|
213
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
214
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
215
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
216
|
|
|
|
217
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
218
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
219
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/service']['data'] ) ); |
220
|
|
|
$this->assertEquals( 'directdebit-test', $result['included'][0]['attributes']['order.base.service.code'] ); |
221
|
|
|
$this->assertEquals( 'unitpaymentcode', $result['included'][1]['attributes']['order.base.service.code'] ); |
222
|
|
|
|
223
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
public function testPatchPluginException() |
228
|
|
|
{ |
229
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) ); |
230
|
|
|
|
231
|
|
|
$response = $object->patch( $this->view->request(), $this->view->response() ); |
232
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
$this->assertEquals( 409, $response->getStatusCode() ); |
236
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
public function testPatchMShopException() |
241
|
|
|
{ |
242
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
243
|
|
|
|
244
|
|
|
$response = $object->patch( $this->view->request(), $this->view->response() ); |
245
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
249
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
|
253
|
|
|
public function testPatchException() |
254
|
|
|
{ |
255
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Exception() ) ); |
256
|
|
|
|
257
|
|
|
$response = $object->patch( $this->view->request(), $this->view->response() ); |
258
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
262
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
|
266
|
|
|
public function testPost() |
267
|
|
|
{ |
268
|
|
|
$manager = \Aimeos\MShop::create( $this->context, 'service' ); |
269
|
|
|
$servId = $manager->find( 'unitdeliverycode', [], 'service', 'delivery' )->getId(); |
270
|
|
|
|
271
|
|
|
$body = '{"data": {"type": "basket/service", "id": "delivery", "attributes": {"service.id": ' . $servId . '}}}'; |
272
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
273
|
|
|
|
274
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
275
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
276
|
|
|
|
277
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
278
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
279
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
280
|
|
|
|
281
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
282
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
283
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/service']['data'] ) ); |
284
|
|
|
$this->assertEquals( 'unitdeliverycode', $result['included'][0]['attributes']['order.base.service.code'] ); |
285
|
|
|
|
286
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
|
290
|
|
|
public function testPostMultiple() |
291
|
|
|
{ |
292
|
|
|
$manager = \Aimeos\MShop::create( $this->context, 'service' ); |
293
|
|
|
$servId = $manager->find( 'unitdeliverycode', [], 'service', 'delivery' )->getId(); |
294
|
|
|
$servId2 = $manager->find( 'unitpaymentcode', [], 'service', 'payment' )->getId(); |
295
|
|
|
|
296
|
|
|
$body = '{"data": [{ |
297
|
|
|
"type": "basket/service", "id": "delivery", "attributes": {"service.id": ' . $servId . '} |
298
|
|
|
}, { |
299
|
|
|
"type": "basket/service", "id": "payment", "attributes": {"service.id": ' . $servId2 . '} |
300
|
|
|
}]}'; |
301
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
302
|
|
|
|
303
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
304
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
305
|
|
|
|
306
|
|
|
|
307
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
308
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
309
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
310
|
|
|
|
311
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
312
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
313
|
|
|
$this->assertEquals( 2, count( $result['data']['relationships']['basket/service']['data'] ) ); |
314
|
|
|
$this->assertEquals( 'unitdeliverycode', $result['included'][0]['attributes']['order.base.service.code'] ); |
315
|
|
|
$this->assertEquals( 'unitpaymentcode', $result['included'][1]['attributes']['order.base.service.code'] ); |
316
|
|
|
|
317
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
|
321
|
|
|
public function testPostPluginException() |
322
|
|
|
{ |
323
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) ); |
324
|
|
|
|
325
|
|
|
$response = $object->post( $this->view->request(), $this->view->response() ); |
326
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
327
|
|
|
|
328
|
|
|
|
329
|
|
|
$this->assertEquals( 409, $response->getStatusCode() ); |
330
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
|
334
|
|
|
public function testPostMShopException() |
335
|
|
|
{ |
336
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
337
|
|
|
|
338
|
|
|
$response = $object->post( $this->view->request(), $this->view->response() ); |
339
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
340
|
|
|
|
341
|
|
|
|
342
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
343
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
|
347
|
|
|
public function testPostException() |
348
|
|
|
{ |
349
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Exception() ) ); |
350
|
|
|
|
351
|
|
|
$response = $object->post( $this->view->request(), $this->view->response() ); |
352
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
353
|
|
|
|
354
|
|
|
|
355
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
356
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
|
360
|
|
|
public function testOptions() |
361
|
|
|
{ |
362
|
|
|
$response = $this->object->options( $this->view->request(), $this->view->response() ); |
363
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
364
|
|
|
|
365
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
366
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
367
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
368
|
|
|
|
369
|
|
|
$this->assertEquals( null, $result['meta']['prefix'] ); |
370
|
|
|
$this->assertEquals( 1, count( $result['meta']['attributes'] ) ); |
371
|
|
|
$this->assertArrayNotHasKey( 'filter', $result['meta'] ); |
372
|
|
|
$this->assertArrayNotHasKey( 'sort', $result['meta'] ); |
373
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Returns a test object with a mocked basket controller |
379
|
|
|
* |
380
|
|
|
* @param string $method Basket controller method name to mock |
381
|
|
|
* @param mixed $result Return value of the mocked method |
382
|
|
|
*/ |
383
|
|
|
protected function object( $method, $result ) |
384
|
|
|
{ |
385
|
|
|
$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class ) |
386
|
|
|
->setConstructorArgs( [$this->context] ) |
387
|
|
|
->setMethods( [$method] ) |
388
|
|
|
->getMock(); |
389
|
|
|
|
390
|
|
|
$cntl->expects( $this->once() )->method( $method )->will( $result ); |
391
|
|
|
|
392
|
|
|
\Aimeos\Controller\Frontend::inject( '\Aimeos\Controller\Frontend\Basket\Standard', $cntl ); |
393
|
|
|
|
394
|
|
|
$object = new \Aimeos\Client\JsonApi\Basket\Service\Standard( $this->context ); |
395
|
|
|
$object->setView( $this->view ); |
396
|
|
|
|
397
|
|
|
return $object; |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
|