StandardTest::object()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
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-2025
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
91
		$response = $this->object()->delete( $this->view->request(), $this->view->response() );
92
		$result = json_decode( (string) $response->getBody(), true );
93
94
		$this->assertEquals( 500, $response->getStatusCode() );
95
		$this->assertArrayHasKey( 'errors', $result );
96
	}
97
98
99
	public function testGet()
100
	{
101
		$params = array(
102
			'fields' => array( 'subscription' => 'subscription.id,subscription.datenext,subscription.dateend' ),
103
			'sort' => 'subscription.datenext,-subscription.id'
104
		);
105
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
106
		$this->view->addHelper( 'param', $helper );
107
108
109
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
110
		$result = json_decode( (string) $response->getBody(), true );
111
112
		$this->assertEquals( 200, $response->getStatusCode() );
113
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
114
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
115
116
		$this->assertEquals( 2, $result['meta']['total'] );
117
		$this->assertEquals( 'subscription', $result['data'][0]['type'] );
118
		$this->assertEquals( 3, count( $result['data'][0]['attributes'] ) );
119
		$this->assertArrayNotHasKey( 'errors', $result );
120
	}
121
122
123
	public function testGetById()
124
	{
125
		$params = array( 'id' => $this->getSubscription()->getId() );
126
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
127
		$this->view->addHelper( 'param', $helper );
128
129
130
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
131
		$result = json_decode( (string) $response->getBody(), true );
132
133
		$this->assertEquals( 200, $response->getStatusCode() );
134
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
135
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
136
137
		$this->assertEquals( 1, $result['meta']['total'] );
138
		$this->assertEquals( 'subscription', $result['data']['type'] );
139
		$this->assertGreaterThan( 4, count( $result['data']['attributes'] ) );
140
		$this->assertArrayNotHasKey( 'errors', $result );
141
	}
142
143
144
	public function testGetControllerException()
145
	{
146
		$this->controller( 'search' )->expects( $this->once() )->method( 'search' )
147
			->will( $this->throwException( new \Aimeos\Controller\Frontend\Exception() ) );
148
149
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
150
		$result = json_decode( (string) $response->getBody(), true );
151
152
		$this->assertEquals( 403, $response->getStatusCode() );
153
		$this->assertArrayHasKey( 'errors', $result );
154
	}
155
156
157
	public function testGetMShopException()
158
	{
159
		$this->controller( 'search' )->expects( $this->once() )->method( 'search' )
160
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
161
162
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
163
		$result = json_decode( (string) $response->getBody(), true );
164
165
		$this->assertEquals( 404, $response->getStatusCode() );
166
		$this->assertArrayHasKey( 'errors', $result );
167
	}
168
169
170
	public function testGetException()
171
	{
172
		$this->controller( 'search' )->expects( $this->once() )->method( 'search' )
173
			->will( $this->throwException( new \Exception() ) );
174
175
		$response = $this->object()->get( $this->view->request(), $this->view->response() );
176
		$result = json_decode( (string) $response->getBody(), true );
177
178
		$this->assertEquals( 500, $response->getStatusCode() );
179
		$this->assertArrayHasKey( 'errors', $result );
180
	}
181
182
183
	public function testOptions()
184
	{
185
		$response = $this->object()->options( $this->view->request(), $this->view->response() );
186
		$result = json_decode( (string) $response->getBody(), true );
187
188
		$this->assertEquals( 200, $response->getStatusCode() );
189
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
190
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
191
192
		$this->assertEquals( null, $result['meta']['prefix'] );
193
		$this->assertEquals( 0, count( $result['meta']['attributes'] ) );
194
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
195
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
196
		$this->assertArrayNotHasKey( 'errors', $result );
197
	}
198
199
200
	protected function getSubscription()
201
	{
202
		$manager = \Aimeos\MShop::create( $this->context, 'subscription' );
203
204
		$search = $manager->filter()->slice( 0, 1 );
205
		$search->setConditions( $search->compare( '==', 'subscription.dateend', '2010-01-01' ) );
206
207
		if( ( $item = $manager->search( $search )->first() ) === null ) {
208
			throw new \RuntimeException( 'No subscription item found' );
209
		}
210
211
		return $item;
212
	}
213
214
215
	/**
216
	 * Returns a mocked subscription controller
217
	 *
218
	 * @param array|string $methods Subscription controller method name to mock
219
	 * @return \Aimeos\Controller\Frontend\Subscription\Standard Mocked subscription controller
220
	 */
221
	protected function controller( $methods )
222
	{
223
		$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Subscription\Standard::class )
224
			->setConstructorArgs( [$this->context] )
225
			->onlyMethods( (array) $methods )
226
			->getMock();
227
228
		\Aimeos\Controller\Frontend::inject( \Aimeos\Controller\Frontend\Subscription\Standard::class, $cntl );
229
230
		return $cntl;
231
	}
232
233
234
	/**
235
	 * Returns the JSON API client object
236
	 *
237
	 * @return \Aimeos\Client\JsonApi\Subscription\Standard JSON API client object
238
	 */
239
	protected function object()
240
	{
241
		$object = new \Aimeos\Client\JsonApi\Subscription\Standard( $this->context );
242
		$object->setView( $this->view );
243
244
		return $object;
245
	}
246
}
247