JsonadmControllerTest::testActionsBulk()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 82
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 57
dl 0
loc 82
rs 8.9381
c 1
b 0
f 0
cc 1
nc 1
nop 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 JsonadmControllerTest extends AimeosTestAbstract
4
{
5
	public function testOptionsActionSite()
6
	{
7
		View::addLocation( dirname( __DIR__ ) . '/fixtures/views' );
8
		$this->app['session']->setPreviousUrl( 'http://localhost/invalid' );
9
10
		$params = ['site' => 'invalid', 'resource' => 'product'];
11
		$response = $this->action( 'OPTIONS', '\Aimeos\Shop\Controller\JsonadmController@optionsAction', $params );
12
13
		$json = json_decode( $response->getContent(), true );
14
15
		$this->assertResponseOk();
16
		$this->assertNotNull( $json );
17
	}
18
19
20
	public function testOptionsAction()
21
	{
22
		View::addLocation( dirname( __DIR__ ) . '/fixtures/views' );
23
		$this->app['session']->setPreviousUrl( 'http://localhost/unittest' );
24
25
		$params = ['site' => 'unittest', 'resource' => 'product'];
26
		$response = $this->action( 'OPTIONS', '\Aimeos\Shop\Controller\JsonadmController@optionsAction', $params );
27
28
		$json = json_decode( $response->getContent(), true );
29
30
		$this->assertNotNull( $json );
31
		$this->assertEquals( 200, $response->getStatusCode() );
32
		$this->assertArrayHasKey( 'resources', $json['meta'] );
33
		$this->assertGreaterThan( 1, count( $json['meta']['resources'] ) );
34
35
36
		$params = ['site' => 'unittest'];
37
		$response = $this->action( 'OPTIONS', '\Aimeos\Shop\Controller\JsonadmController@optionsAction', $params );
38
39
		$json = json_decode( $response->getContent(), true );
40
41
		$this->assertNotNull( $json );
42
		$this->assertEquals( 200, $response->getStatusCode() );
43
		$this->assertArrayHasKey( 'resources', $json['meta'] );
44
		$this->assertGreaterThan( 1, count( $json['meta']['resources'] ) );
45
	}
46
47
48
	public function testActionsSingle()
49
	{
50
		View::addLocation( dirname( __DIR__ ) . '/fixtures/views' );
51
		$this->app['session']->setPreviousUrl( 'http://localhost/unittest' );
52
53
		$params = ['site' => 'unittest', 'resource' => 'stock/type'];
54
		$content = '{"data":{"type":"stock/type","attributes":{"stock.type.code":"laravel","stock.type.label":"laravel"}}}';
55
		$response = $this->action( 'POST', '\Aimeos\Shop\Controller\JsonadmController@postAction', $params, [], [], [], [], $content );
56
57
		$json = json_decode( $response->getContent(), true );
58
59
		$this->assertEquals( 201, $response->getStatusCode() );
60
		$this->assertNotNull( $json );
61
		$this->assertArrayHasKey( 'stock.type.id', $json['data']['attributes'] );
62
		$this->assertEquals( 'laravel', $json['data']['attributes']['stock.type.code'] );
63
		$this->assertEquals( 'laravel', $json['data']['attributes']['stock.type.label'] );
64
		$this->assertEquals( 1, $json['meta']['total'] );
65
66
		$id = $json['data']['attributes']['stock.type.id'];
67
68
69
		$params = ['site' => 'unittest', 'resource' => 'stock/type', 'id' => $id];
70
		$content = '{"data":{"type":"stock/type","attributes":{"stock.type.code":"laravel2","stock.type.label":"laravel2"}}}';
71
		$response = $this->action( 'PATCH', '\Aimeos\Shop\Controller\JsonadmController@patchAction', $params, [], [], [], [], $content );
72
73
		$json = json_decode( $response->getContent(), true );
74
75
		$this->assertResponseOk();
76
		$this->assertNotNull( $json );
77
		$this->assertArrayHasKey( 'stock.type.id', $json['data']['attributes'] );
78
		$this->assertEquals( 'laravel2', $json['data']['attributes']['stock.type.code'] );
79
		$this->assertEquals( 'laravel2', $json['data']['attributes']['stock.type.label'] );
80
		$this->assertEquals( $id, $json['data']['attributes']['stock.type.id'] );
81
		$this->assertEquals( 1, $json['meta']['total'] );
82
83
84
		$params = ['site' => 'unittest', 'resource' => 'stock/type', 'id' => $id];
85
		$response = $this->action( 'GET', '\Aimeos\Shop\Controller\JsonadmController@getAction', $params );
86
87
		$json = json_decode( $response->getContent(), true );
88
89
		$this->assertResponseOk();
90
		$this->assertNotNull( $json );
91
		$this->assertArrayHasKey( 'stock.type.id', $json['data']['attributes'] );
92
		$this->assertEquals( 'laravel2', $json['data']['attributes']['stock.type.code'] );
93
		$this->assertEquals( 'laravel2', $json['data']['attributes']['stock.type.label'] );
94
		$this->assertEquals( $id, $json['data']['attributes']['stock.type.id'] );
95
		$this->assertEquals( 1, $json['meta']['total'] );
96
97
98
		$params = ['site' => 'unittest', 'resource' => 'stock/type', 'id' => $id];
99
		$response = $this->action( 'DELETE', '\Aimeos\Shop\Controller\JsonadmController@deleteAction', $params );
100
101
		$json = json_decode( $response->getContent(), true );
102
103
		$this->assertResponseOk();
104
		$this->assertNotNull( $json );
105
		$this->assertEquals( 1, $json['meta']['total'] );
106
	}
107
108
109
	public function testActionsBulk()
110
	{
111
		View::addLocation( dirname( __DIR__ ) . '/fixtures/views' );
112
		$this->app['session']->setPreviousUrl( 'http://localhost/unittest' );
113
114
		$params = ['site' => 'unittest', 'resource' => 'stock/type'];
115
		$content = '{"data":[
116
			{"type":"stock/type","attributes":{"stock.type.code":"laravel","stock.type.label":"laravel"}},
117
			{"type":"stock/type","attributes":{"stock.type.code":"laravel2","stock.type.label":"laravel"}}
118
		]}';
