Completed
Push — master ( 95e8d1...b686ff )
by Aimeos
02:04
created

JsonapiControllerTest   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 453
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 6
dl 0
loc 453
rs 10
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testOptionsAction() 0 13 1
A testPutAction() 0 12 1
A testGetAttributeAction() 0 13 1
A testGetCatalogAction() 0 13 1
A testGetLocaleAction() 0 13 1
A testGetProductAction() 0 28 1
A testGetServiceAction() 0 13 1
A testGetStockAction() 0 13 1
A testWorkflowCatalog() 0 23 1
A testWorkflowAttributes() 0 25 3
A testWorkflowSearch() 0 13 1
A testWorkflowBasketAddress() 0 22 1
A testWorkflowBasketCoupon() 0 30 1
A testWorkflowBasketProduct() 0 27 1
A testWorkflowBasketService() 0 30 1
A testGetCustomerActionAuthorized() 0 17 1
A testGetCustomerAddressActionAuthorized() 0 22 1
A testGetOrderActionAuthorized() 0 17 1
B testWorkflowOrder() 0 63 1
1
<?php
2
3
namespace Aimeos\ShopBundle\Tests\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6
7
8
class JsonapiControllerTest extends WebTestCase
9
{
10
	protected function setUp()
11
	{
12
		\Aimeos\MShop::cache( false );
13
		\Aimeos\Controller\Frontend::cache( false );
14
	}
15
16
17
	public function testOptionsAction()
18
	{
19
		$client = static::createClient();
20
		$client->request( 'OPTIONS', '/unittest/de/EUR/jsonapi' );
21
		$response = $client->getResponse();
22
23
		$json = json_decode( $response->getContent(), true );
24
25
		$this->assertNotNull( $json );
26
		$this->assertEquals( 200, $response->getStatusCode() );
27
		$this->assertArrayHasKey( 'resources', $json['meta'] );
28
		$this->assertGreaterThan( 1, count( $json['meta']['resources'] ) );
29
	}
30
31
32
	public function testPutAction()
33
	{
34
		$client = static::createClient();
35
		$client->request( 'PUT', '/unittest/de/EUR/jsonapi/basket' );
36
		$response = $client->getResponse();
37
38
		$json = json_decode( $response->getContent(), true );
39
40
		$this->assertNotNull( $json );
41
		$this->assertEquals( 403, $response->getStatusCode() );
42
		$this->assertArrayHasKey( 'errors', $json );
43
	}
44
45
46
	public function testGetAttributeAction()
47
	{
48
		$client = static::createClient();
49
		$client->request( 'GET', '/unittest/de/EUR/jsonapi/attribute', [] );
50
		$response = $client->getResponse();
51
52
		$json = json_decode( $response->getContent(), true );
53
54
		$this->assertNotNull( $json );
55
		$this->assertEquals( 200, $response->getStatusCode() );
56
		$this->assertEquals( 26, $json['meta']['total'] );
57
		$this->assertEquals( 25, count( $json['data'] ) );
58
	}
59
60
61
	public function testGetCatalogAction()
62
	{
63
		$client = static::createClient();
64
		$client->request( 'GET', '/unittest/de/EUR/jsonapi/catalog', [] );
65
		$response = $client->getResponse();
66
67
		$json = json_decode( $response->getContent(), true );
68
69
		$this->assertNotNull( $json );
70
		$this->assertEquals( 200, $response->getStatusCode() );
71
		$this->assertEquals( 1, $json['meta']['total'] );
72
		$this->assertEquals( 4, count( $json['data'] ) );
73
	}
74
75
76
	public function testGetLocaleAction()
77
	{
78
		$client = static::createClient();
79
		$client->request( 'GET', '/unittest/de/EUR/jsonapi/locale', [] );
80
		$response = $client->getResponse();
81
82
		$json = json_decode( $response->getContent(), true );
83
84
		$this->assertNotNull( $json );
85
		$this->assertEquals( 200, $response->getStatusCode() );
86
		$this->assertEquals( 1, $json['meta']['total'] );
87
		$this->assertEquals( 1, count( $json['data'] ) );
88
	}
89
90
91
	public function testGetProductAction()
92
	{
93
		$client = static::createClient();
94
95
		$params = ['filter' => ['f_search' => 'Cafe Noire Cap']];
96
		$client->request( 'GET', '/unittest/de/EUR/jsonapi/product', $params );
97
		$response = $client->getResponse();
98
99
		$json = json_decode( $response->getContent(), true );
100
101
		$this->assertNotNull( $json );
102
		$this->assertEquals( 200, $response->getStatusCode() );
103
		$this->assertEquals( 2, $json['meta']['total'] );
104
		$this->assertEquals( 2, count( $json['data'] ) );
105
		$this->assertArrayHasKey( 'id', $json['data'][0] );
106
		$this->assertEquals( 'CNC', $json['data'][0]['attributes']['product.code'] );
107
108
		$client->request( 'GET', '/unittest/de/EUR/jsonapi/product?id=' . $json['data'][0]['id'] );
109
		$response = $client->getResponse();
110
111
		$json = json_decode( $response->getContent(), true );
112
113
		$this->assertNotNull( $json );
114
		$this->assertEquals( 200, $response->getStatusCode() );
115
		$this->assertEquals( 1, $json['meta']['total'] );
116
		$this->assertArrayHasKey( 'id', $json['data'] );
117
		$this->assertEquals( 'CNC', $json['data']['attributes']['product.code'] );
118
	}
119
120
121
	public function testGetServiceAction()
122
	{
123
		$client = static::createClient();
124
		$client->request( 'GET', '/unittest/de/EUR/jsonapi/service', [] );
125
		$response = $client->getResponse();
126
127
		$json = json_decode( $response->getContent(), true );
128
129
		$this->assertNotNull( $json );
130
		$this->assertEquals( 200, $response->getStatusCode() );
131
		$this->assertEquals( 4, $json['meta']['total'] );
132
		$this->assertEquals( 4, count( $json['data'] ) );
133
	}
134
135
136
	public function testGetStockAction()
137
	{
138
		$client = static::createClient();
139
		$client->request( 'GET', '/unittest/de/EUR/jsonapi/stock', ['filter' => ['s_prodcode' => ['CNC', 'CNE']]] );
140
		$response = $client->getResponse();
141
142
		$json = json_decode( $response->getContent(), true );
143
144
		$this->assertNotNull( $json );
145
		$this->assertEquals( 200, $response->getStatusCode() );
146
		$this->assertEquals( 2, $json['meta']['total'] );
147
		$this->assertEquals( 2, count( $json['data'] ) );
148
	}
149
150
151
	public function testWorkflowCatalog()
152
	{
153
		$client = static::createClient();
154
155
		$client->request( 'OPTIONS', '/unittest/de/EUR/jsonapi' );
156
		$optJson = json_decode( $client->getResponse()->getContent(), true );
157
		$this->assertGreaterThan( 8, count( $optJson['meta']['resources'] ) );
158
159
		// catalog root
160
		$client->request( 'GET', $optJson['meta']['resources']['catalog'], ['include' => 'catalog'] );
161
		$json = json_decode( $client->getResponse()->getContent(), true );
162
		$this->assertEquals( 'categories', $json['included'][0]['attributes']['catalog.code'] );
163
164
		// "categories" category
165
		$client->request( 'GET', $json['included'][0]['links']['self']['href'], ['include' => 'catalog'] );
166
		$json = json_decode( $client->getResponse()->getContent(), true );
167
		$this->assertEquals( 'cafe', $json['included'][0]['attributes']['catalog.code'] );
168
169
		// product list for "cafe" category
170
		$client->request( 'GET', $optJson['meta']['resources']['product'], ['filter' => ['f_catid' => $json['included'][0]['id']]] );
171
		$json = json_decode( $client->getResponse()->getContent(), true );
172
		$this->assertEquals( 'CNE', $json['data'][0]['attributes']['product.code'] );
173
	}
174
175
176
	public function testWorkflowAttributes()
177
	{
178
		$client = static::createClient();
179
180
		$client->request( 'OPTIONS', '/unittest/de/EUR/jsonapi' );
181
		$options = json_decode( $client->getResponse()->getContent(), true );
182
		$this->assertGreaterThan( 8, count( $options['meta']['resources'] ) );
183
184
		// all available attrbutes
185
		$client->request( 'GET', $options['meta']['resources']['attribute'] );
186
		$json = json_decode( $client->getResponse()->getContent(), true );
187
188
		foreach( $json['data'] as $entry )
189
		{
190
			if( $entry['attributes']['attribute.code'] === 'xl' )
191
			{
192
				// products with attrbute "xl"
193
				$client->request( 'GET', $options['meta']['resources']['product'], ['filter' => ['f_attrid' => $entry['id']]] );
194
				break;
195
			}
196
		}
197
198
		$json = json_decode( $client->getResponse()->getContent(), true );
199
		$this->assertEquals( 2, $json['meta']['total'] );
200
	}
201
202
203
	public function testWorkflowSearch()
204
	{
205
		$client = static::createClient();
206
207
		$client->request( 'OPTIONS', '/unittest/de/EUR/jsonapi' );
208
		$json = json_decode( $client->getResponse()->getContent(), true );
209
		$this->assertGreaterThan( 8, count( $json['meta']['resources'] ) );
210
211
		// product list for full text search
212
		$client->request( 'GET', $json['meta']['resources']['product'], ['filter' => ['f_search' => 'cappuccino']] );
213
		$json = json_decode( $client->getResponse()->getContent(), true );
214
		$this->assertEquals( 2, count( $json['data'] ) );
215
	}
216
217
218
	public function testWorkflowBasketAddress()
219
	{
220
		$client = static::createClient();
221
222
		$client->request( 'OPTIONS', '/unittest/de/EUR/jsonapi' );
223
		$json = json_decode( $client->getResponse()->getContent(), true );
224
		$this->assertGreaterThan( 8, count( $json['meta']['resources'] ) );
225
226
		// get empty basket
227
		$client->request( 'GET', $json['meta']['resources']['basket'] );
228
		$json = json_decode( $client->getResponse()->getContent(), true );
229
		$this->assertEquals( 'basket', $json['data']['type'] );
230
231
		$content = '{"data": {"id": "delivery", "attributes": {"order.base.address.firstname": "test"}}}';
232
		$client->request( 'POST', $json['links']['basket/address']['href'], [], [], [], $content );
233
		$json = json_decode( $client->getResponse()->getContent(), true );
234
		$this->assertEquals( 'basket/address', $json['included'][0]['type'] );
235
236
		$client->request( 'DELETE', $json['included'][0]['links']['self']['href'] );
237
		$json = json_decode( $client->getResponse()->getContent(), true );
238
		$this->assertEquals( 0, count( $json['included'] ) );
239
	}
240
241
242
	public function testWorkflowBasketCoupon()
243
	{
244
		$client = static::createClient();
245
246
		$client->request( 'OPTIONS', '/unittest/de/EUR/jsonapi' );
247
		$json = json_decode( $client->getResponse()->getContent(), true );
248
		$this->assertGreaterThan( 8, count( $json['meta']['resources'] ) );
249
250
		// product for code "CNC"
251
		$client->request( 'GET', $json['meta']['resources']['product'], ['filter' => ['==' => ['product.code' => 'CNC']]] );
252
		$json = json_decode( $client->getResponse()->getContent(), true );
253
		$this->assertEquals( 1, count( $json['data'] ) );
254
255
		// add product "CNC" as prerequisite
256
		$content = '{"data": {"attributes": {"product.id": ' . $json['data'][0]['id'] . '}}}';
257
		$client->request( 'POST', $json['data'][0]['links']['basket/product']['href'], [], [], [], $content );
258
		$json = json_decode( $client->getResponse()->getContent(), true );
259
		$this->assertEquals( 'basket/product', $json['included'][0]['type'] );
260
261
		// add coupon "GHIJ"
262
		$content = '{"data": {"id": "GHIJ"}}';
263
		$client->request( 'POST', $json['links']['basket/coupon']['href'], [], [], [], $content );
264
		$json = json_decode( $client->getResponse()->getContent(), true );
265
		$this->assertEquals( 'basket/coupon', $json['included'][2]['type'] );
266
267
		// remove coupon "GHIJ" again
268
		$client->request( 'DELETE', $json['included'][2]['links']['self']['href'] );
269
		$json = json_decode( $client->getResponse()->getContent(), true );
270
		$this->assertEquals( 1, count( $json['included'] ) );
271
	}
272
273
274
	public function testWorkflowBasketProduct()
275
	{
276
		$client = static::createClient();
277
278
		$client->request( 'OPTIONS', '/unittest/de/EUR/jsonapi' );
279
		$json = json_decode( $client->getResponse()->getContent(), true );
280
		$this->assertGreaterThan( 8, count( $json['meta']['resources'] ) );
281
282
		// product for code "CNC"
283
		$client->request( 'GET', $json['meta']['resources']['product'], ['filter' => ['f_search' => 'ABCD']] );
284
		$json = json_decode( $client->getResponse()->getContent(), true );
285
		$this->assertEquals( 1, count( $json['data'] ) );
286
287
		$content = '{"data": {"attributes": {"product.id": ' . $json['data'][0]['id'] . '}}}';
288
		$client->request( 'POST', $json['data'][0]['links']['basket/product']['href'], [], [], [], $content );
289
		$json = json_decode( $client->getResponse()->getContent(), true );
290
		$this->assertEquals( 'basket/product', $json['included'][0]['type'] );
291
292
		$content = '{"data": {"attributes": {"quantity": 2}}}';
293
		$client->request( 'PATCH', $json['included'][0]['links']['self']['href'], [], [], [], $content );
294
		$json = json_decode( $client->getResponse()->getContent(), true );
295
		$this->assertEquals( 2, $json['included'][0]['attributes']['order.base.product.quantity'] );
296
297
		$client->request( 'DELETE', $json['included'][0]['links']['self']['href'] );
298
		$json = json_decode( $client->getResponse()->getContent(), true );
299
		$this->assertEquals( 0, count( $json['included'] ) );
300
	}
301
302
303
	public function testWorkflowBasketService()
304
	{
305
		$client = static::createClient();
306
307
		$client->request( 'OPTIONS', '/unittest/de/EUR/jsonapi' );
308
		$json = json_decode( $client->getResponse()->getContent(), true );
309
		$this->assertGreaterThan( 8, count( $json['meta']['resources'] ) );
310
311
		// payment services
312
		$client->request( 'GET', $json['meta']['resources']['service'], ['filter' => ['cs_type' => 'payment']] );
313
		$json = json_decode( $client->getResponse()->getContent(), true );
314
		$this->assertEquals( 3, count( $json['data'] ) );
315
316
		$content = ['data' => ['id' => 'payment', 'attributes' => [
317
			'service.id' => $json['data'][1]['id'],
318
			'directdebit.accountowner' => 'test user',
319
			'directdebit.accountno' => '12345678',
320
			'directdebit.bankcode' => 'ABCDEFGH',
321
			'directdebit.bankname' => 'test bank',
322
		]]];
323
		$client->request( 'POST', $json['data'][1]['links']['basket/service']['href'], [], [], [], json_encode( $content ) );
324
		$json = json_decode( $client->getResponse()->getContent(), true );
325
		$this->assertEquals( 'basket/service', $json['included'][0]['type'] );
326
		$this->assertEquals( 'directdebit-test', $json['included'][0]['attributes']['order.base.service.code'] );
327
		$this->assertEquals( 5, count( $json['included'][0]['attributes']['attribute'] ) );
328
329
		$client->request( 'DELETE', $json['included'][0]['links']['self']['href'] );
330
		$json = json_decode( $client->getResponse()->getContent(), true );
331
		$this->assertEquals( 0, count( $json['included'] ) );
332
	}
333
334
335
	public function testGetCustomerActionAuthorized()
336
	{
337
		$client = static::createClient(array(), array(
338
			'PHP_AUTH_USER' => 'UTC001',
339
			'PHP_AUTH_PW'   => 'unittest',
340
		) );
341
342
		$client->request( 'GET', '/unittest/de/EUR/jsonapi/customer', [] );
343
		$response = $client->getResponse();
344
345
		$json = json_decode( $response->getContent(), true );
346
347
		$this->assertNotNull( $json );
348
		$this->assertEquals( 200, $response->getStatusCode() );
349
		$this->assertEquals( 1, $json['meta']['total'] );
350
		$this->assertEquals( 4, count( $json['data'] ) );
351
	}
352
353
354
	public function testGetCustomerAddressActionAuthorized()
355
	{
356
		$client = static::createClient(array(), array(
357
			'PHP_AUTH_USER' => 'UTC001',
358
			'PHP_AUTH_PW'   => 'unittest',
359
		) );
360
361
		$client->request( 'GET', '/unittest/de/EUR/jsonapi/customer', [] );
362
		$response = $client->getResponse();
363
364
		$json = json_decode( $response->getContent(), true );
365
366
		$client->request( 'GET', $json['links']['customer/address']['href'], [] );
367
		$response = $client->getResponse();
368
369
		$json = json_decode( $response->getContent(), true );
370
371
		$this->assertNotNull( $json );
372
		$this->assertEquals( 200, $response->getStatusCode() );
373
		$this->assertEquals( 1, $json['meta']['total'] );
374
		$this->assertEquals( 1, count( $json['data'] ) );
375
	}
376
377
378
	public function testGetOrderActionAuthorized()
379
	{
380
		$client = static::createClient(array(), array(
381
			'PHP_AUTH_USER' => 'UTC001',
382
			'PHP_AUTH_PW'   => 'unittest',
383
		) );
384
385
		$client->request( 'GET', '/unittest/de/EUR/jsonapi/order', [] );
386
		$response = $client->getResponse();
387
388
		$json = json_decode( $response->getContent(), true );
389
390
		$this->assertNotNull( $json );
391
		$this->assertEquals( 200, $response->getStatusCode() );
392
		$this->assertEquals( 5, $json['meta']['total'] );
393
		$this->assertEquals( 5, count( $json['data'] ) );
394
	}
395
396
397
	public function testWorkflowOrder()
398
	{
399
		$client = static::createClient();
400
401
		$client->request( 'OPTIONS', '/unittest/de/EUR/jsonapi' );
402
		$optJson = json_decode( $client->getResponse()->getContent(), true );
403
		$this->assertGreaterThan( 8, count( $optJson['meta']['resources'] ) );
404
405
		// product for code "CNC"
406
		$client->request( 'GET', $optJson['meta']['resources']['product'], ['filter' => ['==' => ['product.code' => 'CNC']]] );
407
		$json = json_decode( $client->getResponse()->getContent(), true );
408
		$this->assertEquals( 1, count( $json['data'] ) );
409
410
		// add product "CNC"
411
		$content = '{"data": {"attributes": {"product.id": ' . $json['data'][0]['id'] . '}}}';
412
		$client->request( 'POST', $json['data'][0]['links']['basket/product']['href'], [], [], [], $content );
413
		$json = json_decode( $client->getResponse()->getContent(), true );
414
		$this->assertEquals( 'basket/product', $json['included'][0]['type'] );
415
416
		// delivery services
417
		$client->request( 'GET', $optJson['meta']['resources']['service'], ['filter' => ['cs_type' => 'delivery']] );
418
		$json = json_decode( $client->getResponse()->getContent(), true );
419
		$this->assertEquals( 1, count( $json['data'] ) );
420
421
		// add delivery service
422
		$content = '{"data": {"id": "delivery", "attributes": {"service.id": ' . $json['data'][0]['id'] . '}}}';
423
		$client->request( 'POST', $json['data'][0]['links']['basket/service']['href'], [], [], [], $content );
424
		$json = json_decode( $client->getResponse()->getContent(), true );
425
		$this->assertEquals( 'basket/service', $json['included'][1]['type'] );
426
427
		// payment services
428
		$client->request( 'GET', $optJson['meta']['resources']['service'], ['filter' => ['cs_type' => 'payment']] );
429
		$json = json_decode( $client->getResponse()->getContent(), true );
430
		$this->assertEquals( 3, count( $json['data'] ) );
431
432
		// add payment service
433
		$content = '{"data": {"id": "payment", "attributes": {"service.id": ' . $json['data'][0]['id'] . '}}}';
434
		$client->request( 'POST', $json['data'][0]['links']['basket/service']['href'], [], [], [], $content );
435
		$json = json_decode( $client->getResponse()->getContent(), true );
436
		$this->assertEquals( 'basket/service', $json['included'][2]['type'] );
437
438
		// add address
439
		$content = '{"data": {"id": "payment", "attributes": {"order.base.address.firstname": "test"}}}';
440
		$client->request( 'POST', $json['links']['basket/address']['href'], [], [], [], $content );
441
		$json = json_decode( $client->getResponse()->getContent(), true );
442
		$this->assertEquals( 'basket/address', $json['included'][3]['type'] );
443
444
		// store basket
445
		$client->request( 'POST', $json['data']['links']['self']['href'] );
446
		$basketJson = json_decode( $client->getResponse()->getContent(), true );
447
		$this->assertEquals( true, ctype_digit( $basketJson['data']['id'] ) );
448
449
		// add order
450
		$content = '{"data": {"attributes": {"order.baseid": ' . $basketJson['data']['id'] . '}}}';
451
		$client->request( 'POST', $basketJson['links']['order']['href'], [], [], [], $content );
452
		$json = json_decode( $client->getResponse()->getContent(), true );
453
		$this->assertEquals( true, ctype_digit( $json['data']['id'] ) );
454
455
456
		// delete created order
457
		$context = static::$kernel->getContainer()->get( 'aimeos.context' )->get();
458
		\Aimeos\MShop::create( $context, 'order/base' )->deleteItem( $basketJson['data']['id'] );
459
	}
460
}
461