Passed
Push — master ( 48d2af...a4afe2 )
by Aimeos
11:39
created

JsonadmControllerTest::testActionsBulk()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 85
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 58
nc 1
nop 0
dl 0
loc 85
rs 8.9163
c 3
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
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
21
		$json = json_decode( $response->getContent(), true );
22
23
		$this->assertNotNull( $json );
24
		$this->assertEquals( 200, $response->getStatusCode() );
25
		$this->assertArrayHasKey( 'resources', $json['meta'] );
26
		$this->assertGreaterThan( 1, count( $json['meta']['resources'] ) );
27
28
29
		$client->request( 'OPTIONS', '/unittest/jsonadm' );
30
		$response = $client->getResponse();
31
32
		$json = json_decode( $response->getContent(), true );
33
34
		$this->assertNotNull( $json );
35
		$this->assertEquals( 200, $response->getStatusCode() );
36
		$this->assertArrayHasKey( 'resources', $json['meta'] );
37
		$this->assertGreaterThan( 1, count( $json['meta']['resources'] ) );
38
	}
39
40
41
	public function testActionsSingle()
42
	{
43
		$client = static::createClient( array(), array(
44
			'PHP_AUTH_USER' => 'admin',
45
			'PHP_AUTH_PW'   => 'adminpass',
46
		) );
47
48
49
		$content = '{"data":{"type":"stock/type","attributes":{"stock.type.code":"symfony","stock.type.label":"symfony"}}}';
50
		$client->request( 'POST', '/unittest/jsonadm/stock/type', array(), array(), array(), $content );
51
		$response = $client->getResponse();
52
53
		$json = json_decode( $response->getContent(), true );
54
55
		$this->assertNotNull( $json );
56
		$this->assertEquals( 201, $response->getStatusCode() );
57
		$this->assertArrayHasKey( 'stock.type.id', $json['data']['attributes'] );
58
		$this->assertEquals( 'symfony', $json['data']['attributes']['stock.type.code'] );
59
		$this->assertEquals( 'symfony', $json['data']['attributes']['stock.type.label'] );
60
		$this->assertEquals( 1, $json['meta']['total'] );
61
62
		$id = $json['data']['attributes']['stock.type.id'];
63
64
65
		$content = '{"data":{"type":"stock/type","attributes":{"stock.type.code":"symfony2","stock.type.label":"symfony2"}}}';
66
		$client->request( 'PATCH', '/unittest/jsonadm/stock/type/' . $id, array(), array(), array(), $content );
67
		$response = $client->getResponse();
68
69
		$json = json_decode( $response->getContent(), true );
70
71
		$this->assertNotNull( $json );
72
		$this->assertEquals( 200, $response->getStatusCode() );
73
		$this->assertArrayHasKey( 'stock.type.id', $json['data']['attributes'] );
74
		$this->assertEquals( 'symfony2', $json['data']['attributes']['stock.type.code'] );
75
		$this->assertEquals( 'symfony2', $json['data']['attributes']['stock.type.label'] );
76
		$this->assertEquals( $id, $json['data']['attributes']['stock.type.id'] );
77
		$this->assertEquals( 1, $json['meta']['total'] );
78
79
80
		$client->request( 'GET', '/unittest/jsonadm/stock/type/' . $id );
81
		$response = $client->getResponse();
82
83
		$json = json_decode( $response->getContent(), true );
84
85
		$this->assertNotNull( $json );
86
		$this->assertEquals( 200, $response->getStatusCode() );
87
		$this->assertArrayHasKey( 'stock.type.id', $json['data']['attributes'] );
88
		$this->assertEquals( 'symfony2', $json['data']['attributes']['stock.type.code'] );
89
		$this->assertEquals( 'symfony2', $json['data']['attributes']['stock.type.label'] );
90
		$this->assertEquals( $id, $json['data']['attributes']['stock.type.id'] );
91
		$this->assertEquals( 1, $json['meta']['total'] );
92
93
94
		$client->request( 'DELETE', '/unittest/jsonadm/stock/type/' . $id );
95
		$response = $client->getResponse();
96
97
		$json = json_decode( $response->getContent(), true );
98
99
		$this->assertNotNull( $json );
100
		$this->assertEquals( 200, $response->getStatusCode() );
101
		$this->assertEquals( 1, $json['meta']['total'] );
102
	}
103
104
105
	public function testActionsBulk()
106
	{
107
		$client = static::createClient( array(), array(
108
			'PHP_AUTH_USER' => 'admin',
109
			'PHP_AUTH_PW'   => 'adminpass',
110
		) );
111
112
113
		$content = '{"data":[
114
			{"type":"stock/type","attributes":{"stock.type.code":"symfony","stock.type.label":"symfony"}},
115
			{"type":"stock/type","attributes":{"stock.type.code":"symfony2","stock.type.label":"symfony"}}
116
		]}';
