JsonadmControllerTest::testActionsBulk()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 89
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 61
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 89
rs 8.8509

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
namespace Aimeos\ShopBundle\Tests\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6
7
8
class JsonadmControllerTest extends WebTestCase
9
{
10
	public function testOptionsAction()
11
	{
12
		$client = static::createClient( array(), array(
13
			'PHP_AUTH_USER' => 'admin',
14
			'PHP_AUTH_PW'   => 'adminpass',
15
		) );
16
17
18
		$client->request( 'OPTIONS', '/unittest/jsonadm/product' );
19
		$response = $client->getResponse();
20
		$json = json_decode( $response->getContent(), true );
21
22
		$this->assertNotNull( $json );
23
		$this->assertEquals( 200, $response->getStatusCode() );
24
		$this->assertArrayHasKey( 'resources', $json['meta'] );
25
		$this->assertGreaterThan( 1, count( $json['meta']['resources'] ) );
26
27
28
		$client->request( 'OPTIONS', '/unittest/jsonadm' );
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->assertArrayHasKey( 'resources', $json['meta'] );
36
		$this->assertGreaterThan( 1, count( $json['meta']['resources'] ) );
37
	}
38
39
40
	public function testActionsSingle()
41
	{
42
		$client = static::createClient( array(), array(
43
			'PHP_AUTH_USER' => 'admin',
44
			'PHP_AUTH_PW'   => 'adminpass',
45
		) );
46
47
		$client->request( 'OPTIONS', '/unittest/jsonadm' );
48
		$json = json_decode( $client->getResponse()->getContent(), true );
49
		$token = $json['meta']['csrf']['value'];
50
51
		$content = '{"data":{"type":"stock/type","attributes":{"stock.type.code":"symfony","stock.type.label":"symfony"}}}';
52
		$client->request( 'POST', '/unittest/jsonadm/stock/type', ['_token' => $token], [], [], $content );
53
		$response = $client->getResponse();
54
55
		$json = json_decode( $response->getContent(), true );
56
57
		$this->assertNotNull( $json );
58
		$this->assertEquals( 201, $response->getStatusCode() );
59
		$this->assertArrayHasKey( 'stock.type.id', $json['data']['attributes'] );
60
		$this->assertEquals( 'symfony', $json['data']['attributes']['stock.type.code'] );
61
		$this->assertEquals( 'symfony', $json['data']['attributes']['stock.type.label'] );
62
		$this->assertEquals( 1, $json['meta']['total'] );
63
64
		$id = $json['data']['attributes']['stock.type.id'];
65
66
67
		$content = '{"data":{"type":"stock/type","attributes":{"stock.type.code":"symfony2","stock.type.label":"symfony2"}}}';
68
		$client->request( 'PATCH', '/unittest/jsonadm/stock/type/' . $id, ['_token' => $token], [], [], $content );
69
		$response = $client->getResponse();
70
71
		$json = json_decode( $response->getContent(), true );
72
73
		$this->assertNotNull( $json );
74
		$this->assertEquals( 200, $response->getStatusCode() );
75
		$this->assertArrayHasKey( 'stock.type.id', $json['data']['attributes'] );
76
		$this->assertEquals( 'symfony2', $json['data']['attributes']['stock.type.code'] );
77
		$this->assertEquals( 'symfony2', $json['data']['attributes']['stock.type.label'] );
78
		$this->assertEquals( $id, $json['data']['attributes']['stock.type.id'] );
79
		$this->assertEquals( 1, $json['meta']['total'] );
80
81
82
		$client->request( 'GET', '/unittest/jsonadm/stock/type/' . $id );
83
		$response = $client->getResponse();
84
85
		$json = json_decode( $response->getContent(), true );
86
87
		$this->assertNotNull( $json );
88
		$this->assertEquals( 200, $response->getStatusCode() );
89
		$this->assertArrayHasKey( 'stock.type.id', $json['data']['attributes'] );
90
		$this->assertEquals( 'symfony2', $json['data']['attributes']['stock.type.code'] );
91
		$this->assertEquals( 'symfony2', $json['data']['attributes']['stock.type.label'] );
92
		$this->assertEquals( $id, $json['data']['attributes']['stock.type.id'] );
93
		$this->assertEquals( 1, $json['meta']['total'] );
94
95
96
		$client->request( 'DELETE', '/unittest/jsonadm/stock/type/' . $id, ['_token' => $token], [], [] );
97
		$response = $client->getResponse();
98
99
		$json = json_decode( $response->getContent(), true );
100
101
		$this->assertNotNull( $json );
102
		$this->assertEquals( 200, $response->getStatusCode() );
103
		$this->assertEquals( 1, $json['meta']['total'] );
104
	}
105
106
107
	public function testActionsBulk()
108
	{
109
		$client = static::createClient( array(), array(
110
			'PHP_AUTH_USER' => 'admin',
111
			'PHP_AUTH_PW'   => 'adminpass',
112
		) );
113
114
		$client->request( 'OPTIONS', '/unittest/jsonadm' );
115
		$json = json_decode( $client->getResponse()->getContent(), true );
116
		$token = $json['meta']['csrf']['value'];
117
118
119
		$content = '{"data":[
120
			{"type":"stock/type","attributes":{"stock.type.code":"symfony","stock.type.label":"symfony"}},
121
			{"type":"stock/type","attributes":{"stock.type.code":"symfony2","stock.type.label":"symfony"}}
122
		]}';
