JsonapiControllerTest::testWorkflowBasketAddress()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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