Passed
Push — master ( f17e4f...350b1a )
by Aimeos
16:03
created

StandardTest::controller()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2024
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Customer;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $view;
16
17
18
	protected function setUp() : void
19
	{
20
		\Aimeos\Controller\Frontend::cache( true );
21
22
		$this->context = \TestHelper::context();
23
		$this->view = $this->context->view();
24
25
		$this->context->setUser( \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' ) );
26
	}
27
28
29
	protected function tearDown() : void
30
	{
31
		\Aimeos\Controller\Frontend::cache( false );
32
		unset( $this->view, $this->context );
33
	}
34
35
36
	public function testDelete()
37
	{
38
		$this->controller( 'delete' )->expects( $this->once() )->method( 'delete' )->willReturnSelf();
39
40
		$response = $this->object()->delete( $this->view->request(), $this->view->response() );
41
		$result = json_decode( (string) $response->getBody(), true );
42
43
44
		$this->assertEquals( 200, $response->getStatusCode() );
45
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
46
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
47
48
		$this->assertEquals( 0, $result['meta']['total'] );
49
		$this->assertArrayNotHasKey( 'errors', $result );
50
	}
51
52
53
	public function testDeleteControllerException()
54
	{
55
		$this->controller( 'delete' )->expects( $this->once() )->method( 'delete' )
56
			->will( $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
57
58
		$response = $this->object()->delete( $this->view->request(), $this->view->response() );
59
		$result = json_decode( (string) $response->getBody(), true );
60
61
		$this->assertEquals( 403, $response->getStatusCode() );
62
		$this->assertArrayHasKey( 'errors', $result );
63
	}
64
65
66
	public function testDeleteMShopException()
67
	{
68
		$this->controller( 'delete' )->expects( $this->once() )->method( 'delete' )
69
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
70
71
		$response = $this->object()->delete( $this->view->request(), $this->view->response() );
72
		$result = json_decode( (string) $response->getBody(), true );
73
74
		$this->assertEquals( 404, $response->getStatusCode() );
75
		$this->assertArrayHasKey( 'errors', $result );
76
	}
77
78
79
	public function testDeleteException()
80
	{
81
		$this->controller( 'delete' )->expects( $this->once() )->method( 'delete' )
82
			->will( $this->throwException( new \Exception() ) );
83
84
		$response = $this->object()->delete( $this->view->request(), $this->view->response() );
85
		$result = json_decode( (string) $response->getBody(), true );
86
87
88
		$this->assertEquals( 500, $response->getStatusCode() );
89
		$this->assertArrayHasKey( 'errors', $result );
90
	}
91
92
93
	public function testGet()
94
	{
95
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
96
		$result = json_decode( (string) $response->getBody(), true );
97
98
99
		$this->assertEquals( 200, $response->getStatusCode() );
100
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
101
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
102
103
		$this->assertEquals( 1, $result['meta']['total'] );
104
		$this->assertEquals( 'customer', $result['data']['type'] );
105
		$this->assertGreaterThan( 13, count( $result['data']['attributes'] ) );
106
		$this->assertEquals( 0, count( $result['included'] ) );
107
108
		$this->assertArrayNotHasKey( 'errors', $result );
109
	}
110
111
112
	public function testGetIncluded()
113
	{
114
		$params = ['include' => 'customer.address,customer.property'];
115
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
116
		$this->view->addHelper( 'param', $helper );
117
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( 'customer', $result['data']['type'] );
128
		$this->assertEquals( 1, count( $result['data']['relationships']['customer.address']['data'] ) );
129
		$this->assertEquals( 1, count( $result['data']['relationships']['customer.property']['data'] ) );
130
		$this->assertEquals( 2, count( $result['included'] ) );
131
132
		$this->assertArrayNotHasKey( 'errors', $result );
133
	}
134
135
136
	public function testGetIncludedNone()
137
	{
138
		$params = ['include' => ''];
139
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
140
		$this->view->addHelper( 'param', $helper );
141
142
143
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
144
		$result = json_decode( (string) $response->getBody(), true );
145
146
147
		$this->assertEquals( 200, $response->getStatusCode() );
148
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
149
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
150
151
		$this->assertEquals( 1, $result['meta']['total'] );
152
		$this->assertEquals( 'customer', $result['data']['type'] );
153
		$this->assertArrayNotHasKey( 'relationships', $result['data'] );
154
		$this->assertEquals( 0, count( $result['included'] ) );
155
156
		$this->assertArrayNotHasKey( 'errors', $result );
157
	}
158
159
160
	public function testGetControllerException()
161
	{
162
		$this->controller( 'get' )->expects( $this->once() )->method( 'get' )
163
			->will( $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
164
165
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
166
		$result = json_decode( (string) $response->getBody(), true );
167
168
169
		$this->assertEquals( 403, $response->getStatusCode() );
170
		$this->assertArrayHasKey( 'errors', $result );
171
	}
172
173
174
	public function testGetMShopException()
175
	{
176
		$this->controller( 'get' )->expects( $this->once() )->method( 'get' )
177
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
178
179
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
180
		$result = json_decode( (string) $response->getBody(), true );
181
182
183
		$this->assertEquals( 404, $response->getStatusCode() );
184
		$this->assertArrayHasKey( 'errors', $result );
185
	}
186
187
188
	public function testGetException()
189
	{
190
		$this->controller( 'get' )->expects( $this->once() )->method( 'get' )
191
			->will( $this->throwException( new \Exception() ) );
192
193
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
194
		$result = json_decode( (string) $response->getBody(), true );
195
196
197
		$this->assertEquals( 500, $response->getStatusCode() );
198
		$this->assertArrayHasKey( 'errors', $result );
199
	}
200
201
202
	public function testPatch()
203
	{
204
		$this->controller( 'store' )->expects( $this->once() )->method( 'store' )->willReturnSelf();
205
206
		$body = '{"data": {"attributes": {"customer.status": 0,"customer.latitude": 50.1}}}	';
207
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
208
209
210
		$response = $this->object()->patch( $request, $this->view->response() );
211
		$result = json_decode( (string) $response->getBody(), true );
212
213
214
		$this->assertEquals( 200, $response->getStatusCode() );
215
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
216
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
217
218
		$this->assertEquals( 1, $result['meta']['total'] );
219
		$this->assertEquals( 'customer', $result['data']['type'] );
220
		$this->assertGreaterThanOrEqual( 24, count( $result['data']['attributes'] ) );
221
		$this->assertEquals( '[email protected]', $result['data']['attributes']['customer.code'] );
222
		$this->assertEquals( '50.1', $result['data']['attributes']['customer.latitude'] );
223
224
		$this->assertArrayNotHasKey( 'customer.status', $result['data']['attributes'] );
225
		$this->assertArrayNotHasKey( 'errors', $result );
226
	}
227
228
229
	public function testPatchControllerException()
230
	{
231
		$this->controller( 'store' )->expects( $this->once() )->method( 'store' )
232
			->will( $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
233
234
		$body = '{"data": {"attributes": []}}';
235
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
236
237
		$response = $this->object()->patch( $request, $this->view->response() );
238
		$result = json_decode( (string) $response->getBody(), true );
239
240
241
		$this->assertEquals( 403, $response->getStatusCode() );
242
		$this->assertArrayHasKey( 'errors', $result );
243
	}
244
245
246
	public function testPatchMShopException()
247
	{
248
		$this->controller( 'store' )->expects( $this->once() )->method( 'store' )
249
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
250
251
		$body = '{"data": {"attributes": []}}';
252
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
253
254
		$response = $this->object()->patch( $request, $this->view->response() );
255
		$result = json_decode( (string) $response->getBody(), true );
256
257
258
		$this->assertEquals( 404, $response->getStatusCode() );
259
		$this->assertArrayHasKey( 'errors', $result );
260
	}
261
262
263
	public function testPatchException()
264
	{
265
		$this->controller( 'store' )->expects( $this->once() )->method( 'store' )
266
			->will( $this->throwException( new \Exception() ) );
267
268
		$body = '{"data": {"attributes": []}}';
269
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
270
271
		$response = $this->object()->patch( $request, $this->view->response() );
272
		$result = json_decode( (string) $response->getBody(), true );
273
274
275
		$this->assertEquals( 500, $response->getStatusCode() );
276
		$this->assertArrayHasKey( 'errors', $result );
277
	}
278
279
280
	public function testPost()
281
	{
282
		$body = '{"data": {"attributes": {"customer.code": "unittest-japi"}}}';
283
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
284
285
		$this->controller( 'store' )->expects( $this->once() )->method( 'store' )->willReturnSelf();
286
287
		$response = $this->object()->post( $request, $this->view->response() );
288
		$result = json_decode( (string) $response->getBody(), true );
289
290
		$this->assertEquals( 201, $response->getStatusCode() );
291
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
292
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
293
294
		$this->assertEquals( 1, $result['meta']['total'] );
295
		$this->assertNotNull( $result['data']['id'] );
296
		$this->assertEquals( 'customer', $result['data']['type'] );
297
		$this->assertEquals( 1, count( $result['data']['attributes'] ) ); // only "customer.id" for POST
298
299
		$this->assertArrayNotHasKey( 'errors', $result );
300
	}
301
302
303
	public function testPostControllerException()
304
	{
305
		$this->controller( 'store' )->expects( $this->once() )->method( 'store' )
306
			->will( $this->throwException( new \Aimeos\Controller\Frontend\Customer\Exception() ) );
307
308
		$body = '{"data": {"attributes": {}}}';
309
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
310
311
		$response = $this->object()->post( $request, $this->view->response() );
312
		$result = json_decode( (string) $response->getBody(), true );
313
314
315
		$this->assertEquals( 403, $response->getStatusCode() );
316
		$this->assertArrayHasKey( 'errors', $result );
317
	}
318
319
320
	public function testPostMShopException()
321
	{
322
		$this->controller( 'store' )->expects( $this->once() )->method( 'store' )
323
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
324
325
		$body = '{"data": {"attributes": {}}}';
326
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
327
328
		$response = $this->object()->post( $request, $this->view->response() );
329
		$result = json_decode( (string) $response->getBody(), true );
330
331
332
		$this->assertEquals( 404, $response->getStatusCode() );
333
		$this->assertArrayHasKey( 'errors', $result );
334
	}
335
336
337
	public function testPostException()
338
	{
339
		$response = $this->object()->post( $this->view->request(), $this->view->response() );
340
		$result = json_decode( (string) $response->getBody(), true );
341
342
		$this->assertEquals( 400, $response->getStatusCode() );
343
		$this->assertArrayHasKey( 'errors', $result );
344
	}
345
346
347
	public function testOptions()
348
	{
349
		$response = $this->object()->options( $this->view->request(), $this->view->response() );
350
		$result = json_decode( (string) $response->getBody(), true );
351
352
		$this->assertEquals( 200, $response->getStatusCode() );
353
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
354
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
355
356
		$this->assertEquals( null, $result['meta']['prefix'] );
357
		$this->assertEquals( 25, count( $result['meta']['attributes'] ) );
358
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
359
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
360
		$this->assertArrayNotHasKey( 'errors', $result );
361
	}
362
363
364
	/**
365
	 * Returns a mocked customer controller
366
	 *
367
	 * @param array|string $methods Customer controller method name to mock
368
	 * @return Mocked customer controller
0 ignored issues
show
Bug introduced by
The type Aimeos\Client\JsonApi\Customer\Mocked was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
369
	 */
370
	protected function controller( $methods )
371
	{
372
		$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Customer\Standard::class )
373
			->setConstructorArgs( [$this->context] )
374
			->onlyMethods( (array) $methods )
375
			->getMock();
376
377
		\Aimeos\Controller\Frontend::inject( \Aimeos\Controller\Frontend\Customer\Standard::class, $cntl );
378
379
		return $cntl;
380
	}
381
382
383
	/**
384
	 * Returns the JSON API client object
385
	 *
386
	 * @return \Aimeos\Client\JsonApi\Customer\Standard JSON API client object
387
	 */
388
	protected function object()
389
	{
390
		$object = new \Aimeos\Client\JsonApi\Customer\Standard( $this->context );
391
		$object->setView( $this->view );
392
393
		return $object;
394
	}
395
}
396