1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017 |
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() |
20
|
|
|
{ |
21
|
|
|
$this->context = \TestHelperJapi::getContext(); |
22
|
|
|
$templatePaths = \TestHelperJapi::getTemplatePaths(); |
23
|
|
|
$this->view = $this->context->getView(); |
24
|
|
|
|
25
|
|
|
$this->object = new \Aimeos\Client\JsonApi\Basket\Product\Standard( $this->context, $this->view, $templatePaths, 'basket/product' ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
protected function tearDown() |
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\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' )->getId(); |
|
|
|
|
38
|
|
|
$body = '{"data": {"type": "basket/product", "attributes": {"productid": ' . $prodId . '}}}'; |
39
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
40
|
|
|
|
41
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
42
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
43
|
|
|
|
44
|
|
|
$this->assertEquals( 1, count( $result['data']['attributes']['products'] ) ); |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
$body = '{"data": {"type": "basket/product", "id": 0}}'; |
48
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
49
|
|
|
|
50
|
|
|
$response = $this->object->delete( $request, $this->view->response() ); |
51
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
52
|
|
|
|
53
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
54
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
55
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
56
|
|
|
|
57
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
58
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
59
|
|
|
$this->assertArrayNotHasKey( 'product', $result['data']['attributes'] ); |
60
|
|
|
|
61
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
public function testDeleteById() |
66
|
|
|
{ |
67
|
|
|
$prodId = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' )->getId(); |
|
|
|
|
68
|
|
|
$body = '{"data": {"type": "basket/product", "attributes": {"productid": ' . $prodId . '}}}'; |
69
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
70
|
|
|
|
71
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
72
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
73
|
|
|
|
74
|
|
|
$this->assertEquals( 1, count( $result['data']['attributes']['products'] ) ); |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
$params = array( 'id' => 'default', 'relatedid' => 0 ); |
78
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
79
|
|
|
$this->view->addHelper( 'param', $helper ); |
80
|
|
|
|
81
|
|
|
$response = $this->object->delete( $request, $this->view->response() ); |
82
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
83
|
|
|
|
84
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
85
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
86
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
87
|
|
|
|
88
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
89
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
90
|
|
|
$this->assertArrayNotHasKey( 'product', $result['data']['attributes'] ); |
91
|
|
|
|
92
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
public function testDeleteMShopException() |
97
|
|
|
{ |
98
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
99
|
|
|
|
100
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
101
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
105
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
public function testDeleteException() |
110
|
|
|
{ |
111
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) ); |
112
|
|
|
|
113
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
114
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
118
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
public function testGet() |
123
|
|
|
{ |
124
|
|
|
$prodId = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' )->getId(); |
|
|
|
|
125
|
|
|
$body = '{"data": {"type": "basket/product", "attributes": {"productid": ' . $prodId . '}}}'; |
126
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
127
|
|
|
|
128
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
129
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
130
|
|
|
|
131
|
|
|
$this->assertEquals( 1, count( $result['data']['attributes']['products'] ) ); |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
135
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
136
|
|
|
|
137
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
138
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
139
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
140
|
|
|
|
141
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
142
|
|
|
$this->assertEquals( 1, count( $result['data'] ) ); |
143
|
|
|
$this->assertEquals( 'basket/product', $result['data'][0]['type'] ); |
144
|
|
|
$this->assertEquals( $prodId, $result['data'][0]['attributes']['order.base.product.productid'] ); |
145
|
|
|
|
146
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
public function testGetById() |
151
|
|
|
{ |
152
|
|
|
$user = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' )->findItem( 'UTC001' ); |
|
|
|
|
153
|
|
|
$this->context->setUserId( $user->getId() ); |
154
|
|
|
|
155
|
|
|
$orderProduct = $this->getOrderProductItem(); |
156
|
|
|
$params = array( |
157
|
|
|
'id' => $orderProduct->getBaseId(), |
158
|
|
|
'related' => 'product', |
159
|
|
|
'relatedid' => $orderProduct->getId(), |
160
|
|
|
); |
161
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
162
|
|
|
$this->view->addHelper( 'param', $helper ); |
163
|
|
|
|
164
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
165
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
166
|
|
|
|
167
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
168
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
169
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
170
|
|
|
|
171
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
172
|
|
|
$this->assertEquals( 'basket/product', $result['data']['type'] ); |
173
|
|
|
$this->assertNotNull( $result['data']['id'] ); |
174
|
|
|
$this->assertGreaterThan( 21, count( $result['data']['attributes'] ) ); |
175
|
|
|
|
176
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
public function testGetMShopException() |
181
|
|
|
{ |
182
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
183
|
|
|
|
184
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
185
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
189
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
public function testGetException() |
194
|
|
|
{ |
195
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) ); |
196
|
|
|
|
197
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
198
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
202
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
public function testPatch() |
207
|
|
|
{ |
208
|
|
|
$prodId = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' )->getId(); |
|
|
|
|
209
|
|
|
$body = '{"data": {"type": "basket/product", "attributes": {"productid": ' . $prodId . '}}}'; |
210
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
211
|
|
|
|
212
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
213
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
|
|
|
|
214
|
|
|
|
215
|
|
|
|
216
|
|
|
$body = '{"data": {"type": "basket/product", "id": 0, "attributes": {"quantity": 2}}}'; |
217
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
218
|
|
|
|
219
|
|
|
$response = $this->object->patch( $request, $this->view->response() ); |
220
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
221
|
|
|
|
222
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
223
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
224
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
225
|
|
|
|
226
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
227
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
228
|
|
|
$this->assertArrayHasKey( 'products', $result['data']['attributes'] ); |
229
|
|
|
$this->assertEquals( 1, count( $result['data']['attributes']['products'] ) ); |
230
|
|
|
$this->assertEquals( 2, $result['data']['attributes']['products'][0]['order.base.product.quantity'] ); |
231
|
|
|
|
232
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
public function testPatchMShopException() |
237
|
|
|
{ |
238
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
239
|
|
|
|
240
|
|
|
$body = '{"data": {"attributes": []}}'; |
241
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
242
|
|
|
|
243
|
|
|
$response = $object->patch( $request, $this->view->response() ); |
244
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
245
|
|
|
|
246
|
|
|
|
247
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
248
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
public function testPatchException() |
253
|
|
|
{ |
254
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) ); |
255
|
|
|
|
256
|
|
|
$body = '{"data": {"attributes": []}}'; |
257
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
258
|
|
|
|
259
|
|
|
$response = $object->patch( $request, $this->view->response() ); |
260
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
261
|
|
|
|
262
|
|
|
|
263
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
264
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
|
268
|
|
|
public function testPost() |
269
|
|
|
{ |
270
|
|
|
$prodId = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' )->getId(); |
|
|
|
|
271
|
|
|
$body = '{"data": {"type": "basket/product", "attributes": {"productid": ' . $prodId . '}}}'; |
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
|
|
|
|
278
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
279
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
280
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
281
|
|
|
|
282
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
283
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
284
|
|
|
$this->assertEquals( 1, count( $result['data']['attributes']['products'] ) ); |
285
|
|
|
$this->assertEquals( $prodId, $result['data']['attributes']['products'][0]['order.base.product.productid'] ); |
286
|
|
|
|
287
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
|
291
|
|
|
public function testPostMultiple() |
292
|
|
|
{ |
293
|
|
|
$prodId = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' )->getId(); |
|
|
|
|
294
|
|
|
$prodId2 = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNE' )->getId(); |
|
|
|
|
295
|
|
|
|
296
|
|
|
$body = '{"data": [{ |
297
|
|
|
"type": "basket/product", "attributes": {"productid": ' . $prodId . '} |
298
|
|
|
}, { |
299
|
|
|
"type": "basket/product", "attributes": {"productid": ' . $prodId2 . '} |
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']['attributes']['products'] ) ); |
314
|
|
|
$this->assertEquals( $prodId, $result['data']['attributes']['products'][0]['order.base.product.productid'] ); |
315
|
|
|
$this->assertEquals( $prodId2, $result['data']['attributes']['products'][1]['order.base.product.productid'] ); |
316
|
|
|
|
317
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
|
321
|
|
|
public function testPostMShopException() |
322
|
|
|
{ |
323
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\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( 404, $response->getStatusCode() ); |
330
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
|
334
|
|
|
public function testPostException() |
335
|
|
|
{ |
336
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \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( 500, $response->getStatusCode() ); |
343
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Returns a stored product item from the order |
349
|
|
|
* |
350
|
|
|
* @return \Aimeos\MShop\Order\Item\Base\Product\Iface Ordered product item |
351
|
|
|
*/ |
352
|
|
|
protected function getOrderProductItem() |
353
|
|
|
{ |
354
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'order/base/product' ); |
355
|
|
|
|
356
|
|
|
$search = $manager->createSearch(); |
357
|
|
|
$search->setSlice( 0, 1 ); |
358
|
|
|
|
359
|
|
|
$items = $manager->searchItems( $search ); |
360
|
|
|
|
361
|
|
|
if( ( $item = reset( $items ) ) === false ) { |
362
|
|
|
throw new \Exception( 'No order/base/product item found' ); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
return $item; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
|
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Returns a test object with a mocked basket controller |
372
|
|
|
* |
373
|
|
|
* @param string $method Basket controller method name to mock |
374
|
|
|
* @param mixed $result Return value of the mocked method |
375
|
|
|
*/ |
376
|
|
|
protected function getObject( $method, $result ) |
377
|
|
|
{ |
378
|
|
|
$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' ) |
379
|
|
|
->setConstructorArgs( [$this->context] ) |
380
|
|
|
->setMethods( [$method] ) |
381
|
|
|
->getMock(); |
382
|
|
|
|
383
|
|
|
$cntl->expects( $this->once() )->method( $method )->will( $result ); |
384
|
|
|
|
385
|
|
|
\Aimeos\Controller\Frontend\Basket\Factory::injectController( '\Aimeos\Controller\Frontend\Basket\Standard', $cntl ); |
386
|
|
|
|
387
|
|
|
$templatePaths = \TestHelperJapi::getTemplatePaths(); |
388
|
|
|
$object = new \Aimeos\Client\JsonApi\Basket\Product\Standard( $this->context, $this->view, $templatePaths, 'basket/product' ); |
389
|
|
|
|
390
|
|
|
return $object; |
391
|
|
|
} |
392
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: