Completed
Push — master ( 3272b0...853e83 )
by Aimeos
02:24 queued 12s
created

StandardTest::testGetControllerException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018
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()
20
	{
21
		$this->context = \TestHelperJapi::getContext();
22
		$this->view = $this->context->getView();
23
24
		$this->object = new \Aimeos\Client\JsonApi\Subscription\Standard( $this->context, 'subscription' );
25
		$this->object->setView( $this->view );
26
	}
27
28
29
	protected function tearDown()
30
	{
31
		\Aimeos\Controller\Frontend\Subscription\Factory::injectController( '\Aimeos\Controller\Frontend\Subscription\Standard', null );
32
		unset( $this->context, $this->object, $this->view );
33
	}
34
35
36
	public function testCancel()
37
	{
38
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'subscription' );
39
		$search = $manager->createSearch()->setSlice( 0, 1 );
40
		$search->setConditions( $search->compare( '==', 'subscription.dateend', '2010-01-01' ) );
41
		$items = $manager->searchItems( $search );
42
43
		if( ( $item = reset( $items ) ) === false ) {
44
			throw new \RuntimeException( 'No subscription item found' );
45
		}
46
47
		$params = array( 'id' => $item->getId() );
48
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
49
		$this->view->addHelper( 'param', $helper );
50
51
52
		$object = $this->getObject( 'cancel', $this->returnValue( $item ) );
53
54
		$response = $object->delete( $this->view->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( 'subscription', $result['data']['type'] );
63
		$this->assertGreaterThan( 4, count( $result['data']['attributes'] ) );
64
		$this->assertArrayNotHasKey( 'errors', $result );
65
	}
66
67
68
	public function testCancelControllerException()
69
	{
70
		$object = $this->getObject( 'cancel', $this->throwException( new \Aimeos\Controller\Frontend\Exception() ) );
71
72
		$response = $object->delete( $this->view->request(), $this->view->response() );
73
		$result = json_decode( (string) $response->getBody(), true );
74
75
		$this->assertEquals( 403, $response->getStatusCode() );
76
		$this->assertArrayHasKey( 'errors', $result );
77
	}
78
79
80
	public function testCancelMShopException()
81
	{
82
		$object = $this->getObject( 'cancel', $this->throwException( new \Aimeos\MShop\Exception() ) );
83
84
		$response = $object->delete( $this->view->request(), $this->view->response() );
85
		$result = json_decode( (string) $response->getBody(), true );
86
87
		$this->assertEquals( 404, $response->getStatusCode() );
88
		$this->assertArrayHasKey( 'errors', $result );
89
	}
90
91
92
	public function testCancelException()
93
	{
94
		$object = $this->getObject( 'cancel', $this->throwException( new \Exception() ) );
95
96
		$response = $object->delete( $this->view->request(), $this->view->response() );
97
		$result = json_decode( (string) $response->getBody(), true );
98
99
		$this->assertEquals( 500, $response->getStatusCode() );
100
		$this->assertArrayHasKey( 'errors', $result );
101
	}
102
103
104
	public function testGet()
105
	{
106
		$user = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' )->findItem( 'UTC001' );
107
		$this->context->setUserId( $user->getId() );
108
109
		$params = array( 'fields' => array( 'subscription' => 'subscription.id,subscription.datenext,subscription.dateend' ) );
110
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
111
		$this->view->addHelper( 'param', $helper );
112
113
114
		$response = $this->object->get( $this->view->request(), $this->view->response() );
115
		$result = json_decode( (string) $response->getBody(), true );
116
117
		$this->assertEquals( 200, $response->getStatusCode() );
118
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
119
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
120
121
		$this->assertEquals( 2, $result['meta']['total'] );
122
		$this->assertEquals( 'subscription', $result['data'][0]['type'] );
123
		$this->assertEquals( 3, count( $result['data'][0]['attributes'] ) );
124
		$this->assertArrayNotHasKey( 'errors', $result );
125
	}
126
127
128
	public function testGetById()
