Passed
Push — master ( 413d3b...72c491 )
by Aimeos
06:13 queued 03:36
created

StandardTest::getObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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