119
		$response = $this->action( 'POST', '\Aimeos\Shop\Controller\JsonadmController@postAction', $params, [], [], [], [], $content );
120
121
		$json = json_decode( $response->getContent(), true );
122
123
		$this->assertEquals( 201, $response->getStatusCode() );
124
		$this->assertNotNull( $json );
125
		$this->assertEquals( 2, count( $json['data'] ) );
126
		$this->assertArrayHasKey( 'stock.type.id', $json['data'][0]['attributes'] );
127
		$this->assertArrayHasKey( 'stock.type.id', $json['data'][1]['attributes'] );
128
		$this->assertEquals( 'laravel', $json['data'][0]['attributes']['stock.type.label'] );
129
		$this->assertEquals( 'laravel', $json['data'][1]['attributes']['stock.type.label'] );
130
		$this->assertEquals( 2, $json['meta']['total'] );
131
132
		$ids = array( $json['data'][0]['attributes']['stock.type.id'], $json['data'][1]['attributes']['stock.type.id'] );
133
134
135
		$params = ['site' => 'unittest', 'resource' => 'stock/type'];
136
		$content = '{"data":[
137
			{"type":"stock/type","id":' . $ids[0] . ',"attributes":{"stock.type.label":"laravel2"}},
138
			{"type":"stock/type","id":' . $ids[1] . ',"attributes":{"stock.type.label":"laravel2"}}
139
		]}';
140
		$response = $this->action( 'PATCH', '\Aimeos\Shop\Controller\JsonadmController@patchAction', $params, [], [], [], [], $content );
141
142
		$json = json_decode( $response->getContent(), true );
