Passed
Push — master ( ecc215...4dc4bd )
by Aimeos
02:57
created

StandardTest::testPatchPluginException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 10
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2022
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
		\Aimeos\Controller\Frontend::cache( true );
22
23
		$this->context = \TestHelper::context();
24
		$this->view = $this->context->view();
25
26
		$this->object = new \Aimeos\Client\JsonApi\Basket\Address\Standard( $this->context );
27
		$this->object->setView( $this->view );
28
	}
29
30
31
	protected function tearDown() : void
32
	{
33
		\Aimeos\Controller\Frontend::cache( false );
34
		unset( $this->view, $this->object, $this->context );
35
	}
36
37
38
	public function testDelete()
39
	{
40
		$body = '{"data": {"type": "basket/address", "id": "payment", "attributes": {"firstname": "test"}}}';
41
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
42
43
		$response = $this->object->post( $request, $this->view->response() );
44
		$result = json_decode( (string) $response->getBody(), true );
45
46
		$this->assertEquals( 1, count( $result['data']['relationships']['basket/address']['data'] ) );
47
48
49
		$body = '{"data": {"type": "basket/address", "id": "payment"}}';
50
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
51
52
		$response = $this->object->delete( $request, $this->view->response() );
53
		$result = json_decode( (string) $response->getBody(), true );
54
55
		$this->assertEquals( 200, $response->getStatusCode() );
56
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
57
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
58
59
		$this->assertEquals( 1, $result['meta']['total'] );
60
		$this->assertEquals( 'basket', $result['data']['type'] );
61
		$this->assertArrayNotHasKey( 'basket/address', $result['data']['relationships'] );
62
63
		$this->assertArrayNotHasKey( 'errors', $result );
64
	}
65
66
67
	public function testDeleteById()
68
	{
69
		$body = '{"data": {"type": "basket/address", "id": "payment", "attributes": {"firstname": "test"}}}';
70
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
71
72
		$response = $this->object->post( $request, $this->view->response() );
73
		$result = json_decode( (string) $response->getBody(), true );
74
75
		$this->assertEquals( 1, count( $result['data']['relationships']['basket/address']['data'] ) );
76
77
78
		$params = array( 'id' => 'default', 'relatedid' => 'payment' );
79
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
80
		$this->view->addHelper( 'param', $helper );
81
82
		$response = $this->object->delete( $request, $this->view->response() );
83
		$result = json_decode( (string) $response->getBody(), true );
84
85
		$this->assertEquals( 200, $response->getStatusCode() );
86
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
87
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
88
89
		$this->assertEquals( 1, $result['meta']['total'] );
90
		$this->assertEquals( 'basket', $result['data']['type'] );
91
		$this->assertArrayNotHasKey( 'basket/address', $result['data']['relationships'] );
92
93
		$this->assertArrayNotHasKey( 'errors', $result );
94
	}
95
96
97
	public function testDeletePluginException()
98
	{
99
		$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );
100
101
		$response = $object->delete( $this->view->request(), $this->view->response() );
102
		$result = json_decode( (string) $response->getBody(), true );
103
104
105
		$this->assertEquals( 409, $response->getStatusCode() );
106
		$this->assertArrayHasKey( 'errors', $result );
107
	}
108
109
110
	public function testDeleteMShopException()
111
	{
112
		$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
113
114
		$response = $object->delete( $this->view->request(), $this->view->response() );
115
		$result = json_decode( (string) $response->getBody(), true );
116
117
118
		$this->assertEquals( 404, $response->getStatusCode() );
119
		$this->assertArrayHasKey( 'errors', $result );
120
	}
121
122
123
	public function testDeleteException()
124
	{
125
		$object = $this->object( 'setType', $this->throwException( new \Exception() ) );
126
127
		$response = $object->delete( $this->view->request(), $this->view->response() );
128
		$result = json_decode( (string) $response->getBody(), true );
129
130
131
		$this->assertEquals( 500, $response->getStatusCode() );
132
		$this->assertArrayHasKey( 'errors', $result );
133
	}
