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

StandardTest::getObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
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 16
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), 2018-2021
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Subscription;
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
		$user = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' );
25
		$this->context->setUserId( $user->getId() );
26
27
		$this->object = new \Aimeos\Client\JsonApi\Subscription\Standard( $this->context, 'subscription' );
28
		$this->object->setView( $this->view );
29
	}
30
31
32
	protected function tearDown() : void
33
	{
34
		\Aimeos\Controller\Frontend\Subscription\Factory::injectController( '\Aimeos\Controller\Frontend\Subscription\Standard', null );
35
		unset( $this->context, $this->object, $this->view );
36
	}
37
38
39
	public function testCancel()
40
	{
41
		$item = $this->getSubscription();
42
		$params = array( 'id' => $item->getId() );
43
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
44
		$this->view->addHelper( 'param', $helper );
45
46
47
		$object = $this->object( 'cancel', $this->returnValue( $item ) );
48
49
		$response = $object->delete( $this->view->request(), $this->view->response() );
50
		$result = json_decode( (string) $response->getBody(), true );
51
52
		$this->assertEquals( 200, $response->getStatusCode() );
53
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
54
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
55
56
		$this->assertEquals( 1, $result['meta']['total'] );
57
		$this->assertEquals( 'subscription', $result['data']['type'] );
58
		$this->assertGreaterThan( 4, count( $result['data']['attributes'] ) );
59
		$this->assertArrayNotHasKey( 'errors', $result );
60
	}
61
62
63
	public function testCancelControllerException()
64
	{
65
		$object = $this->object( 'cancel', $this->throwException( new \Aimeos\Controller\Frontend\Exception() ) );
66
67
		$response = $object->delete( $this->view->request(), $this->view->response() );
68
		$result = json_decode( (string) $response->getBody(), true );
69
70
		$this->assertEquals( 403, $response->getStatusCode() );
71
		$this->assertArrayHasKey( 'errors', $result );
72
	}
73
74
75
	public function testCancelMShopException()
76
	{
77
		$object = $this->object( 'cancel', $this->throwException( new \Aimeos\MShop\Exception() ) );
78
79
		$response = $object->delete( $this->view->request(), $this->view->response() );
80
		$result = json_decode( (string) $response->getBody(), true );
81
82
		$this->assertEquals( 404, $response->getStatusCode() );
83
		$this->assertArrayHasKey( 'errors', $result );
84
	}
85
86
87
	public function testCancelException()
88
	{
89
		$object = $this->object( 'cancel', $this->throwException( new \Exception() ) );
90
91
		$response = $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\MW\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\MW\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
		$object = $this->object( 'search', $this->throwException( new \Aimeos\Controller\Frontend\Exception() ) );
147
148
		$response = $object->get( $this->view->request(), $this->view->response() );
149
		$result = json_decode( (string) $response->getBody(), true );
150
151
		$this->assertEquals( 403, $response->getStatusCode() );
152
		$this->assertArrayHasKey( 'errors', $result );
153
	}
154
155
156
	public function testGetMShopException()
157
	{
158
		$object = $this->object( 'search', $this->throwException( new \Aimeos\MShop\Exception() ) );
159
160
		$response = $object->get( $this->view->request(), $this->view->response() );
161
		$result = json_decode( (string) $response->getBody(), true );
162
163
		$this->assertEquals( 404, $response->getStatusCode() );
164
		$this->assertArrayHasKey( 'errors', $result );
165
	}
166
167
168
	public function testGetException()
169
	{
170
		$object = $this->object( 'search', $this->throwException( new \Exception() ) );
171
172
		$response = $object->get( $this->view->request(), $this->view->response() );
173
		$result = json_decode( (string) $response->getBody(), true );
174
175
		$this->assertEquals( 500, $response->getStatusCode() );
176
		$this->assertArrayHasKey( 'errors', $result );
177
	}
178
179
180
	public function testOptions()
181
	{
182
		$response = $this->object->options( $this->view->request(), $this->view->response() );
183
		$result = json_decode( (string) $response->getBody(), true );
184
185
		$this->assertEquals( 200, $response->getStatusCode() );
186
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
187
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
188
189
		$this->assertEquals( null, $result['meta']['prefix'] );
190
		$this->assertEquals( 0, count( $result['meta']['attributes'] ) );
191
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
192
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
193
		$this->assertArrayNotHasKey( 'errors', $result );
194
	}
195
196
197
	/**
198
	 * Returns a test object with a mocked subscription controller
199
	 *
200
	 * @param string $method Subscription controller method name to mock
201
	 * @param mixed $result Return value of the mocked method
202
	 */
203
	protected function object( $method, $result )
204
	{
205
		$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Subscription\Standard::class )
206
			->setConstructorArgs( [$this->context] )
207
			->setMethods( [$method] )
208
			->getMock();
209
210
		$cntl->expects( $this->once() )->method( $method )->will( $result );
211
212
		\Aimeos\Controller\Frontend\Subscription\Factory::injectController( '\Aimeos\Controller\Frontend\Subscription\Standard', $cntl );
213
214
		$object = new \Aimeos\Client\JsonApi\Subscription\Standard( $this->context, 'subscription' );
215
		$object->setView( $this->view );
216
217
218
		return $object;
219
	}
220
221
222
	protected function getSubscription()
223
	{
224
		$manager = \Aimeos\MShop::create( $this->context, 'subscription' );
225
226
		$search = $manager->filter()->slice( 0, 1 );
227
		$search->setConditions( $search->compare( '==', 'subscription.dateend', '2010-01-01' ) );
228
229
		if( ( $item = $manager->search( $search )->first() ) === null ) {
230
			throw new \RuntimeException( 'No subscription item found' );
231
		}
232
233
		return $item;
234
	}
235
}
236