143
144
		$this->assertResponseOk();
145
		$this->assertNotNull( $json );
146
		$this->assertEquals( 2, count( $json['data'] ) );
147
		$this->assertArrayHasKey( 'stock.type.id', $json['data'][0]['attributes'] );
148
		$this->assertArrayHasKey( 'stock.type.id', $json['data'][1]['attributes'] );
149
		$this->assertEquals( 'laravel2', $json['data'][0]['attributes']['stock.type.label'] );
150
		$this->assertEquals( 'laravel2', $json['data'][1]['attributes']['stock.type.label'] );
151
		$this->assertTrue( in_array( $json['data'][0]['attributes']['stock.type.id'], $ids ) );
152
		$this->assertTrue( in_array( $json['data'][1]['attributes']['stock.type.id'], $ids ) );
153
		$this->assertEquals( 2, $json['meta']['total'] );
154
155
156
		$params = ['site' => 'unittest', 'resource' => 'stock/type'];
157
		$getParams = ['filter' => ['&&' => [
158
			['=~' => ['stock.type.code' => 'laravel']],
159
			['==' => ['stock.type.label' => 'laravel2']]
160
			]],
161
			'sort' => 'stock.type.code', 'page' => ['offset' => 0, 'limit' => 3]
162
		];
163
		$response = $this->action( 'GET', '\Aimeos\Shop\Controller\JsonadmController@getAction', $params, $getParams );
164
165
		$json = json_decode( $response->getContent(), true );
166
167
		$this->assertResponseOk();
168
		$this->assertNotNull( $json );
169
		$this->assertEquals( 2, count( $json['data'] ) );
170
		$this->assertEquals( 'laravel', $json['data'][0]['attributes']['stock.type.code'] );
171
		$this->assertEquals( 'laravel2', $json['data'][1]['attributes']['stock.type.code'] );
172
		$this->assertEquals( 'laravel2', $json['data'][0]['attributes']['stock.type.label'] );
173
		$this->assertEquals( 'laravel2', $json['data'][1]['attributes']['stock.type.label'] );
174
		$this->assertTrue( in_array( $json['data'][0]['attributes']['stock.type.id'], $ids ) );
175
		$this->assertTrue( in_array( $json['data'][1]['attributes']['stock.type.id'], $ids ) );
176
		$this->assertEquals( 2, $json['meta']['total'] );
177
178
179
		$params = ['site' => 'unittest', 'resource' => 'stock/type'];
180
		$content = '{"data":[
181
			{"type":"stock/type","id":' . $ids[0] . '},
182
			{"type":"stock/type","id":' . $ids[1] . '}
183
		]}';
184
		$response = $this->action( 'DELETE', '\Aimeos\Shop\Controller\JsonadmController@deleteAction', $params, [], [], [], [], $content );
185
186
		$json = json_decode( $response->getContent(), true );
187
188
		$this->assertResponseOk();
189
		$this->assertNotNull( $json );
190
		$this->assertEquals( 2, $json['meta']['total'] );
191
	}
192
193
194
	public function testPutAction()
195
	{
196
		View::addLocation( dirname( __DIR__ ) . '/fixtures/views' );
197
		$this->app['session']->setPreviousUrl( 'http://localhost/unittest' );
198
199
		$params = ['site' => 'unittest', 'resource' => 'stock/type'];
200
		$content = '{"data":[
201
			{"type":"stock/type","attributes":{"stock.type.code":"laravel","stock.type.label":"laravel"}},
202
			{"type":"stock/type","attributes":{"stock.type.code":"laravel2","stock.type.label":"laravel"}}
203
		]}';
204
		$response = $this->action( 'PUT', '\Aimeos\Shop\Controller\JsonadmController@postAction', $params, [], [], [], [], $content );
205
206
		$json = json_decode( $response->getContent(), true );
207
208
		$this->assertEquals( 501, $response->getStatusCode() );
209
		$this->assertNotNull( $json );
210
	}
211
}
212