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