Completed
Push — master ( 3d76e0...38a76c )
by Aimeos
07:49
created

StandardTest::testDelete()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Basket;
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()
20
	{
21
		$this->context = \TestHelperJapi::getContext();
22
		$templatePaths = \TestHelperJapi::getTemplatePaths();
23
		$this->view = $this->context->getView();
24
25
		$this->object = new \Aimeos\Client\JsonApi\Basket\Standard( $this->context, $this->view, $templatePaths, 'basket' );
26
	}
27
28
29
	protected function tearDown()
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": {"attributes": {"order.base.comment": "test"}}}';
38
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
39
40
		$response = $this->object->patch( $request, $this->view->response() );
41
		$result = json_decode( (string) $response->getBody(), true );
42
43
		$this->assertEquals( 'test', $result['data']['attributes']['order.base.comment'] );
44
45
46
		$response = $this->object->delete( $request, $this->view->response() );
47
		$result = json_decode( (string) $response->getBody(), true );
48
49
		$this->assertEquals( 200, $response->getStatusCode() );
50
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
51
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
52
53
		$this->assertEquals( 1, $result['meta']['total'] );
54
		$this->assertEquals( 'basket', $result['data']['type'] );
55
		$this->assertGreaterThan( 13, count( $result['data']['attributes'] ) );
56
		$this->assertEquals( '', $result['data']['attributes']['order.base.comment'] );
57
58
		$this->assertArrayNotHasKey( 'errors', $result );
59
	}
60
61
62
	public function testDeleteMShopException()
63
	{
64
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
65
66
		$response = $object->delete( $this->view->request(), $this->view->response() );
67
		$result = json_decode( (string) $response->getBody(), true );
68
69
70
		$this->assertEquals( 404, $response->getStatusCode() );
71
		$this->assertArrayHasKey( 'errors', $result );
72
	}
73
74
75
	public function testDeleteException()
76
	{
77
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
78
79
		$response = $object->delete( $this->view->request(), $this->view->response() );
80
		$result = json_decode( (string) $response->getBody(), true );
81
82
83
		$this->assertEquals( 500, $response->getStatusCode() );
84
		$this->assertArrayHasKey( 'errors', $result );
85
	}
86
87
88
	public function testGet()
89
	{
90
		$response = $this->object->get( $this->view->request(), $this->view->response() );
91
		$result = json_decode( (string) $response->getBody(), true );
92
93
		$this->assertEquals( 200, $response->getStatusCode() );
94
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
95
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
96
97
		$this->assertEquals( 1, $result['meta']['total'] );
98
		$this->assertEquals( 'basket', $result['data']['type'] );
99
		$this->assertGreaterThan( 13, count( $result['data']['attributes'] ) );
100
101
		$this->assertArrayNotHasKey( 'errors', $result );
102
	}
103
104
105
	public function testGetById()
106
	{
107
		$params = array(
108
			'id' => $this->getOrderBaseItem()->getId(),
109
			'fields' => array(
110
				'basket' => 'order.base.id,order.base.comment'
111
			),
112
		);
113
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
114
		$this->view->addHelper( 'param', $helper );
115
116
		$response = $this->object->get( $this->view->request(), $this->view->response() );
117
		$result = json_decode( (string) $response->getBody(), true );
118
119
		$this->assertEquals( 200, $response->getStatusCode() );
120
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
121
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
122
123
		$this->assertEquals( 1, $result['meta']['total'] );
124
		$this->assertEquals( 'basket', $result['data']['type'] );
125
		$this->assertEquals( 2, count( $result['data']['attributes'] ) );
126
127
		$this->assertArrayNotHasKey( 'errors', $result );
128
	}
129
130
131
	public function testGetMShopException()
132
	{
133
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
134
135
		$response = $object->get( $this->view->request(), $this->view->response() );
136
		$result = json_decode( (string) $response->getBody(), true );
137
138
139
		$this->assertEquals( 404, $response->getStatusCode() );
140
		$this->assertArrayHasKey( 'errors', $result );
141
	}
142
143
144
	public function testGetException()
145
	{
146
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
147
148
		$response = $object->get( $this->view->request(), $this->view->response() );
149
		$result = json_decode( (string) $response->getBody(), true );
150
151
152
		$this->assertEquals( 500, $response->getStatusCode() );
153
		$this->assertArrayHasKey( 'errors', $result );
154
	}
155
156
157
	public function testPatch()