123
		$client->request( 'POST', '/unittest/jsonadm/stock/type', ['_token' => $token], [], [], $content );
124
		$response = $client->getResponse();
125
126
		$json = json_decode( $response->getContent(), true );
127
128
		$this->assertNotNull( $json );
129
		$this->assertEquals( 201, $response->getStatusCode() );
130
		$this->assertEquals( 2, count( $json['data'] ) );
131
		$this->assertArrayHasKey( 'stock.type.id', $json['data'][0]['attributes'] );
132
		$this->assertArrayHasKey( 'stock.type.id', $json['data'][1]['attributes'] );
133
		$this->assertEquals( 'symfony', $json['data'][0]['attributes']['stock.type.label'] );
134
		$this->assertEquals( 'symfony', $json['data'][1]['attributes']['stock.type.label'] );
135
		$this->assertEquals( 2, $json['meta']['total'] );
136
137
		$ids = array( $json['data'][0]['attributes']['stock.type.id'], $json['data'][1]['attributes']['stock.type.id'] );
138
139
140
		$content = '{"data":[
141
			{"type":"stock/type","id":' . $ids[0] . ',"attributes":{"stock.type.label":"symfony2"}},
142
			{"type":"stock/type","id":' . $ids[1] . ',"attributes":{"stock.type.label":"symfony2"}}
143
		]}';
144
		$client->request( 'PATCH', '/unittest/jsonadm/stock/type', ['_token' => $token], [], [], $content );
145
		$response = $client->getResponse();
146
147
		$json = json_decode( $response->getContent(), true );
148
149
		$this->assertNotNull( $json );
150
		$this->assertEquals( 200, $response->getStatusCode() );
151
		$this->assertEquals( 2, count( $json['data'] ) );
152
		$this->assertArrayHasKey( 'stock.type.id', $json['data'][0]['attributes'] );
153
		$this->assertArrayHasKey( 'stock.type.id', $json['data'][1]['attributes'] );
154
		$this->assertEquals( 'symfony2', $json['data'][0]['attributes']['stock.type.label'] );
155
		$this->assertEquals( 'symfony2', $json['data'][1]['attributes']['stock.type.label'] );
156
		$this->assertTrue( in_array( $json['data'][0]['attributes']['stock.type.id'], $ids ) );
157
		$this->assertTrue( in_array( $json['data'][1]['attributes']['stock.type.id'], $ids ) );
158
		$this->assertEquals( 2, $json['meta']['total'] );
159
160
161
		$getParams = array( 'filter' => array( '&&' => array(
162
			array( '=~' => array( 'stock.type.code' => 'symfony' ) ),
163
			array( '==' => array( 'stock.type.label' => 'symfony2' ) )
164
			) ),
165
			'sort' => 'stock.type.code', 'page' => array( 'offset' => 0, 'limit' => 3 )
166
		);
167
		$client->request( 'GET', '/unittest/jsonadm/stock/type', $getParams );
168
		$response = $client->getResponse();
169
170
		$json = json_decode( $response->getContent(), true );
171
172
		$this->assertNotNull( $json );
173
		$this->assertEquals( 200, $response->getStatusCode() );
174
		$this->assertEquals( 2, count( $json['data'] ) );
175
		$this->assertEquals( 'symfony', $json['data'][0]['attributes']['stock.type.code'] );
176
		$this->assertEquals( 'symfony2', $json['data'][1]['attributes']['stock.type.code'] );
177
		$this->assertEquals( 'symfony2', $json['data'][0]['attributes']['stock.type.label'] );
178
		$this->assertEquals( 'symfony2', $json['data'][1]['attributes']['stock.type.label'] );
179
		$this->assertTrue( in_array( $json['data'][0]['attributes']['stock.type.id'], $ids ) );
180
		$this->assertTrue( in_array( $json['data'][1]['attributes']['stock.type.id'], $ids ) );
181
		$this->assertEquals( 2, $json['meta']['total'] );
182
183
184
		$content = '{"data":[
185
			{"type":"stock/type","id":' . $ids[0] . '},
186
			{"type":"stock/type","id":' . $ids[1] . '}
187
		]}';
188
		$client->request( 'DELETE', '/unittest/jsonadm/stock/type', ['_token' => $token], [], [], $content );
189
		$response = $client->getResponse();
190
191
		$json = json_decode( $response->getContent(), true );
192
193
		$this->assertNotNull( $json );
194
		$this->assertEquals( 200, $response->getStatusCode() );
195
		$this->assertEquals( 2, $json['meta']['total'] );
196
	}
197
}
198