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