158
	{
159
		$body = '{"data": {"attributes": {"order.base.comment": "test"}}}	';
160
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
161
162
		$response = $this->object->patch( $request, $this->view->response() );
163
		$result = json_decode( (string) $response->getBody(), true );
164
165
166
		$this->assertEquals( 200, $response->getStatusCode() );
167
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
168
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
169
170
		$this->assertEquals( 1, $result['meta']['total'] );
171
		$this->assertEquals( 'basket', $result['data']['type'] );
172
		$this->assertGreaterThan( 13, count( $result['data']['attributes'] ) );
173
		$this->assertEquals( 'test', $result['data']['attributes']['order.base.comment'] );
174
175
		$this->assertArrayNotHasKey( 'errors', $result );
176
	}
177
178
179
	public function testPatchMShopException()
180
	{
181
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
182
183
		$body = '{"data": {"attributes": []}}';
184
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
185
186
		$response = $object->patch( $request, $this->view->response() );
187
		$result = json_decode( (string) $response->getBody(), true );
188
189
190
		$this->assertEquals( 404, $response->getStatusCode() );
191
		$this->assertArrayHasKey( 'errors', $result );
192
	}
193
194
195
	public function testPatchException()
196
	{
197
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
198
199
		$body = '{"data": {"attributes": []}}';
200
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
201
202
		$response = $object->patch( $request, $this->view->response() );
203
		$result = json_decode( (string) $response->getBody(), true );
204
205
206
		$this->assertEquals( 500, $response->getStatusCode() );
207
		$this->assertArrayHasKey( 'errors', $result );
208
	}
209
210
211
	public function testPost()
212
	{
213
		$orderBaseItem = $this->getOrderBaseItem();
214
		$object = $this->getObject( 'store', $this->returnValue( $orderBaseItem ) );
215
216
		$body = '{"data": {"attributes": {"order.base.comment": "test"}}}';
217
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
218
219
		$response = $object->post( $request, $this->view->response() );
220
		$result = json_decode( (string) $response->getBody(), true );
221
222
		$this->assertEquals( 200, $response->getStatusCode() );
223
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
224
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
225
226
		$this->assertEquals( 1, $result['meta']['total'] );
227
		$this->assertNotNull( $result['data']['id'] );
228
		$this->assertEquals( 'basket', $result['data']['type'] );
229
		$this->assertGreaterThan( 13, count( $result['data']['attributes'] ) );
230
231
		$this->assertArrayNotHasKey( 'errors', $result );
232
	}
233
234
235
	public function testPostMShopException()
236
	{
237
		$object = $this->getObject( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
238
239
		$response = $object->post( $this->view->request(), $this->view->response() );
240
		$result = json_decode( (string) $response->getBody(), true );
241
242
243
		$this->assertEquals( 404, $response->getStatusCode() );
244
		$this->assertArrayHasKey( 'errors', $result );
245
	}
246
247
248
	public function testPostException()
249
	{
250
		$object = $this->getObject( 'setType', $this->throwException( new \Exception() ) );
251
252
		$response = $object->post( $this->view->request(), $this->view->response() );
253
		$result = json_decode( (string) $response->getBody(), true );
254
255
256
		$this->assertEquals( 500, $response->getStatusCode() );
257
		$this->assertArrayHasKey( 'errors', $result );
258
	}
259
260
261
	/**
262
	 * Returns a stored basket
263
	 *
264
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Basket object
265
	 */
266
	protected function getOrderBaseItem()
267
	{
268
		$baseManager = \Aimeos\MShop\Factory::createManager( $this->context, 'order/base' );
269
270
		$search = $baseManager->createSearch();
271
		$search->setConditions( $search->compare( '==', 'order.base.price', '672.00') );
272
273
		$items = $baseManager->searchItems( $search );
274
275
		if( ( $item = reset( $items ) ) === false ) {
276
			throw new \Exception( 'No order/base item with price "672.00" found' );
277
		}
278
279
		return $item;
280
	}
281
282
283
	/**
284
	 * Returns a test object with a mocked basket controller
285
	 *
286
	 * @param string $method Basket controller method name to mock
287
	 * @param mixed $result Return value of the mocked method
288
	 */
289
	protected function getObject( $method, $result )
290
	{
291
		$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Basket\Standard' )
292
			->setConstructorArgs( [$this->context] )
293
			->setMethods( [$method] )
294
			->getMock();
295
296
		$cntl->expects( $this->once() )->method( $method )->will( $result );
297
298
		\Aimeos\Controller\Frontend\Basket\Factory::injectController( '\Aimeos\Controller\Frontend\Basket\Standard', $cntl );
299
300
		$templatePaths = \TestHelperJapi::getTemplatePaths();
301
		$object = new \Aimeos\Client\JsonApi\Basket\Standard( $this->context, $this->view, $templatePaths, 'basket' );
302
303
		return $object;
304
	}
305
}