StandardTest   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 126
dl 0
loc 269
rs 10
c 0
b 0
f 0
wmc 15

15 Methods

Rating   Name   Duplication   Size   Complexity  
A testDeleteException() 0 10 1
A testDeleteMShopException() 0 10 1
A object() 0 15 1
A tearDown() 0 4 1
A testPost() 0 20 1
A testDeleteById() 0 29 1
A testOptions() 0 14 1
A testDeletePluginException() 0 10 1
A testPostMultiple() 0 24 1
A testDelete() 0 28 1
A testPostMShopException() 0 10 1
A testPostPluginException() 0 10 1
A setUp() 0 9 1
A testPostException() 0 10 1
A addProduct() 0 11 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2025
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
		\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\Coupon\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
		$this->addProduct( 'CNC' );
41
42
		$body = '{"data": {"type": "basket.coupon", "id": "GHIJ"}}';
43
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
0 ignored issues
show
Bug introduced by
The method createStreamFromString() does not exist on Psr\Http\Message\ResponseInterface. It seems like you code against a sub-type of Psr\Http\Message\ResponseInterface such as Aimeos\Base\View\Helper\Response\Iface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
		$request = $this->view->request()->withBody( $this->view->response()->/** @scrutinizer ignore-call */ createStreamFromString( $body ) );
Loading history...
44
45
		$response = $this->object->post( $request, $this->view->response() );
46
		$result = json_decode( (string) $response->getBody(), true );
47
48
		$this->assertEquals( 1, count( $result['data']['relationships']['basket.coupon']['data'] ) );
49
50
51
		$body = '{"data": {"type": "basket.coupon", "id": "GHIJ"}}';
52
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
53
54
		$response = $this->object->delete( $request, $this->view->response() );
55
		$result = json_decode( (string) $response->getBody(), true );
56
57
		$this->assertEquals( 200, $response->getStatusCode() );
58
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
59
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
60
61
		$this->assertEquals( 1, $result['meta']['total'] );
62
		$this->assertEquals( 'basket', $result['data']['type'] );
63
		$this->assertArrayNotHasKey( 'basket.coupon', $result['data']['relationships'] );
64
65
		$this->assertArrayNotHasKey( 'errors', $result );
66
	}
67
68
69
	public function testDeleteById()
70
	{
71
		$this->addProduct( 'CNC' );
72
73
		$body = '{"data": {"type": "basket.coupon", "id": "GHIJ"}}';
74
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
75
76
		$response = $this->object->post( $request, $this->view->response() );
77
		$result = json_decode( (string) $response->getBody(), true );
78
79
		$this->assertEquals( 1, count( $result['data']['relationships']['basket.coupon']['data'] ) );
80
81
82
		$params = array( 'id' => 'default', 'relatedid' => 'GHIJ' );
83
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
84
		$this->view->addHelper( 'param', $helper );
85
86
		$response = $this->object->delete( $request, $this->view->response() );
87
		$result = json_decode( (string) $response->getBody(), true );
88
89
		$this->assertEquals( 200, $response->getStatusCode() );
90
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
91
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
92
93
		$this->assertEquals( 1, $result['meta']['total'] );
94
		$this->assertEquals( 'basket', $result['data']['type'] );
95
		$this->assertArrayNotHasKey( 'basket.coupon', $result['data']['relationships'] );
96
97
		$this->assertArrayNotHasKey( 'errors', $result );
98
	}
99
100
101
	public function testDeletePluginException()
102
	{
103
		$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );
104
105
		$response = $object->delete( $this->view->request(), $this->view->response() );
106
		$result = json_decode( (string) $response->getBody(), true );
107
108
109
		$this->assertEquals( 409, $response->getStatusCode() );
110
		$this->assertArrayHasKey( 'errors', $result );
111
	}
112
113
114
	public function testDeleteMShopException()
115
	{
116
		$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
117
118
		$response = $object->delete( $this->view->request(), $this->view->response() );
119
		$result = json_decode( (string) $response->getBody(), true );
120
121
122
		$this->assertEquals( 404, $response->getStatusCode() );
123
		$this->assertArrayHasKey( 'errors', $result );
124
	}
125
126
127
	public function testDeleteException()
128
	{
129
		$object = $this->object( 'setType', $this->throwException( new \Exception() ) );
130
131
		$response = $object->delete( $this->view->request(), $this->view->response() );
132
		$result = json_decode( (string) $response->getBody(), true );
133
134
135
		$this->assertEquals( 500, $response->getStatusCode() );
136
		$this->assertArrayHasKey( 'errors', $result );
137
	}
138
139
140
	public function testPost()
