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\Coupon; |
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\Coupon\Standard( $this->context, 'basket/coupon' ); |
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
|
|
|
$this->addProduct( 'CNC' ); |
38
|
|
|
|
39
|
|
|
$body = '{"data": {"type": "basket/coupon", "id": "GHIJ"}}'; |
40
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
|
|
|
|
41
|
|
|
|
42
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
43
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
44
|
|
|
|
45
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/coupon']['data'] ) ); |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
$body = '{"data": {"type": "basket/coupon", "id": "GHIJ"}}'; |
49
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
50
|
|
|
|
51
|
|
|
$response = $this->object->delete( $request, $this->view->response() ); |
52
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
53
|
|
|
|
54
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
55
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
56
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
57
|
|
|
|
58
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
59
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
60
|
|
|
$this->assertArrayNotHasKey( 'basket/coupon', $result['data']['relationships'] ); |
61
|
|
|
|
62
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
public function testDeleteById() |
67
|
|
|
{ |
68
|
|
|
$this->addProduct( 'CNC' ); |
69
|
|
|
|
70
|
|
|
$body = '{"data": {"type": "basket/coupon", "id": "GHIJ"}}'; |
71
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
72
|
|
|
|
73
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
74
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
75
|
|
|
|
76
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/coupon']['data'] ) ); |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
$params = array( 'id' => 'default', 'relatedid' => 'GHIJ' ); |
80
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
81
|
|
|
$this->view->addHelper( 'param', $helper ); |
82
|
|
|
|
83
|
|
|
$response = $this->object->delete( $request, $this->view->response() ); |
84
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
85
|
|
|
|
86
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
87
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
88
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
89
|
|
|
|
90
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
91
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
92
|
|
|
$this->assertArrayNotHasKey( 'basket/coupon', $result['data']['relationships'] ); |
93
|
|
|
|
94
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
public function testDeletePluginException() |
99
|
|
|
{ |
100
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) ); |
101
|
|
|
|
102
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
103
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
$this->assertEquals( 409, $response->getStatusCode() ); |
107
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
public function testDeleteMShopException() |
112
|
|
|
{ |
113
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
114
|
|
|
|
115
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
116
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
120
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
public function testDeleteException() |
125
|
|
|
{ |
126
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Exception() ) ); |
127
|
|
|
|
128
|
|
|
$response = $object->delete( $this->view->request(), $this->view->response() ); |
129
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
133
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
public function testPost() |
138
|
|
|
{ |
139
|
|
|
$this->addProduct( 'CNC' ); |
140
|
|
|
|
141
|
|
|
$body = '{"data": {"type": "basket/coupon", "id": "GHIJ"}}'; |
142
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
143
|
|
|
|
144
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
145
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
149
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
150
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
151
|
|
|
|
152
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
153
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
154
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['basket/coupon']['data'] ) ); |
155
|
|
|
|
156
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
public function testPostMultiple() |
161
|
|
|
{ |
162
|
|
|
$this->context->getConfig()->set( 'controller/frontend/basket/coupon/allowed', 2 ); |
163
|
|
|
$this->addProduct( 'CNC' ); |
164
|
|
|
|
165
|
|
|
$body = '{"data": [{ |
166
|
|
|
"type": "basket/coupon", "id": "90AB" |
167
|
|
|
}, { |
168
|
|
|
"type": "basket/coupon", "id": "GHIJ" |
169
|
|
|
}]}'; |
170
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
171
|
|
|
|
172
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
173
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
174
|
|
|
|
175
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
176
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
177
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
178
|
|
|
|
179
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
180
|
|
|
$this->assertEquals( 'basket', $result['data']['type'] ); |
181
|
|
|
$this->assertEquals( 2, count( $result['data']['relationships']['basket/coupon']['data'] ) ); |
182
|
|
|
|
183
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
public function testPostPluginException() |
188
|
|
|
{ |
189
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) ); |
190
|
|
|
|
191
|
|
|
$response = $object->post( $this->view->request(), $this->view->response() ); |
192
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
$this->assertEquals( 409, $response->getStatusCode() ); |
196
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
public function testPostMShopException() |
201
|
|
|
{ |
202
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
203
|
|
|
|
204
|
|
|
$response = $object->post( $this->view->request(), $this->view->response() ); |
205
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
209
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
public function testPostException() |
214
|
|
|
{ |
215
|
|
|
$object = $this->object( 'setType', $this->throwException( new \Exception() ) ); |
216
|
|
|
|
217
|
|
|
$response = $object->post( $this->view->request(), $this->view->response() ); |
218
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
219
|
|
|
|
220
|
|
|
|
221
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
222
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
public function testOptions() |
227
|
|
|
{ |
228
|
|
|
$response = $this->object->options( $this->view->request(), $this->view->response() ); |
229
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
230
|
|
|
|
231
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
232
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
233
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
234
|
|
|
|
235
|
|
|
$this->assertEquals( null, $result['meta']['prefix'] ); |
236
|
|
|
$this->assertArrayNotHasKey( 'attributes', $result['meta'] ); |
237
|
|
|
$this->assertArrayNotHasKey( 'filter', $result['meta'] ); |
238
|
|
|
$this->assertArrayNotHasKey( 'sort', $result['meta'] ); |
239
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
|
243
|
|
|
protected function addProduct( $code ) |
244
|
|
|
{ |
245
|
|
|
$prodId = \Aimeos\MShop::create( $this->context, 'product' )->find( $code )->getId(); |
246
|
|
|
|
247
|
|
|
$body = '{"data": {"type": "basket/product", "attributes": {"product.id": ' . $prodId . '}}}'; |
248
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
249
|
|
|
|
250
|
|
|
$object = new \Aimeos\Client\JsonApi\Basket\Product\Standard( $this->context, 'basket/product' ); |
251
|
|
|
$object->setView( $this->view ); |
252
|
|
|
|
253
|
|
|
$object->post( $request, $this->view->response() ); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Returns a test object with a mocked basket controller |
259
|
|
|
* |
260
|
|
|
* @param string $method Basket controller method name to mock |
261
|
|
|
* @param mixed $result Return value of the mocked method |
262
|
|
|
*/ |
263
|
|
|
protected function object( $method, $result ) |
264
|
|
|
{ |
265
|
|
|
$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class ) |
266
|
|
|
->setConstructorArgs( [$this->context] ) |
267
|
|
|
->setMethods( [$method] ) |
268
|
|
|
->getMock(); |
269
|
|
|
|
270
|
|
|
$cntl->expects( $this->once() )->method( $method )->will( $result ); |
271
|
|
|
|
272
|
|
|
\Aimeos\Controller\Frontend\Basket\Factory::injectController( '\Aimeos\Controller\Frontend\Basket\Standard', $cntl ); |
273
|
|
|
|
274
|
|
|
$object = new \Aimeos\Client\JsonApi\Basket\Coupon\Standard( $this->context, 'basket/coupon' ); |
275
|
|
|
$object->setView( $this->view ); |
276
|
|
|
|
277
|
|
|
return $object; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|