JsonadmTest::testActionsBulk()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 75
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 51
nc 1
nop 0
dl 0
loc 75
rs 9.069
c 2
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class JsonadmTest extends \LocalWebTestCase
4
{
5
	public function testOptionsAction()
6
	{
7
		$response = $this->call( 'OPTIONS', '/unittest/admin/jsonadm/product' );
8
9
		$json = json_decode( (string) $response->getBody(), true );
10
11
		$this->assertNotNull( $json );
12
		$this->assertEquals( 200, $response->getStatusCode() );
13
		$this->assertArrayHasKey( 'resources', $json['meta'] );
14
		$this->assertGreaterThan( 1, count( $json['meta']['resources'] ) );
15
16
17
		$response = $this->call( 'OPTIONS', '/unittest/admin/jsonadm/' );
18
19
		$json = json_decode( (string) $response->getBody(), true );
20
21
		$this->assertNotNull( $json );
22
		$this->assertEquals( 200, $response->getStatusCode() );
23
		$this->assertArrayHasKey( 'resources', $json['meta'] );
24
		$this->assertGreaterThan( 1, count( $json['meta']['resources'] ) );
25
	}
26
27
28
	public function testActionsSingle()
29
	{
30
		$content = '{"data":{"type":"stock/type","attributes":{"stock.type.code":"slim","stock.type.label":"slim"}}}';
31
		$response = $this->call( 'POST', '/unittest/admin/jsonadm/stock/type', [], $content );
32
33
		$json = json_decode( (string) $response->getBody(), true );
34
35
		$this->assertNotNull( $json );
36
		$this->assertEquals( 201, $response->getStatusCode() );
37
		$this->assertArrayHasKey( 'stock.type.id', $json['data']['attributes'] );
38
		$this->assertEquals( 'slim', $json['data']['attributes']['stock.type.code'] );
39
		$this->assertEquals( 'slim', $json['data']['attributes']['stock.type.label'] );
40
		$this->assertEquals( 1, $json['meta']['total'] );
41
42
		$id = $json['data']['attributes']['stock.type.id'];
43
44
45
		$content = '{"data":{"type":"stock/type","attributes":{"stock.type.code":"slim2","stock.type.label":"slim2"}}}';
46
		$response = $this->call( 'PATCH', '/unittest/admin/jsonadm/stock/type/' . $id, [], $content );
47
48
		$json = json_decode( (string) $response->getBody(), true );
49
50
		$this->assertNotNull( $json );
51
		$this->assertEquals( 200, $response->getStatusCode() );
52
		$this->assertArrayHasKey( 'stock.type.id', $json['data']['attributes'] );
53
		$this->assertEquals( 'slim2', $json['data']['attributes']['stock.type.code'] );
54
		$this->assertEquals( 'slim2', $json['data']['attributes']['stock.type.label'] );
55
		$this->assertEquals( $id, $json['data']['attributes']['stock.type.id'] );
56
		$this->assertEquals( 1, $json['meta']['total'] );
57
58
59
		$response = $this->call( 'GET', '/unittest/admin/jsonadm/stock/type/' . $id );
60
61
		$json = json_decode( (string) $response->getBody(), true );
62
63
		$this->assertNotNull( $json );
64
		$this->assertEquals( 200, $response->getStatusCode() );
65
		$this->assertArrayHasKey( 'stock.type.id', $json['data']['attributes'] );
66
		$this->assertEquals( 'slim2', $json['data']['attributes']['stock.type.code'] );
67
		$this->assertEquals( 'slim2', $json['data']['attributes']['stock.type.label'] );
68
		$this->assertEquals( $id, $json['data']['attributes']['stock.type.id'] );
69
		$this->assertEquals( 1, $json['meta']['total'] );
70
71
72
		$response = $this->call( 'DELETE', '/unittest/admin/jsonadm/stock/type/' . $id );
73
74
		$json = json_decode( (string) $response->getBody(), true );
75
76
		$this->assertNotNull( $json );
77
		$this->assertEquals( 200, $response->getStatusCode() );
78
		$this->assertEquals( 1, $json['meta']['total'] );
79
	}
80
81
82
	public function testActionsBulk()
83
	{
84
		$content = '{"data":[
85
			{"type":"stock/type","attributes":{"stock.type.code":"slim","stock.type.label":"slim"}},
86
			{"type":"stock/type","attributes":{"stock.type.code":"slim2","stock.type.label":"slim"}}
87
		]}';
88
		$response = $this->call( 'POST', '/unittest/admin/jsonadm/stock/type', [], $content );
89
90
		$json = json_decode( (string) $response->getBody(), true );
91
92
		$this->assertNotNull( $json );
93
		$this->assertEquals( 201, $response->getStatusCode() );