141
	{
142
		$this->addProduct( 'CNC' );
143
144
		$body = '{"data": {"type": "basket.coupon", "id": "GHIJ"}}';
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
151
		$this->assertEquals( 201, $response->getStatusCode() );
152
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
153
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
154
155
		$this->assertEquals( 1, $result['meta']['total'] );
156
		$this->assertEquals( 'basket', $result['data']['type'] );
157
		$this->assertEquals( 1, count( $result['data']['relationships']['basket.coupon']['data'] ) );
158
159
		$this->assertArrayNotHasKey( 'errors', $result );
160
	}
161
162
163
	public function testPostMultiple()
164
	{
165
		$this->context->config()->set( 'controller/frontend/basket/coupon/allowed', 2 );
166
		$this->addProduct( 'CNC' );
167
168
		$body = '{"data": [{
169
			"type": "basket.coupon", "id": "90AB"
170
		}, {
171
			"type": "basket.coupon", "id": "GHIJ"
172
		}]}';
173
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
174
175
		$response = $this->object->post( $request, $this->view->response() );
176
		$result = json_decode( (string) $response->getBody(), true );
177
178
		$this->assertEquals( 201, $response->getStatusCode() );
179
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
180
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
181
182
		$this->assertEquals( 1, $result['meta']['total'] );
183
		$this->assertEquals( 'basket', $result['data']['type'] );
184
		$this->assertEquals( 2, count( $result['data']['relationships']['basket.coupon']['data'] ) );
185
186
		$this->assertArrayNotHasKey( 'errors', $result );
187
	}
188
189
190
	public function testPostPluginException()
191
	{
192
		$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Plugin\Provider\Exception() ) );
193
194
		$response = $object->post( $this->view->request(), $this->view->response() );
195
		$result = json_decode( (string) $response->getBody(), true );
196
197
198
		$this->assertEquals( 409, $response->getStatusCode() );
199
		$this->assertArrayHasKey( 'errors', $result );
200
	}
201
202
203
	public function testPostMShopException()
204
	{
205
		$object = $this->object( 'setType', $this->throwException( new \Aimeos\MShop\Exception() ) );
206
207
		$response = $object->post( $this->view->request(), $this->view->response() );
208
		$result = json_decode( (string) $response->getBody(), true );
209
210
211
		$this->assertEquals( 404, $response->getStatusCode() );
212
		$this->assertArrayHasKey( 'errors', $result );
213
	}
214
215
216
	public function testPostException()
217
	{
218
		$object = $this->object( 'setType', $this->throwException( new \Exception() ) );
219
220
		$response = $object->post( $this->view->request(), $this->view->response() );
221
		$result = json_decode( (string) $response->getBody(), true );
222
223
224
		$this->assertEquals( 500, $response->getStatusCode() );
225
		$this->assertArrayHasKey( 'errors', $result );
226
	}
227
228
229
	public function testOptions()
230
	{
231
		$response = $this->object->options( $this->view->request(), $this->view->response() );
232
		$result = json_decode( (string) $response->getBody(), true );
233
234
		$this->assertEquals( 200, $response->getStatusCode() );
235
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
236
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
237
238
		$this->assertEquals( null, $result['meta']['prefix'] );
239
		$this->assertArrayNotHasKey( 'attributes', $result['meta'] );
240
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
241
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
242
		$this->assertArrayNotHasKey( 'errors', $result );
243
	}
244
245
246
	protected function addProduct( $code )
247
	{
248
		$prodId = \Aimeos\MShop::create( $this->context, 'product' )->find( $code )->getId();
249
250
		$body = '{"data": {"type": "basket/product", "attributes": {"product.id": ' . $prodId . '}}}';
251
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
252
253
		$object = new \Aimeos\Client\JsonApi\Basket\Product\Standard( $this->context, 'basket/product' );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Client\JsonApi\Ba...Standard::__construct() has too many arguments starting with 'basket/product'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

253
		$object = /** @scrutinizer ignore-call */ new \Aimeos\Client\JsonApi\Basket\Product\Standard( $this->context, 'basket/product' );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
254
		$object->setView( $this->view );
255
256
		$object->post( $request, $this->view->response() );
257
	}
258
259
260
	/**
261
	 * Returns a test object with a mocked basket controller
262
	 *
263
	 * @param string $method Basket controller method name to mock
264
	 * @param mixed $result Return value of the mocked method
265
	 */
266
	protected function object( $method, $result )
267
	{
268
		$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Basket\Standard::class )
269
			->setConstructorArgs( [$this->context] )
270
			->onlyMethods( [$method] )
271
			->getMock();
272
273
		$cntl->expects( $this->once() )->method( $method )->will( $result );
274
275
		\Aimeos\Controller\Frontend::inject( \Aimeos\Controller\Frontend\Basket\Standard::class, $cntl );
276
277
		$object = new \Aimeos\Client\JsonApi\Basket\Coupon\Standard( $this->context );
278
		$object->setView( $this->view );
279
280
		return $object;
281
	}
282
}
283