117
		$client->request( 'POST', '/unittest/jsonadm/stock/type', array(), array(), array(), $content );
118
		$response = $client->getResponse();
119
120
		$json = json_decode( $response->getContent(), true );
121
122
		$this->assertNotNull( $json );
123
		$this->assertEquals( 201, $response->getStatusCode() );
124
		$this->assertEquals( 2, count( $json['data'] ) );
125
		$this->assertArrayHasKey( 'stock.type.id', $json['data'][0]['attributes'] );
126
		$this->assertArrayHasKey( 'stock.type.id', $json['data'][1]['attributes'] );
127
		$this->assertEquals( 'symfony', $json['data'][0]['attributes']['stock.type.label'] );
128
		$this->assertEquals( 'symfony', $json['data'][1]['attributes']['stock.type.label'] );
129
		$this->assertEquals( 2, $json['meta']['total'] );
130
131
		$ids = array( $json['data'][0]['attributes']['stock.type.id'], $json['data'][1]['attributes']['stock.type.id'] );
132
133
134
		$content = '{"data":[
135
			{"type":"stock/type","id":' . $ids[0] . ',"attributes":{"stock.type.label":"symfony2"}},
136
			{"type":"stock/type","id":' . $ids[1] . ',"attributes":{"stock.type.label":"symfony2"}}
137
		]}';
138
		$client->request( 'PATCH', '/unittest/jsonadm/stock/type', array(), array(), array(), $content );
139
		$response = $client->getResponse();
140
141
		$json = json_decode( $response->getContent(), true );
142
143
		$this->assertNotNull( $json );
144
		$this->assertEquals( 200, $response->getStatusCode() );
145
		$this->assertEquals( 2, count( $json['data'] ) );
146
		$this->assertArrayHasKey( 'stock.type.id', $json['data'][0]['attributes'] );
147
		$this->assertArrayHasKey( 'stock.type.id', $json['data'][1]['attributes'] );
148
		$this->assertEquals( 'symfony2', $json['data'][0]['attributes']['stock.type.label'] );
149
		$this->assertEquals( 'symfony2', $json['data'][1]['attributes']['stock.type.label'] );
150
		$this->assertTrue( in_array( $json['data'][0]['attributes']['stock.type.id'], $ids ) );
151
		$this->assertTrue( in_array( $json['data'][1]['attributes']['stock.type.id'], $ids ) );
152
		$this->assertEquals( 2, $json['meta']['total'] );
153
154
155
		$getParams = array( 'filter' => array( '&&' => array(
156
			array( '=~' => array( 'stock.type.code' => 'symfony' ) ),
157
			array( '==' => array( 'stock.type.label' => 'symfony2' ) )
158
			) ),
159
			'sort' => 'stock.type.code', 'page' => array( 'offset' => 0, 'limit' => 3 )
160
		);
161
		$client->request( 'GET', '/unittest/jsonadm/stock/type', $getParams );
162
		$response = $client->getResponse();
163
164
		$json = json_decode( $response->getContent(), true );
165
166
		$this->assertNotNull( $json );
167
		$this->assertEquals( 200, $response->getStatusCode() );
168
		$this->assertEquals( 2, count( $json['data'] ) );
169
		$this->assertEquals( 'symfony', $json['data'][0]['attributes']['stock.type.code'] );
170
		$this->assertEquals( 'symfony2', $json['data'][1]['attributes']['stock.type.code'] );
171
		$this->assertEquals( 'symfony2', $json['data'][0]['attributes']['stock.type.label'] );
172
		$this->assertEquals( 'symfony2', $json['data'][1]['attributes']['stock.type.label'] );
173
		$this->assertTrue( in_array( $json['data'][0]['attributes']['stock.type.id'], $ids ) );
174
		$this->assertTrue( in_array( $json['data'][1]['attributes']['stock.type.id'], $ids ) );
175
		$this->assertEquals( 2, $json['meta']['total'] );
176
177
178
		$content = '{"data":[
179
			{"type":"stock/type","id":' . $ids[0] . '},
180
			{"type":"stock/type","id":' . $ids[1] . '}
181
		]}';
182
		$client->request( 'DELETE', '/unittest/jsonadm/stock/type', array(), array(), array(), $content );
183
		$response = $client->getResponse();
184
185
		$json = json_decode( $response->getContent(), true );
186
187
		$this->assertNotNull( $json );
188
		$this->assertEquals( 200, $response->getStatusCode() );
189
		$this->assertEquals( 2, $json['meta']['total'] );
190
	}
191
}
192