Completed
Branch master (136162)
by Aimeos
06:57
created

JsonadmControllerTest::testOptionsAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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