JsonapiControllerTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 4
eloc 54
c 5
b 0
f 0
dl 0
loc 97
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testPutAction() 0 9 1
A testPostPatchDeleteAction() 0 34 1
A testOptionsAction() 0 13 1
A testGetAction() 0 30 1
1
<?php
2
3
class JsonapiControllerTest extends AimeosTestAbstract
4
{
5
	public function testOptionsAction()
6
	{
7
		View::addLocation(dirname(__DIR__).'/fixtures/views');
8
9
		$params = ['site' => 'unittest'];
10
		$response = $this->action('OPTIONS', '\Aimeos\Shop\Controller\[email protected]', $params);
11
12
		$json = json_decode( $response->getContent(), true );
13
14
		$this->assertNotNull( $json );
15
		$this->assertEquals( 200, $response->getStatusCode() );
16
		$this->assertArrayHasKey( 'resources', $json['meta'] );
17
		$this->assertGreaterThan( 1, count( $json['meta']['resources'] ) );
18
	}
19
20
21
	public function testGetAction()
22
	{
23
		View::addLocation(dirname(__DIR__).'/fixtures/views');
24
25
		$params = ['site' => 'unittest', 'resource' => 'product'];
26
		$getParams = ['filter' => ['f_search' => 'Cafe Noire Cap']];
27
		$response = $this->action('GET', '\Aimeos\Shop\Controller\[email protected]', $params, $getParams);
28
29
		$json = json_decode( $response->getContent(), true );
30
31
		$this->assertResponseOk();
32
		$this->assertNotNull( $json );
33
		$this->assertEquals( 1, $json['meta']['total'] );
34
		$this->assertEquals( 1, count( $json['data'] ) );
35
		$this->assertArrayHasKey( 'id', $json['data'][0] );
36
		$this->assertEquals( 'CNC', $json['data'][0]['attributes']['product.code'] );
37
38
		$id = $json['data'][0]['id'];
39
40
41
		$params = ['site' => 'unittest', 'resource' => 'product', 'id' => $id ];
42
		$response = $this->action('GET', '\Aimeos\Shop\Controller\[email protected]', $params);
43
44
		$json = json_decode( $response->getContent(), true );
45
46
		$this->assertResponseOk();
47
		$this->assertNotNull( $json );
48
		$this->assertEquals( 1, $json['meta']['total'] );
49
		$this->assertArrayHasKey( 'id', $json['data'] );
50
		$this->assertEquals( 'CNC', $json['data']['attributes']['product.code'] );
51
	}
52
53
54
	public function testPostPatchDeleteAction()
55
	{
56
		View::addLocation(dirname(__DIR__).'/fixtures/views');
57
58
		// get CNC product
59
		$params = ['site' => 'unittest', 'resource' => 'product'];
60
		$getParams = ['filter' => ['f_search' => 'Cafe Noire Cap', 'f_listtype' => 'unittype19']];
61
		$response = $this->action('GET', '\Aimeos\Shop\Controller\[email protected]', $params, $getParams);
62
63
		$json = json_decode( $response->getContent(), true );
64
		$this->assertEquals( 'CNC', $json['data'][0]['attributes']['product.code'] );
65
66
		// add CNC product to basket
67
		$params = ['site' => 'unittest', 'resource' => 'basket', 'id' => 'default', 'related' => 'product'];
68
		$content = json_encode( ['data' => ['attributes' => ['product.id' => $json['data'][0]['id']]]] );
69
		$response = $this->action('POST', '\Aimeos\Shop\Controller\[email protected]', $params, [], [], [], [], $content);
70
71
		$json = json_decode( $response->getContent(), true );
72
		$this->assertEquals( 'CNC', $json['included'][0]['attributes']['order.base.product.prodcode'] );
73
74
		// change product quantity in basket
75
		$params = ['site' => 'unittest', 'resource' => 'basket', 'id' => 'default', 'related' => 'product', 'relatedid' => 0];
76
		$content = json_encode( ['data' => ['attributes' => ['quantity' => 2]]] );
77
		$response = $this->action('PATCH', '\Aimeos\Shop\Controller\[email protected]', $params, [], [], [], [], $content);
78
79
		$json = json_decode( $response->getContent(), true );
80
		$this->assertEquals( 2, $json['included'][0]['attributes']['order.base.product.quantity'] );
81
82
		// delete product from basket
83
		$params = ['site' => 'unittest', 'resource' => 'basket', 'id' => 'default', 'related' => 'product', 'relatedid' => 0];
84
		$response = $this->action('DELETE', '\Aimeos\Shop\Controller\[email protected]', $params);
85
86
		$json = json_decode( $response->getContent(), true );
87
		$this->assertEquals( 0, count( $json['included'] ) );
88
	}
89
90
91
	public function testPutAction()
92
	{
93
		View::addLocation(dirname(__DIR__).'/fixtures/views');
94
95
		$params = ['site' => 'unittest', 'resource' => 'basket'];
96
		$response = $this->action('PUT', '\Aimeos\Shop\Controller\[email protected]', $params);
97
98
		$json = json_decode( $response->getContent(), true );
99
		$this->assertArrayHasKey( 'errors', $json );
100
	}
101
}
102