Passed
Push — master ( f17e4f...350b1a )
by Aimeos
16:03
created

StandardTest::controller()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2024
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Subscription;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $view;
16
17
18
	protected function setUp() : void
19
	{
20
		\Aimeos\Controller\Frontend::cache( true );
21
22
		$this->context = \TestHelper::context();
23
		$this->view = $this->context->view();
24
25
		$this->context->setUser( \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' ) );
26
	}
27
28
29
	protected function tearDown() : void
30
	{
31
		\Aimeos\Controller\Frontend::cache( false );
32
		unset( $this->view, $this->context );
33
	}
34
35
36
	public function testCancel()
37
	{
38
		$item = $this->getSubscription();
39
		$params = array( 'id' => $item->getId() );
40
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
41
		$this->view->addHelper( 'param', $helper );
42
43
		$this->controller( 'cancel' )->expects( $this->once() )->method( 'cancel' )->willReturn( $item );
44
45
46
		$response = $this->object()->delete( $this->view->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( 'subscription', $result['data']['type'] );
55
		$this->assertGreaterThan( 4, count( $result['data']['attributes'] ) );
56
		$this->assertArrayNotHasKey( 'errors', $result );
57
	}
58
59
60
	public function testCancelControllerException()
61
	{
62
		$this->controller( 'cancel' )->expects( $this->once() )->method( 'cancel' )
63
			->will( $this->throwException( new \Aimeos\Controller\Frontend\Exception() ) );
64
65
		$response = $this->object()->delete( $this->view->request(), $this->view->response() );
66
		$result = json_decode( (string) $response->getBody(), true );
67
68
		$this->assertEquals( 403, $response->getStatusCode() );
69
		$this->assertArrayHasKey( 'errors', $result );
70
	}
71
72
73
	public function testCancelMShopException()
74
	{
75
		$this->controller( 'cancel' )->expects( $this->once() )->method( 'cancel' )
76
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
77
78
		$response = $this->object()->delete( $this->view->request(), $this->view->response() );
79
		$result = json_decode( (string) $response->getBody(), true );
80
81
		$this->assertEquals( 404, $response->getStatusCode() );
82
		$this->assertArrayHasKey( 'errors', $result );
83
	}
84
85
86
	public function testCancelException()
87
	{
88
		$this->controller( 'cancel' )->expects( $this->once() )->method( 'cancel' )
89
			->will( $this->throwException( new \Exception() ) );
90
		$object = $this->object( 'cancel', $this->throwException( new \Exception() ) );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Client\JsonApi\Su...\StandardTest::object() has too many arguments starting with 'cancel'. ( Ignorable by Annotation )

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

90
		/** @scrutinizer ignore-call */ 
91
  $object = $this->object( 'cancel', $this->throwException( new \Exception() ) );

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...
Unused Code introduced by
The assignment to $object is dead and can be removed.
Loading history...
91
92
		$response = $this->object()->delete( $this->view->request(), $this->view->response() );
93
		$result = json_decode( (string) $response->getBody(), true );
94
95
		$this->assertEquals( 500, $response->getStatusCode() );
96
		$this->assertArrayHasKey( 'errors', $result );
97
	}
98
99
100
	public function testGet()
101
	{
102
		$params = array(
103
			'fields' => array( 'subscription' => 'subscription.id,subscription.datenext,subscription.dateend' ),
104
			'sort' => 'subscription.datenext,-subscription.id'
105
		);
106
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
107
		$this->view->addHelper( 'param', $helper );
108
109
110
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
111
		$result = json_decode( (string) $response->getBody(), true );
112
113
		$this->assertEquals( 200, $response->getStatusCode() );
114
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
115
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
116
117
		$this->assertEquals( 2, $result['meta']['total'] );
118
		$this->assertEquals( 'subscription', $result['data'][0]['type'] );
119
		$this->assertEquals( 3, count( $result['data'][0]['attributes'] ) );
120
		$this->assertArrayNotHasKey( 'errors', $result );
121
	}
122
123
124
	public function testGetById()
125
	{
126
		$params = array( 'id' => $this->getSubscription()->getId() );
127
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
128
		$this->view->addHelper( 'param', $helper );
129
130
131
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
132
		$result = json_decode( (string) $response->getBody(), true );
133
134
		$this->assertEquals( 200, $response->getStatusCode() );
135
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
136
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
137
138
		$this->assertEquals( 1, $result['meta']['total'] );
139
		$this->assertEquals( 'subscription', $result['data']['type'] );
140
		$this->assertGreaterThan( 4, count( $result['data']['attributes'] ) );
141
		$this->assertArrayNotHasKey( 'errors', $result );
142
	}
143
144
145
	public function testGetControllerException()
146
	{
147
		$this->controller( 'search' )->expects( $this->once() )->method( 'search' )
148
			->will( $this->throwException( new \Aimeos\Controller\Frontend\Exception() ) );
149
150
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
151
		$result = json_decode( (string) $response->getBody(), true );
152
153
		$this->assertEquals( 403, $response->getStatusCode() );
154
		$this->assertArrayHasKey( 'errors', $result );
155
	}
156
157
158
	public function testGetMShopException()
159
	{
160
		$this->controller( 'search' )->expects( $this->once() )->method( 'search' )
161
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
162
163
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
164
		$result = json_decode( (string) $response->getBody(), true );
165
166
		$this->assertEquals( 404, $response->getStatusCode() );
167
		$this->assertArrayHasKey( 'errors', $result );
168
	}
169
170
171
	public function testGetException()
172
	{
173
		$this->controller( 'search' )->expects( $this->once() )->method( 'search' )
174
			->will( $this->throwException( new \Exception() ) );
175
176
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
177
		$result = json_decode( (string) $response->getBody(), true );
178
179
		$this->assertEquals( 500, $response->getStatusCode() );
180
		$this->assertArrayHasKey( 'errors', $result );
181
	}
182
183
184
	public function testOptions()
185
	{
186
		$response = $this->object()->options( $this->view->request(), $this->view->response() );
187
		$result = json_decode( (string) $response->getBody(), true );
188
189
		$this->assertEquals( 200, $response->getStatusCode() );
190
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
191
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
192
193
		$this->assertEquals( null, $result['meta']['prefix'] );
194
		$this->assertEquals( 0, count( $result['meta']['attributes'] ) );
195
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
196
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
197
		$this->assertArrayNotHasKey( 'errors', $result );
198
	}
199
200
201
	protected function getSubscription()
202
	{
203
		$manager = \Aimeos\MShop::create( $this->context, 'subscription' );
204
205
		$search = $manager->filter()->slice( 0, 1 );
206
		$search->setConditions( $search->compare( '==', 'subscription.dateend', '2010-01-01' ) );
207
208
		if( ( $item = $manager->search( $search )->first() ) === null ) {
209
			throw new \RuntimeException( 'No subscription item found' );
210
		}
211
212
		return $item;
213
	}
214
215
216
	/**
217
	 * Returns a mocked subscription controller
218
	 *
219
	 * @param array|string $methods Subscription controller method name to mock
220
	 * @return Mocked subscription controller
0 ignored issues
show
Bug introduced by
The type Aimeos\Client\JsonApi\Subscription\Mocked was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
221
	 */
222
	protected function controller( $methods )
223
	{
224
		$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Subscription\Standard::class )
225
			->setConstructorArgs( [$this->context] )
226
			->onlyMethods( (array) $methods )
227
			->getMock();
228
229
		\Aimeos\Controller\Frontend::inject( \Aimeos\Controller\Frontend\Subscription\Standard::class, $cntl );
230
231
		return $cntl;
232
	}
233
234
235
	/**
236
	 * Returns the JSON API client object
237
	 *
238
	 * @return \Aimeos\Client\JsonApi\Subscription\Standard JSON API client object
239
	 */
240
	protected function object()
241
	{
242
		$object = new \Aimeos\Client\JsonApi\Subscription\Standard( $this->context );
243
		$object->setView( $this->view );
244
245
		return $object;
246
	}
247
}
248