94
		$this->assertEquals( 2, count( $json['data'] ) );
95
		$this->assertArrayHasKey( 'stock.type.id', $json['data'][0]['attributes'] );
96
		$this->assertArrayHasKey( 'stock.type.id', $json['data'][1]['attributes'] );
97
		$this->assertEquals( 'slim', $json['data'][0]['attributes']['stock.type.label'] );
98
		$this->assertEquals( 'slim', $json['data'][1]['attributes']['stock.type.label'] );
99
		$this->assertEquals( 2, $json['meta']['total'] );
100
101
		$ids = array( $json['data'][0]['attributes']['stock.type.id'], $json['data'][1]['attributes']['stock.type.id'] );
102
103
104
		$content = '{"data":[
105
			{"type":"stock/type","id":' . $ids[0] . ',"attributes":{"stock.type.label":"slim2"}},
106
			{"type":"stock/type","id":' . $ids[1] . ',"attributes":{"stock.type.label":"slim2"}}
107
		]}';
108
		$response = $this->call( 'PATCH', '/unittest/admin/jsonadm/stock/type', [], $content );
109
110
		$json = json_decode( (string) $response->getBody(), true );
111
112
		$this->assertNotNull( $json );
113
		$this->assertEquals( 200, $response->getStatusCode() );
114
		$this->assertEquals( 2, count( $json['data'] ) );
115
		$this->assertArrayHasKey( 'stock.type.id', $json['data'][0]['attributes'] );
116
		$this->assertArrayHasKey( 'stock.type.id', $json['data'][1]['attributes'] );
117
		$this->assertEquals( 'slim2', $json['data'][0]['attributes']['stock.type.label'] );
118
		$this->assertEquals( 'slim2', $json['data'][1]['attributes']['stock.type.label'] );
119
		$this->assertTrue( in_array( $json['data'][0]['attributes']['stock.type.id'], $ids ) );
120
		$this->assertTrue( in_array( $json['data'][1]['attributes']['stock.type.id'], $ids ) );
121
		$this->assertEquals( 2, $json['meta']['total'] );
122
123
124
		$getParams = ['filter' => ['&&' => [
125
			['=~' => ['stock.type.code' => 'slim']],
126
			['==' => ['stock.type.label' => 'slim2']]
127
			]],
128
			'sort' => 'stock.type.code', 'page' => ['offset' => 0, 'limit' => 3]
129
		];
130
		$response = $this->call( 'GET', '/unittest/admin/jsonadm/stock/type', $getParams );
131
132
		$json = json_decode( (string) $response->getBody(), true );
133
134
		$this->assertNotNull( $json );
135
		$this->assertEquals( 200, $response->getStatusCode() );
136
		$this->assertEquals( 2, count( $json['data'] ) );
137
		$this->assertEquals( 'slim', $json['data'][0]['attributes']['stock.type.code'] );
138
		$this->assertEquals( 'slim2', $json['data'][1]['attributes']['stock.type.code'] );
139
		$this->assertEquals( 'slim2', $json['data'][0]['attributes']['stock.type.label'] );
140
		$this->assertEquals( 'slim2', $json['data'][1]['attributes']['stock.type.label'] );
141
		$this->assertTrue( in_array( $json['data'][0]['attributes']['stock.type.id'], $ids ) );
142
		$this->assertTrue( in_array( $json['data'][1]['attributes']['stock.type.id'], $ids ) );
143
		$this->assertEquals( 2, $json['meta']['total'] );
144
145
146
		$content = '{"data":[
147
			{"type":"stock/type","id":' . $ids[0] . '},
148
			{"type":"stock/type","id":' . $ids[1] . '}
149
		]}';
150
		$response = $this->call( 'DELETE', '/unittest/admin/jsonadm/stock/type', [], $content );
151
152
		$json = json_decode( (string) $response->getBody(), true );
153
154
		$this->assertNotNull( $json );
155
		$this->assertEquals( 200, $response->getStatusCode() );
156
		$this->assertEquals( 2, $json['meta']['total'] );
157
	}
158
159
160
	public function testPutAction()
161
	{
162
		$content = '{"data":[
163
			{"type":"stock/type","attributes":{"stock.type.code":"slim","stock.type.label":"slim"}},
164
			{"type":"stock/type","attributes":{"stock.type.code":"slim2","stock.type.label":"slim"}}
165
		]}';
166
		$response = $this->call( 'PUT', '/unittest/admin/jsonadm/stock/type', [], $content );
167
168
		$json = json_decode( (string) $response->getBody(), true );
169
170
		$this->assertEquals( 501, $response->getStatusCode() );
171
		$this->assertNotNull( $json );
172
	}
173
}
174