Completed
Push — master ( 361140...04e2fd )
by Aimeos
17:05
created

JsonapiControllerTest::testGetServiceAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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