134
135
136
	public function testPatch()
137
	{
138
		$body = '{"data": {"type": "basket/address", "id": "payment", "attributes": {"firstname": "test"}}}';
139
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
140
141
		$params = array( 'id' => 'default', 'relatedid' => 'payment' );
142
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
143
		$this->view->addHelper( 'param', $helper );
144
145
		$response = $this->object->patch( $request, $this->view->response() );
146
		$result = json_decode( (string) $response->getBody(), true );
147
148
149
		$this->assertEquals( 200, $response->getStatusCode() );
150
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
151
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
152
153
		$this->assertEquals( 1, $result['meta']['total'] );
154
		$this->assertEquals( 'basket', $result['data']['type'] );
155
		$this->assertEquals( 1, count( $result['data']['relationships']['basket/address']['data'] ) );
156
		$this->assertEquals( 'test', $result['included'][0]['attributes']['order.base.address.firstname'] );
157
158
		$this->assertArrayNotHasKey( 'errors', $result );
159
	}
160
161
162
	public function testPatchMultiple()
163
	{
164
		$body = '{"data": [{
165
			"type": "basket/address", "id": "delivery", "attributes": {"firstname": "test"}
166
		}, {
167
			"type": "basket/address", "id": "delivery", "attributes": {"lastname": "test"}
168
		}]}';
169
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
170
171
		$params = array( 'id' => 'default', 'relatedid' => 'delivery' );
172
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
173
		$this->view->addHelper( 'param', $helper );
174
175
		$response = $this->object->patch( $request, $this->view->response() );
176
		$result = json_decode( (string) $response->getBody(), true );
177
178
179
		$this->assertEquals( 200, $response->getStatusCode() );
180
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
181
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
182
183
		$this->assertEquals( 1, $result['meta']['total'] );
184
		$this->assertEquals( 'basket', $result['data']['type'] );
185
		$this->assertEquals( 1, count( $result['data']['relationships']['basket/address']['data'] ) );
186
		$this->assertEquals( 'test', $result['included'][0]['attributes']['order.base.address.firstname'] );
187
		$this->assertEquals( 'test', $result['included'][1]['attributes']['order.base.address.lastname'] );
188
189
		$this->assertArrayNotHasKey( 'errors', $result );
190
	}
191
192
193
	public function testPatchPluginException()
194
	{
195
		$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );
196
197
		$response = $object->patch( $this->view->request(), $this->view->response() );
198
		$result = json_decode( (string) $response->getBody(), true );
199
200
201
		$this->assertEquals( 409, $response->getStatusCode() );
202
		$this->assertArrayHasKey( 'errors', $result );
203
	}
204
205
206
	public function testPatchMShopException()
207
	{
208
		$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
209
210
		$response = $object->patch( $this->view->request(), $this->view->response() );
211
		$result = json_decode( (string) $response->getBody(), true );
212
213
214
		$this->assertEquals( 404, $response->getStatusCode() );
215
		$this->assertArrayHasKey( 'errors', $result );
216
	}
217
218
219
	public function testPatchException()
220
	{
221
		$object = $this->object( 'setType', $this->throwException( new \Exception() ) );
222
223
		$response = $object->patch( $this->view->request(), $this->view->response() );
224
		$result = json_decode( (string) $response->getBody(), true );
225
226
227
		$this->assertEquals( 500, $response->getStatusCode() );
228
		$this->assertArrayHasKey( 'errors', $result );
229
	}
230
231
232
	public function testPost()
233
	{
234
		$body = '{"data": {"type": "basket/address", "id": "payment", "attributes": {"firstname": "test"}}}';
235
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
236
237
		$response = $this->object->post( $request, $this->view->response() );
238
		$result = json_decode( (string) $response->getBody(), true );
239
240
241
		$this->assertEquals( 201, $response->getStatusCode() );
242
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
243
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
244
245
		$this->assertEquals( 1, $result['meta']['total'] );
246
		$this->assertEquals( 'basket', $result['data']['type'] );
247
		$this->assertEquals( 1, count( $result['data']['relationships']['basket/address']['data'] ) );
248
		$this->assertEquals( 'test', $result['included'][0]['attributes']['order.base.address.firstname'] );
249
250
		$this->assertArrayNotHasKey( 'errors', $result );
251
	}
