1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017-2021 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
namespace Aimeos\Client\JsonApi\Basket\Address; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class StandardTest extends \PHPUnit\Framework\TestCase |
13
|
|
|
{ |
14
|
|
|
private $context; |
15
|
|
|
private $object; |
16
|
|
|
private $view; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
protected function setUp() : void |
20
|
|
|
{ |
21
|
|
|
$this->context = \TestHelperJapi::getContext(); |
22
|
|
|
$this->view = $this->context->view(); |
23
|
|
|
|
24
|
|
|
$this->object = new \Aimeos\Client\JsonApi\Basket\Address\Standard( $this->context, 'basket/address' ); |
25
|
|
|
$this->object->setView( $this->view ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
protected function tearDown() : void |
30
|
|
|
{ |
31
|
|
|
\Aimeos\Controller\Frontend\Basket\Factory::injectController( '\Aimeos\Controller\Frontend\Basket\Standard', null ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
public function testDelete() |
36
|
|
|
{ |
37
|
|
|
$body = '{"data": {"type": "basket/address", "id": "payment", "attributes": {"firstname": "test"}}}'; |
38
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
39
|
|
|
|
40
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
41
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
42
|
|
|
|
43
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/address']['data'] ) ); |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
$body = '{"data": {"type": "basket/address", "id": "payment"}}'; |
47
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
48
|
|
|
|
49
|
|
|
$response = $this->object->delete( $request, $this->view->response() ); |
50
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
51
|
|
|
|
52
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
53
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
54
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
55
|
|
|
|
56
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
57
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
58
|
|
|
$this->assertArrayNotHasKey( 'basket/address', $result['data']['relationships'] ); |
59
|
|
|
|
60
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
public function testDeleteById() |
65
|
|
|
{ |
66
|
|
|
$body = '{"data": {"type": "basket/address", "id": "payment", "attributes": {"firstname": "test"}}}'; |
67
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
68
|
|
|
|
69
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
70
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
71
|
|
|
|
72
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/address']['data'] ) ); |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
$params = array( 'id' => 'default', 'relatedid' => 'payment' ); |
76
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
77
|
|
|
$this->view->addHelper( 'param', $helper ); |
78
|
|
|
|
79
|
|
|
$response = $this->object->delete( $request, $this->view->response() ); |
80
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
81
|
|
|
|
82
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
83
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
84
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
85
|
|
|
|
86
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
87
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
88
|
|
|
$this->assertArrayNotHasKey( 'basket/address', $result['data']['relationships'] ); |
89
|
|
|
|
90
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
public function testDeletePluginException() |
95
|
|
|
{ |
96
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) ); |
97
|
|
|
|
98
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
99
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
$this->assertEquals( 409, $response->getStatusCode() ); |
103
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
public function testDeleteMShopException() |
108
|
|
|
{ |
109
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
110
|
|
|
|
111
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
112
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
116
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
public function testDeleteException() |
121
|
|
|
{ |
122
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Exception() ) ); |
123
|
|
|
|
124
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
125
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
129
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
public function testPost() |
134
|
|
|
{ |
135
|
|
|
$body = '{"data": {"type": "basket/address", "id": "payment", "attributes": {"firstname": "test"}}}'; |
136
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
137
|
|
|
|
138
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
139
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
143
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
144
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
145
|
|
|
|
146
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
147
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
148
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/address']['data'] ) ); |
149
|
|
|
$this->assertEquals( 'test', $result['included'][0]['attributes']['order.base.address.firstname'] ); |
150
|
|
|
|
151
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
public function testPostMultiple() |
156
|
|
|
{ |
157
|
|
|
$body = '{"data": [{ |
158
|
|
|
"type": "basket/address", "id": "payment", "attributes": {"firstname": "test"} |
159
|
|
|
}, { |
160
|
|
|
"type": "basket/address", "id": "delivery", "attributes": {"lastname": "test"} |
161
|
|
|
}]}'; |
162
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
163
|
|
|
|
164
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
165
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
169
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
170
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
171
|
|
|
|
172
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
173
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
174
|
|
|
$this->assertEquals( 2, count( $result['data']['relationships']['basket/address']['data'] ) ); |
175
|
|
|
$this->assertEquals( 'test', $result['included'][0]['attributes']['order.base.address.firstname'] ); |
176
|
|
|
$this->assertEquals( 'test', $result['included'][1]['attributes']['order.base.address.lastname'] ); |
177
|
|
|
|
178
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
public function testPostPluginException() |
183
|
|
|
{ |
184
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) ); |
185
|
|
|
|
186
|
|
|
$response = $object->post( $this->view->request(), $this->view->response() ); |
187
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
$this->assertEquals( 409, $response->getStatusCode() ); |
191
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
public function testPostMShopException() |
196
|
|
|
{ |
197
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
198
|
|
|
|
199
|
|
|
$response = $object->post( $this->view->request(), $this->view->response() ); |
200
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
204
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
public function testPostException() |
209
|
|
|
{ |
210
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Exception() ) ); |
211
|
|
|
|
212
|
|
|
$response = $object->post( $this->view->request(), $this->view->response() ); |
213
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
217
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
|
221
|
|
|
public function testOptions() |
222
|
|
|
{ |
223
|
|
|
$response = $this->object->options( $this->view->request(), $this->view->response() ); |
224
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
225
|
|
|
|
226
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
227
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
228
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
229
|
|
|
|
230
|
|
|
$this->assertEquals( null, $result['meta']['prefix'] ); |
231
|
|
|
$this->assertEquals( 21, count( $result['meta']['attributes'] ) ); |
232
|
|
|
$this->assertArrayNotHasKey( 'filter', $result['meta'] ); |
233
|
|
|
$this->assertArrayNotHasKey( 'sort', $result['meta'] ); |
234
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Returns a test object with a mocked basket controller |
240
|
|
|
* |
241
|
|
|
* @param string $method Basket controller method name to mock |
242
|
|
|
* @param mixed $result Return value of the mocked method |
243
|
|
|
*/ |
244
|
|
|
protected function object( $method, $result ) |
245
|
|
|
{ |
246
|
|
|
$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class ) |
247
|
|
|
->setConstructorArgs( [$this->context] ) |
248
|
|
|
->setMethods( [$method] ) |
249
|
|
|
->getMock(); |
250
|
|
|
|
251
|
|
|
$cntl->expects( $this->once() )->method( $method )->will( $result ); |
252
|
|
|
|
253
|
|
|
\Aimeos\Controller\Frontend\Basket\Factory::injectController( '\Aimeos\Controller\Frontend\Basket\Standard', $cntl ); |
254
|
|
|
|
255
|
|
|
$object = new \Aimeos\Client\JsonApi\Basket\Address\Standard( $this->context, 'basket/address' ); |
256
|
|
|
$object->setView( $this->view ); |
257
|
|
|
|
258
|
|
|
return $object; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|