Passed
Push — master ( 2d061e...22a5ca )
by Aimeos
03:38
created

JsonadmControllerTest::testOptionsAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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