252
253
254
	public function testPostMultiple()
255
	{
256
		$body = '{"data": [{
257
			"type": "basket/address", "id": "payment", "attributes": {"firstname": "test"}
258
		}, {
259
			"type": "basket/address", "id": "delivery", "attributes": {"lastname": "test"}
260
		}]}';
261
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
262
263
		$response = $this->object->post( $request, $this->view->response() );
264
		$result = json_decode( (string) $response->getBody(), true );
265
266
267
		$this->assertEquals( 201, $response->getStatusCode() );
268
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
269
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
270
271
		$this->assertEquals( 1, $result['meta']['total'] );
272
		$this->assertEquals( 'basket', $result['data']['type'] );
273
		$this->assertEquals( 2, count( $result['data']['relationships']['basket/address']['data'] ) );
274
		$this->assertEquals( 'test', $result['included'][0]['attributes']['order.base.address.firstname'] );
275
		$this->assertEquals( 'test', $result['included'][1]['attributes']['order.base.address.lastname'] );
276
277
		$this->assertArrayNotHasKey( 'errors', $result );
278
	}
279
280
281
	public function testPostPluginException()
282
	{
283
		$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );
284
285
		$response = $object->post( $this->view->request(), $this->view->response() );
286
		$result = json_decode( (string) $response->getBody(), true );
287
288
289
		$this->assertEquals( 409, $response->getStatusCode() );
290
		$this->assertArrayHasKey( 'errors', $result );
291
	}
292
293
294
	public function testPostMShopException()
295
	{
296
		$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
297
298
		$response = $object->post( $this->view->request(), $this->view->response() );
299
		$result = json_decode( (string) $response->getBody(), true );
300
301
302
		$this->assertEquals( 404, $response->getStatusCode() );
303
		$this->assertArrayHasKey( 'errors', $result );
304
	}
305
306
307
	public function testPostException()
308
	{
309
		$object = $this->object( 'setType', $this->throwException( new \Exception() ) );
310
311
		$response = $object->post( $this->view->request(), $this->view->response() );
312
		$result = json_decode( (string) $response->getBody(), true );
313
314
315
		$this->assertEquals( 500, $response->getStatusCode() );
316
		$this->assertArrayHasKey( 'errors', $result );
317
	}
318
319
320
	public function testOptions()
321
	{
322
		$response = $this->object->options( $this->view->request(), $this->view->response() );
323
		$result = json_decode( (string) $response->getBody(), true );
324
325
		$this->assertEquals( 200, $response->getStatusCode() );
326
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
327
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
328
329
		$this->assertEquals( null, $result['meta']['prefix'] );
330
		$this->assertEquals( 21, count( $result['meta']['attributes'] ) );
331
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
332
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
333
		$this->assertArrayNotHasKey( 'errors', $result );
334
	}
335
336
337
	/**
338
	 * Returns a test object with a mocked basket controller
339
	 *
340
	 * @param string $method Basket controller method name to mock
341
	 * @param mixed $result Return value of the mocked method
342
	 */
343
	protected function object( $method, $result )
344
	{
345
		$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
346
			->setConstructorArgs( [$this->context] )
347
			->setMethods( [$method] )
348
			->getMock();
349
350
		$cntl->expects( $this->once() )->method( $method )->will( $result );
351
352
		\Aimeos\Controller\Frontend::inject( '\Aimeos\Controller\Frontend\Basket\Standard', $cntl );
353
354
		$object = new \Aimeos\Client\JsonApi\Basket\Address\Standard( $this->context );
355
		$object->setView( $this->view );
356
357
		return $object;
358
	}
359
}
360