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\Customer; |
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 = \TestHelperJapi::getContext(); |
24
|
|
|
$this->view = $this->context->view(); |
25
|
|
|
|
26
|
|
|
$this->object = new \Aimeos\Client\JsonApi\Customer\Standard( $this->context, 'customer' ); |
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
|
|
|
$id = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' )->getId(); |
41
|
|
|
$this->context->setUserId( $id ); |
42
|
|
|
|
43
|
|
|
$this->object( 'delete', $this->returnSelf() ); |
44
|
|
|
|
45
|
|
|
$response = $this->object->delete( $this->view->request(), $this->view->response() ); |
46
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
47
|
|
|
|
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( 0, $result['meta']['total'] ); |
54
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
public function testDeleteControllerException() |
59
|
|
|
{ |
60
|
|
|
$object = $this->object( 'delete', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) ); |
61
|
|
|
|
62
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
63
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
64
|
|
|
|
65
|
|
|
$this->assertEquals( 403, $response->getStatusCode() ); |
66
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
public function testDeleteMShopException() |
71
|
|
|
{ |
72
|
|
|
$object = $this->object( 'delete', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
73
|
|
|
|
74
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
75
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
76
|
|
|
|
77
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
78
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
public function testDeleteException() |
83
|
|
|
{ |
84
|
|
|
$object = $this->object( 'delete', $this->throwException( new \Exception() ) ); |
85
|
|
|
|
86
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
87
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
91
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
public function testGet() |
96
|
|
|
{ |
97
|
|
|
$user = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' ); |
98
|
|
|
$this->context->setUserId( $user->getId() ); |
99
|
|
|
|
100
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
101
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
105
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
106
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
107
|
|
|
|
108
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
109
|
|
|
$this->assertEquals( 'customer', $result['data']['type'] ); |
110
|
|
|
$this->assertGreaterThan( 13, count( $result['data']['attributes'] ) ); |
111
|
|
|
$this->assertEquals( 0, count( $result['included'] ) ); |
112
|
|
|
|
113
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
public function testGetNoAccess() |
118
|
|
|
{ |
119
|
|
|
$this->context->setUserId( -1 ); |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
123
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
127
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
128
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
129
|
|
|
|
130
|
|
|
$this->assertEquals( 0, $result['meta']['total'] ); |
131
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
public function testGetIncluded() |
136
|
|
|
{ |
137
|
|
|
$user = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' ); |
138
|
|
|
$this->context->setUserId( $user->getId() ); |
139
|
|
|
|
140
|
|
|
$params = ['include' => 'customer/address,customer/property']; |
141
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
142
|
|
|
$this->view->addHelper( 'param', $helper ); |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
146
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
147
|
|
|
|
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( 'customer', $result['data']['type'] ); |
155
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['customer/address']['data'] ) ); |
156
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['customer/property']['data'] ) ); |
157
|
|
|
$this->assertEquals( 2, count( $result['included'] ) ); |
158
|
|
|
|
159
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
public function testGetIncludedNone() |
164
|
|
|
{ |
165
|
|
|
$user = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' ); |
166
|
|
|
$this->context->setUserId( $user->getId() ); |
167
|
|
|
|
168
|
|
|
$params = ['include' => '']; |
169
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
170
|
|
|
$this->view->addHelper( 'param', $helper ); |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
174
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
178
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
179
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
180
|
|
|
|
181
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
182
|
|
|
$this->assertEquals( 'customer', $result['data']['type'] ); |
183
|
|
|
$this->assertArrayNotHasKey( 'relationships', $result['data'] ); |
184
|
|
|
$this->assertEquals( 0, count( $result['included'] ) ); |
185
|
|
|
|
186
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
public function testGetControllerException() |
191
|
|
|
{ |
192
|
|
|
$object = $this->object( 'get', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) ); |
193
|
|
|
|
194
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
195
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
$this->assertEquals( 403, $response->getStatusCode() ); |
199
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
public function testGetMShopException() |
204
|
|
|
{ |
205
|
|
|
$object = $this->object( 'get', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
206
|
|
|
|
207
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
208
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
209
|
|
|
|
210
|
|
|
|
211
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
212
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
public function testGetException() |
217
|
|
|
{ |
218
|
|
|
$object = $this->object( 'get', $this->throwException( new \Exception() ) ); |
219
|
|
|
|
220
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
221
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
225
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
public function testPatch() |
230
|
|
|
{ |
231
|
|
|
$id = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' )->getId(); |
232
|
|
|
$this->context->setUserId( $id ); |
233
|
|
|
|
234
|
|
|
$this->object( 'store', $this->returnSelf() ); |
235
|
|
|
|
236
|
|
|
$body = '{"data": {"attributes": {"customer.status": 0,"customer.latitude": 50.1}}} '; |
237
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
$response = $this->object->patch( $request, $this->view->response() ); |
241
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
242
|
|
|
|
243
|
|
|
|
244
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
245
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
246
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
247
|
|
|
|
248
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
249
|
|
|
$this->assertEquals( 'customer', $result['data']['type'] ); |
250
|
|
|
$this->assertGreaterThanOrEqual( 24, count( $result['data']['attributes'] ) ); |
251
|
|
|
$this->assertEquals( '[email protected]', $result['data']['attributes']['customer.code'] ); |
252
|
|
|
$this->assertEquals( '50.1', $result['data']['attributes']['customer.latitude'] ); |
253
|
|
|
|
254
|
|
|
$this->assertArrayNotHasKey( 'customer.status', $result['data']['attributes'] ); |
255
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
|
259
|
|
|
public function testPatchControllerException() |
260
|
|
|
{ |
261
|
|
|
$object = $this->object( 'store', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) ); |
262
|
|
|
|
263
|
|
|
$body = '{"data": {"attributes": []}}'; |
264
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
265
|
|
|
|
266
|
|
|
$response = $object->patch( $request, $this->view->response() ); |
267
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
268
|
|
|
|
269
|
|
|
|
270
|
|
|
$this->assertEquals( 403, $response->getStatusCode() ); |
271
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
|
275
|
|
|
public function testPatchMShopException() |
276
|
|
|
{ |
277
|
|
|
$object = $this->object( 'store', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
278
|
|
|
|
279
|
|
|
$body = '{"data": {"attributes": []}}'; |
280
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
281
|
|
|
|
282
|
|
|
$response = $object->patch( $request, $this->view->response() ); |
283
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
284
|
|
|
|
285
|
|
|
|
286
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
287
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
|
291
|
|
|
public function testPatchException() |
292
|
|
|
{ |
293
|
|
|
$object = $this->object( 'store', $this->throwException( new \Exception() ) ); |
294
|
|
|
|
295
|
|
|
$body = '{"data": {"attributes": []}}'; |
296
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
297
|
|
|
|
298
|
|
|
$response = $object->patch( $request, $this->view->response() ); |
299
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
300
|
|
|
|
301
|
|
|
|
302
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
303
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
|
307
|
|
|
public function testPost() |
308
|
|
|
{ |
309
|
|
|
$body = '{"data": {"attributes": {"customer.code": "unittest-japi"}}}'; |
310
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
311
|
|
|
|
312
|
|
|
|
313
|
|
|
$object = $this->object( 'store', $this->returnSelf() ); |
314
|
|
|
$response = $object->post( $request, $this->view->response() ); |
315
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
316
|
|
|
|
317
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
318
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
319
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
320
|
|
|
|
321
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
322
|
|
|
$this->assertNotNull( $result['data']['id'] ); |
323
|
|
|
$this->assertEquals( 'customer', $result['data']['type'] ); |
324
|
|
|
$this->assertEquals( 1, count( $result['data']['attributes'] ) ); // only "customer.id" for POST |
325
|
|
|
|
326
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
|
330
|
|
|
public function testPostControllerException() |
331
|
|
|
{ |
332
|
|
|
$object = $this->object( 'store', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) ); |
333
|
|
|
|
334
|
|
|
$body = '{"data": {"attributes": {}}}'; |
335
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
336
|
|
|
|
337
|
|
|
$response = $object->post( $request, $this->view->response() ); |
338
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
339
|
|
|
|
340
|
|
|
|
341
|
|
|
$this->assertEquals( 403, $response->getStatusCode() ); |
342
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
|
346
|
|
|
public function testPostMShopException() |
347
|
|
|
{ |
348
|
|
|
$object = $this->object( 'store', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
349
|
|
|
|
350
|
|
|
$body = '{"data": {"attributes": {}}}'; |
351
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
352
|
|
|
|
353
|
|
|
$response = $object->post( $request, $this->view->response() ); |
354
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
355
|
|
|
|
356
|
|
|
|
357
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
358
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
|
362
|
|
|
public function testPostException() |
363
|
|
|
{ |
364
|
|
|
$response = $this->object->post( $this->view->request(), $this->view->response() ); |
365
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
366
|
|
|
|
367
|
|
|
$this->assertEquals( 400, $response->getStatusCode() ); |
368
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
|
372
|
|
|
public function testOptions() |
373
|
|
|
{ |
374
|
|
|
$response = $this->object->options( $this->view->request(), $this->view->response() ); |
375
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
376
|
|
|
|
377
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
378
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
379
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
380
|
|
|
|
381
|
|
|
$this->assertEquals( null, $result['meta']['prefix'] ); |
382
|
|
|
$this->assertEquals( 25, count( $result['meta']['attributes'] ) ); |
383
|
|
|
$this->assertArrayNotHasKey( 'filter', $result['meta'] ); |
384
|
|
|
$this->assertArrayNotHasKey( 'sort', $result['meta'] ); |
385
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Returns a test object with a mocked customer controller |
391
|
|
|
* |
392
|
|
|
* @param string $method Customer controller method name to mock |
393
|
|
|
* @param mixed $result Return value of the mocked method |
394
|
|
|
*/ |
395
|
|
|
protected function object( $method, $result ) |
396
|
|
|
{ |
397
|
|
|
$id = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' )->getId(); |
398
|
|
|
$this->context->setUserId( $id ); |
399
|
|
|
|
400
|
|
|
$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Customer\Standard::class ) |
401
|
|
|
->setConstructorArgs( [$this->context] ) |
402
|
|
|
->setMethods( [$method] ) |
403
|
|
|
->getMock(); |
404
|
|
|
|
405
|
|
|
$cntl->expects( $this->once() )->method( $method )->will( $result ); |
406
|
|
|
|
407
|
|
|
\Aimeos\Controller\Frontend::inject( 'customer', $cntl ); |
408
|
|
|
|
409
|
|
|
$object = new \Aimeos\Client\JsonApi\Customer\Standard( $this->context, 'customer' ); |
410
|
|
|
$object->setView( $this->view ); |
411
|
|
|
|
412
|
|
|
return $object; |
413
|
|
|
} |
414
|
|
|
} |
415
|
|
|
|