1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017-2023 |
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 = \TestHelper::context(); |
24
|
|
|
$this->view = $this->context->view(); |
25
|
|
|
|
26
|
|
|
$this->object = new \Aimeos\Client\JsonApi\Customer\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
|
|
|
$user = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' ); |
41
|
|
|
$this->context->setUser( $user ); |
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->setUser( $user ); |
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 testGetIncluded() |
118
|
|
|
{ |
119
|
|
|
$user = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' ); |
120
|
|
|
$this->context->setUser( $user ); |
121
|
|
|
|
122
|
|
|
$params = ['include' => 'customer.address,customer.property']; |
123
|
|
|
$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params ); |
124
|
|
|
$this->view->addHelper( 'param', $helper ); |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
128
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
129
|
|
|
|
130
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
131
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
132
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
133
|
|
|
|
134
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
135
|
|
|
$this->assertEquals( 'customer', $result['data']['type'] ); |
136
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['customer.address']['data'] ) ); |
137
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['customer.property']['data'] ) ); |
138
|
|
|
$this->assertEquals( 2, count( $result['included'] ) ); |
139
|
|
|
|
140
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
public function testGetIncludedNone() |
145
|
|
|
{ |
146
|
|
|
$user = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' ); |
147
|
|
|
$this->context->setUser( $user ); |
148
|
|
|
|
149
|
|
|
$params = ['include' => '']; |
150
|
|
|
$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params ); |
151
|
|
|
$this->view->addHelper( 'param', $helper ); |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
155
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
159
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
160
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
161
|
|
|
|
162
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
163
|
|
|
$this->assertEquals( 'customer', $result['data']['type'] ); |
164
|
|
|
$this->assertArrayNotHasKey( 'relationships', $result['data'] ); |
165
|
|
|
$this->assertEquals( 0, count( $result['included'] ) ); |
166
|
|
|
|
167
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
public function testGetControllerException() |
172
|
|
|
{ |
173
|
|
|
$object = $this->object( 'get', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) ); |
174
|
|
|
|
175
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
176
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
$this->assertEquals( 403, $response->getStatusCode() ); |
180
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
public function testGetMShopException() |
185
|
|
|
{ |
186
|
|
|
$object = $this->object( 'get', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
187
|
|
|
|
188
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
189
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
193
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
public function testGetException() |
198
|
|
|
{ |
199
|
|
|
$object = $this->object( 'get', $this->throwException( new \Exception() ) ); |
200
|
|
|
|
201
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
202
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
206
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
public function testPatch() |
211
|
|
|
{ |
212
|
|
|
$user = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' ); |
213
|
|
|
$this->context->setUser( $user ); |
214
|
|
|
|
215
|
|
|
$this->object( 'store', $this->returnSelf() ); |
216
|
|
|
|
217
|
|
|
$body = '{"data": {"attributes": {"customer.status": 0,"customer.latitude": 50.1}}} '; |
218
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
219
|
|
|
|
220
|
|
|
|
221
|
|
|
$response = $this->object->patch( $request, $this->view->response() ); |
222
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
$this->assertEquals( 200, $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( 'customer', $result['data']['type'] ); |
231
|
|
|
$this->assertGreaterThanOrEqual( 24, count( $result['data']['attributes'] ) ); |
232
|
|
|
$this->assertEquals( '[email protected]', $result['data']['attributes']['customer.code'] ); |
233
|
|
|
$this->assertEquals( '50.1', $result['data']['attributes']['customer.latitude'] ); |
234
|
|
|
|
235
|
|
|
$this->assertArrayNotHasKey( 'customer.status', $result['data']['attributes'] ); |
236
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
public function testPatchControllerException() |
241
|
|
|
{ |
242
|
|
|
$object = $this->object( 'store', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) ); |
243
|
|
|
|
244
|
|
|
$body = '{"data": {"attributes": []}}'; |
245
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
246
|
|
|
|
247
|
|
|
$response = $object->patch( $request, $this->view->response() ); |
248
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
249
|
|
|
|
250
|
|
|
|
251
|
|
|
$this->assertEquals( 403, $response->getStatusCode() ); |
252
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
|
256
|
|
|
public function testPatchMShopException() |
257
|
|
|
{ |
258
|
|
|
$object = $this->object( 'store', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
259
|
|
|
|
260
|
|
|
$body = '{"data": {"attributes": []}}'; |
261
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
262
|
|
|
|
263
|
|
|
$response = $object->patch( $request, $this->view->response() ); |
264
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
265
|
|
|
|
266
|
|
|
|
267
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
268
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
|
272
|
|
|
public function testPatchException() |
273
|
|
|
{ |
274
|
|
|
$object = $this->object( 'store', $this->throwException( new \Exception() ) ); |
275
|
|
|
|
276
|
|
|
$body = '{"data": {"attributes": []}}'; |
277
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
278
|
|
|
|
279
|
|
|
$response = $object->patch( $request, $this->view->response() ); |
280
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
281
|
|
|
|
282
|
|
|
|
283
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
284
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
|
288
|
|
|
public function testPost() |
289
|
|
|
{ |
290
|
|
|
$body = '{"data": {"attributes": {"customer.code": "unittest-japi"}}}'; |
291
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
292
|
|
|
|
293
|
|
|
|
294
|
|
|
$object = $this->object( 'store', $this->returnSelf() ); |
295
|
|
|
$response = $object->post( $request, $this->view->response() ); |
296
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
297
|
|
|
|
298
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
299
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
300
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
301
|
|
|
|
302
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
303
|
|
|
$this->assertNotNull( $result['data']['id'] ); |
304
|
|
|
$this->assertEquals( 'customer', $result['data']['type'] ); |
305
|
|
|
$this->assertEquals( 1, count( $result['data']['attributes'] ) ); // only "customer.id" for POST |
306
|
|
|
|
307
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
public function testPostControllerException() |
312
|
|
|
{ |
313
|
|
|
$object = $this->object( 'store', $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) ); |
314
|
|
|
|
315
|
|
|
$body = '{"data": {"attributes": {}}}'; |
316
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
317
|
|
|
|
318
|
|
|
$response = $object->post( $request, $this->view->response() ); |
319
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
320
|
|
|
|
321
|
|
|
|
322
|
|
|
$this->assertEquals( 403, $response->getStatusCode() ); |
323
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
|
327
|
|
|
public function testPostMShopException() |
328
|
|
|
{ |
329
|
|
|
$object = $this->object( 'store', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
330
|
|
|
|
331
|
|
|
$body = '{"data": {"attributes": {}}}'; |
332
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
333
|
|
|
|
334
|
|
|
$response = $object->post( $request, $this->view->response() ); |
335
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
336
|
|
|
|
337
|
|
|
|
338
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
339
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
|
343
|
|
|
public function testPostException() |
344
|
|
|
{ |
345
|
|
|
$response = $this->object->post( $this->view->request(), $this->view->response() ); |
346
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
347
|
|
|
|
348
|
|
|
$this->assertEquals( 400, $response->getStatusCode() ); |
349
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
|
353
|
|
|
public function testOptions() |
354
|
|
|
{ |
355
|
|
|
$response = $this->object->options( $this->view->request(), $this->view->response() ); |
356
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
357
|
|
|
|
358
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
359
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
360
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
361
|
|
|
|
362
|
|
|
$this->assertEquals( null, $result['meta']['prefix'] ); |
363
|
|
|
$this->assertEquals( 25, count( $result['meta']['attributes'] ) ); |
364
|
|
|
$this->assertArrayNotHasKey( 'filter', $result['meta'] ); |
365
|
|
|
$this->assertArrayNotHasKey( 'sort', $result['meta'] ); |
366
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Returns a test object with a mocked customer controller |
372
|
|
|
* |
373
|
|
|
* @param string $method Customer controller method name to mock |
374
|
|
|
* @param mixed $result Return value of the mocked method |
375
|
|
|
*/ |
376
|
|
|
protected function object( $method, $result ) |
377
|
|
|
{ |
378
|
|
|
$user = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' ); |
379
|
|
|
$this->context->setUser( $user ); |
380
|
|
|
|
381
|
|
|
$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Customer\Standard::class ) |
382
|
|
|
->setConstructorArgs( [$this->context] ) |
383
|
|
|
->onlyMethods( [$method] ) |
384
|
|
|
->getMock(); |
385
|
|
|
|
386
|
|
|
$cntl->expects( $this->once() )->method( $method )->will( $result ); |
387
|
|
|
|
388
|
|
|
\Aimeos\Controller\Frontend::inject( \Aimeos\Controller\Frontend\Customer\Standard::class, $cntl ); |
389
|
|
|
|
390
|
|
|
$object = new \Aimeos\Client\JsonApi\Customer\Standard( $this->context, 'customer' ); |
|
|
|
|
391
|
|
|
$object->setView( $this->view ); |
392
|
|
|
|
393
|
|
|
return $object; |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.