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; |
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\Standard( $this->context, $this->view, $templatePaths, 'basket' ); |
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
|
|
|
$body = '{"data": {"attributes": {"order.base.comment": "test"}}}'; |
38
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
39
|
|
|
|
40
|
|
|
$response = $this->object->patch( $request, $this->view->response() ); |
41
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
42
|
|
|
|
43
|
|
|
$this->assertEquals( 'test', $result['data']['attributes']['order.base.comment'] ); |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
$response = $this->object->delete( $request, $this->view->response() ); |
47
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
48
|
|
|
|
49
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
50
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
51
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
52
|
|
|
|
53
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
54
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
55
|
|
|
$this->assertGreaterThan( 13, count( $result['data']['attributes'] ) ); |
56
|
|
|
$this->assertEquals( '', $result['data']['attributes']['order.base.comment'] ); |
57
|
|
|
|
58
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
public function testDeleteMShopException() |
63
|
|
|
{ |
64
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
65
|
|
|
|
66
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
67
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
71
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
public function testDeleteException() |
76
|
|
|
{ |
77
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) ); |
78
|
|
|
|
79
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
80
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
84
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
public function testGet() |
89
|
|
|
{ |
90
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
91
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
92
|
|
|
|
93
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
94
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
95
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
96
|
|
|
|
97
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
98
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
99
|
|
|
$this->assertGreaterThan( 13, count( $result['data']['attributes'] ) ); |
100
|
|
|
|
101
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
public function testGetById() |
106
|
|
|
{ |
107
|
|
|
$user = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' )->findItem( 'UTC001' ); |
|
|
|
|
108
|
|
|
$this->context->setUserId( $user->getId() ); |
109
|
|
|
|
110
|
|
|
$params = array( |
111
|
|
|
'id' => $this->getOrderBaseItem()->getId(), |
112
|
|
|
'fields' => array( |
113
|
|
|
'basket' => 'order.base.id,order.base.comment' |
114
|
|
|
), |
115
|
|
|
); |
116
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
117
|
|
|
$this->view->addHelper( 'param', $helper ); |
118
|
|
|
|
119
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
120
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
121
|
|
|
|
122
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
123
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
124
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
125
|
|
|
|
126
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
127
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
128
|
|
|
$this->assertNotNull( $result['data']['id'] ); |
129
|
|
|
$this->assertEquals( 2, count( $result['data']['attributes'] ) ); |
130
|
|
|
$this->assertEquals( 'This is another comment.', $result['data']['attributes']['order.base.comment'] ); |
131
|
|
|
|
132
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
public function testGetNoAccess() |
137
|
|
|
{ |
138
|
|
|
$this->context->setUserId( null ); |
139
|
|
|
|
140
|
|
|
$params = array( |
141
|
|
|
'id' => $this->getOrderBaseItem()->getId(), |
142
|
|
|
); |
143
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
144
|
|
|
$this->view->addHelper( 'param', $helper ); |
145
|
|
|
|
146
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
147
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
148
|
|
|
|
149
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
150
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
151
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
152
|
|
|
|
153
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
154
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
155
|
|
|
$this->assertNull( $result['data']['id'] ); |
156
|
|
|
|
157
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
public function testGetMShopException() |
162
|
|
|
{ |
163
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
164
|
|
|
|
165
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
166
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
170
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
public function testGetException() |
175
|
|
|
{ |
176
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) ); |
177
|
|
|
|
178
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
179
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
183
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
public function testPatch() |
188
|
|
|
{ |
189
|
|
|
$body = '{"data": {"attributes": {"order.base.comment": "test"}}} '; |
190
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
191
|
|
|
|
192
|
|
|
$response = $this->object->patch( $request, $this->view->response() ); |
193
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
$this->assertEquals( 200, $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->assertGreaterThan( 13, count( $result['data']['attributes'] ) ); |
203
|
|
|
$this->assertEquals( 'test', $result['data']['attributes']['order.base.comment'] ); |
204
|
|
|
|
205
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
|
209
|
|
|
public function testPatchMShopException() |
210
|
|
|
{ |
211
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
212
|
|
|
|
213
|
|
|
$body = '{"data": {"attributes": []}}'; |
214
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
215
|
|
|
|
216
|
|
|
$response = $object->patch( $request, $this->view->response() ); |
217
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
221
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
public function testPatchException() |
226
|
|
|
{ |
227
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) ); |
228
|
|
|
|
229
|
|
|
$body = '{"data": {"attributes": []}}'; |
230
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
231
|
|
|
|
232
|
|
|
$response = $object->patch( $request, $this->view->response() ); |
233
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
237
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
public function testPost() |
242
|
|
|
{ |
243
|
|
|
$orderBaseItem = $this->getOrderBaseItem(); |
244
|
|
|
$object = $this->getObject( 'store', $this->returnValue( $orderBaseItem ) ); |
245
|
|
|
|
246
|
|
|
$body = '{"data": {"attributes": {"order.base.comment": "test"}}}'; |
247
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
248
|
|
|
|
249
|
|
|
$response = $object->post( $request, $this->view->response() ); |
250
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
251
|
|
|
|
252
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
253
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
254
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
255
|
|
|
|
256
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
257
|
|
|
$this->assertNotNull( $result['data']['id'] ); |
258
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
259
|
|
|
$this->assertGreaterThan( 13, count( $result['data']['attributes'] ) ); |
260
|
|
|
|
261
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
|
265
|
|
|
public function testPostMShopException() |
266
|
|
|
{ |
267
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
268
|
|
|
|
269
|
|
|
$response = $object->post( $this->view->request(), $this->view->response() ); |
270
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
271
|
|
|
|
272
|
|
|
|
273
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
274
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
|
278
|
|
|
public function testPostException() |
279
|
|
|
{ |
280
|
|
|
$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) ); |
281
|
|
|
|
282
|
|
|
$response = $object->post( $this->view->request(), $this->view->response() ); |
283
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
284
|
|
|
|
285
|
|
|
|
286
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
287
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Returns a stored basket |
293
|
|
|
* |
294
|
|
|
* @return \Aimeos\MShop\Order\Item\Base\Iface Basket object |
295
|
|
|
*/ |
296
|
|
|
protected function getOrderBaseItem() |
297
|
|
|
{ |
298
|
|
|
$baseManager = \Aimeos\MShop\Factory::createManager( $this->context, 'order/base' ); |
299
|
|
|
|
300
|
|
|
$search = $baseManager->createSearch(); |
301
|
|
|
$search->setConditions( $search->compare( '==', 'order.base.price', '672.00') ); |
302
|
|
|
|
303
|
|
|
$items = $baseManager->searchItems( $search ); |
304
|
|
|
|
305
|
|
|
if( ( $item = reset( $items ) ) === false ) { |
306
|
|
|
throw new \Exception( 'No order/base item with price "672.00" found' ); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return $item; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Returns a test object with a mocked basket controller |
315
|
|
|
* |
316
|
|
|
* @param string $method Basket controller method name to mock |
317
|
|
|
* @param mixed $result Return value of the mocked method |
318
|
|
|
*/ |
319
|
|
|
protected function getObject( $method, $result ) |
320
|
|
|
{ |
321
|
|
|
$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' ) |
322
|
|
|
->setConstructorArgs( [$this->context] ) |
323
|
|
|
->setMethods( [$method] ) |
324
|
|
|
->getMock(); |
325
|
|
|
|
326
|
|
|
$cntl->expects( $this->once() )->method( $method )->will( $result ); |
327
|
|
|
|
328
|
|
|
\Aimeos\Controller\Frontend\Basket\Factory::injectController( '\Aimeos\Controller\Frontend\Basket\Standard', $cntl ); |
329
|
|
|
|
330
|
|
|
$templatePaths = \TestHelperJapi::getTemplatePaths(); |
331
|
|
|
$object = new \Aimeos\Client\JsonApi\Basket\Standard( $this->context, $this->view, $templatePaths, 'basket' ); |
332
|
|
|
|
333
|
|
|
return $object; |
334
|
|
|
} |
335
|
|
|
} |
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: