Completed
Push — master ( 04e2fd...d54d2f )
by Aimeos
02:31
created

JsonapiControllerTest::testPutAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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