129
	{
130
		$user = \Aimeos\MShop\Factory::createManager( $this->context, 'customer' )->findItem( 'UTC001' );
131
		$this->context->setUserId( $user->getId() );
132
133
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'subscription' );
134
		$search = $manager->createSearch()->setSlice( 0, 1 );
135
		$search->setConditions( $search->compare( '==', 'subscription.dateend', '2010-01-01' ) );
136
		$items = $manager->searchItems( $search );
137
138
		if( ( $item = reset( $items ) ) === false ) {
139
			throw new \RuntimeException( 'No subscription item found' );
140
		}
141
142
		$params = array( 'id' => $item->getId() );
143
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
144
		$this->view->addHelper( 'param', $helper );
145
146
147
		$response = $this->object->get( $this->view->request(), $this->view->response() );
148
		$result = json_decode( (string) $response->getBody(), true );
149
150
		$this->assertEquals( 200, $response->getStatusCode() );
151
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
152
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
153
154
		$this->assertEquals( 1, $result['meta']['total'] );
155
		$this->assertEquals( 'subscription', $result['data']['type'] );
156
		$this->assertGreaterThan( 4, count( $result['data']['attributes'] ) );
157
		$this->assertArrayNotHasKey( 'errors', $result );
158
	}
159
160
161
	public function testGetControllerException()
162
	{
163
		$object = $this->getObject( 'createFilter', $this->throwException( new \Aimeos\Controller\Frontend\Exception() ) );
164
165
		$response = $object->get( $this->view->request(), $this->view->response() );
166
		$result = json_decode( (string) $response->getBody(), true );
167
168
		$this->assertEquals( 403, $response->getStatusCode() );
169
		$this->assertArrayHasKey( 'errors', $result );
170
	}
171
172
173
	public function testGetMShopException()
174
	{
175
		$object = $this->getObject( 'createFilter', $this->throwException( new \Aimeos\MShop\Exception() ) );
176
177
		$response = $object->get( $this->view->request(), $this->view->response() );
178
		$result = json_decode( (string) $response->getBody(), true );
179
180
		$this->assertEquals( 404, $response->getStatusCode() );
181
		$this->assertArrayHasKey( 'errors', $result );
182
	}
183
184
185
	public function testGetException()
186
	{
187
		$object = $this->getObject( 'createFilter', $this->throwException( new \Exception() ) );
188
189
		$response = $object->get( $this->view->request(), $this->view->response() );
190
		$result = json_decode( (string) $response->getBody(), true );
191
192
		$this->assertEquals( 500, $response->getStatusCode() );
193
		$this->assertArrayHasKey( 'errors', $result );
194
	}
195
196
197
	public function testOptions()
198
	{
199
		$response = $this->object->options( $this->view->request(), $this->view->response() );
200
		$result = json_decode( (string) $response->getBody(), true );
201
202
		$this->assertEquals( 200, $response->getStatusCode() );
203
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
204
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
205
206
		$this->assertEquals( null, $result['meta']['prefix'] );
207
		$this->assertEquals( 0, count( $result['meta']['attributes'] ) );
208
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
209
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
210
		$this->assertArrayNotHasKey( 'errors', $result );
211
	}
212
213
214
	/**
215
	 * Returns a test object with a mocked subscription controller
216
	 *
217
	 * @param string $method Subscription controller method name to mock
218
	 * @param mixed $result Return value of the mocked method
219
	 */
220
	protected function getObject( $method, $result )
221
	{
222
		$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Subscription\Standard' )
223
			->setConstructorArgs( [$this->context] )
224
			->setMethods( [$method] )
225
			->getMock();
226
227
		$cntl->expects( $this->once() )->method( $method )->will( $result );
228
229
		\Aimeos\Controller\Frontend\Subscription\Factory::injectController( '\Aimeos\Controller\Frontend\Subscription\Standard', $cntl );
230
231
		$object = new \Aimeos\Client\JsonApi\Subscription\Standard( $this->context, 'subscription' );
232
		$object->setView( $this->view );
233
234
235
		return $object;
236
	}
237
}