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": {"product.id": ' . $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']['relationships']['basket/product']['data'] ) ); |
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( 'basket/product', $result['data']['relationships'] ); |
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": {"product.id": ' . $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']['relationships']['basket/product']['data'] ) ); |
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( 'basket/product', $result['data']['relationships'] ); |
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 testPatch() |
123
|
|
|
{ |
124
|
|
|
$prodId = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' )->getId(); |
125
|
|
|
$body = '{"data": {"type": "basket/product", "attributes": {"product.id": ' . $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']['relationships']['basket/product']['data'] ) ); |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
$body = '{"data": {"type": "basket/product", "id": 0, "attributes": {"quantity": 2}}}'; |
135
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
136
|
|
|
|
137
|
|
|
$response = $this->object->patch( $request, $this->view->response() ); |
138
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
139
|
|
|
|
140
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
141
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
142
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
143
|
|
|
|
144
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
145
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
146
|
|
|
$this->assertArrayHasKey( 'basket/product', $result['data']['relationships'] ); |
147
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/product']['data'] ) ); |
148
|
|
|
$this->assertEquals( 2, $result['included'][0]['attributes']['order.base.product.quantity'] ); |
149
|
|
|
|
150
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
public function testPatchMShopException() |
155
|
|
|
{ |
156
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
157
|
|
|
|
158
|
|
|
$body = '{"data": {"attributes": []}}'; |
159
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
160
|
|
|
|
161
|
|
|
$response = $object->patch( $request, $this->view->response() ); |
162
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
166
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
public function testPatchException() |
171
|
|
|
{ |
172
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) ); |
173
|
|
|
|
174
|
|
|
$body = '{"data": {"attributes": []}}'; |
175
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
176
|
|
|
|
177
|
|
|
$response = $object->patch( $request, $this->view->response() ); |
178
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
182
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
public function testPost() |
187
|
|
|
{ |
188
|
|
|
$prodId = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' )->getId(); |
189
|
|
|
$body = '{"data": {"type": "basket/product", "attributes": {"product.id": ' . $prodId . '}}}'; |
190
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
191
|
|
|
|
192
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
193
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
197
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
198
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
199
|
|
|
|
200
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
201
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
202
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/product']['data'] ) ); |
203
|
|
|
$this->assertEquals( $prodId, $result['included'][0]['attributes']['order.base.product.productid'] ); |
204
|
|
|
|
205
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
|
209
|
|
|
public function testPostMultiple() |
210
|
|
|
{ |
211
|
|
|
$prodId = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' )->getId(); |
212
|
|
|
$prodId2 = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNE' )->getId(); |
213
|
|
|
|
214
|
|
|
$body = '{"data": [{ |
215
|
|
|
"type": "basket/product", "attributes": {"product.id": ' . $prodId . '} |
216
|
|
|
}, { |
217
|
|
|
"type": "basket/product", "attributes": {"product.id": ' . $prodId2 . '} |
218
|
|
|
}]}'; |
219
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
220
|
|
|
|
221
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
222
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
226
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
227
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
228
|
|
|
|
229
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
230
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
231
|
|
|
$this->assertEquals( 2, count( $result['data']['relationships']['basket/product']['data'] ) ); |
232
|
|
|
$this->assertEquals( $prodId, $result['included'][0]['attributes']['order.base.product.productid'] ); |
233
|
|
|
$this->assertEquals( $prodId2, $result['included'][1]['attributes']['order.base.product.productid'] ); |
234
|
|
|
|
235
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
|
239
|
|
|
public function testPostMShopException() |
240
|
|
|
{ |
241
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
242
|
|
|
|
243
|
|
|
$response = $object->post( $this->view->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 testPostException() |
253
|
|
|
{ |
254
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) ); |
255
|
|
|
|
256
|
|
|
$response = $object->post( $this->view->request(), $this->view->response() ); |
257
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
261
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
|
265
|
|
|
public function testOptions() |
266
|
|
|
{ |
267
|
|
|
$response = $this->object->options( $this->view->request(), $this->view->response() ); |
268
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
269
|
|
|
|
270
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
271
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
272
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
273
|
|
|
|
274
|
|
|
$this->assertEquals( null, $result['meta']['prefix'] ); |
275
|
|
|
$this->assertEquals( 8, count( $result['meta']['attributes'] ) ); |
276
|
|
|
$this->assertArrayNotHasKey( 'filter', $result['meta'] ); |
277
|
|
|
$this->assertArrayNotHasKey( 'sort', $result['meta'] ); |
278
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Returns a stored product item from the order |
284
|
|
|
* |
285
|
|
|
* @return \Aimeos\MShop\Order\Item\Base\Product\Iface Ordered product item |
286
|
|
|
*/ |
287
|
|
|
protected function getOrderProductItem() |
288
|
|
|
{ |
289
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'order/base/product' ); |
290
|
|
|
|
291
|
|
|
$search = $manager->createSearch(); |
292
|
|
|
$search->setSlice( 0, 1 ); |
293
|
|
|
|
294
|
|
|
$items = $manager->searchItems( $search ); |
295
|
|
|
|
296
|
|
|
if( ( $item = reset( $items ) ) === false ) { |
297
|
|
|
throw new \Exception( 'No order/base/product item found' ); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
return $item; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
|
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Returns a test object with a mocked basket controller |
307
|
|
|
* |
308
|
|
|
* @param string $method Basket controller method name to mock |
309
|
|
|
* @param mixed $result Return value of the mocked method |
310
|
|
|
*/ |
311
|
|
|
protected function getObject( $method, $result ) |
312
|
|
|
{ |
313
|
|
|
$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' ) |
314
|
|
|
->setConstructorArgs( [$this->context] ) |
315
|
|
|
->setMethods( [$method] ) |
316
|
|
|
->getMock(); |
317
|
|
|
|
318
|
|
|
$cntl->expects( $this->once() )->method( $method )->will( $result ); |
319
|
|
|
|
320
|
|
|
\Aimeos\Controller\Frontend\Basket\Factory::injectController( '\Aimeos\Controller\Frontend\Basket\Standard', $cntl ); |
321
|
|
|
|
322
|
|
|
$templatePaths = \TestHelperJapi::getTemplatePaths(); |
323
|
|
|
$object = new \Aimeos\Client\JsonApi\Basket\Product\Standard( $this->context, $this->view, $templatePaths, 'basket/product' ); |
324
|
|
|
|
325
|
|
|
return $object; |
326
|
|
|
} |
327
|
|
|
} |