Completed
Push — master ( c8ac74...421741 )
by Aimeos
02:12
created

StandardTest::getSubscription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
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
		$user = \Aimeos\MShop::create( $this->context, 'customer' )->findItem( 'UTC001' );
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()
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->getObject( '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->getObject( '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->getObject( '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->getObject( '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( 'fields' => array( 'subscription' => 'subscription.id,subscription.datenext,subscription.dateend' ) );
102
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
103
		$this->view->addHelper( 'param', $helper );
104
105
106
		$response = $this->object->get( $this->view->request(), $this->view->response() );
107
		$result = json_decode( (string) $response->getBody(), true );
108
109
		$this->assertEquals( 200, $response->getStatusCode() );
110
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
111
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
112
113
		$this->assertEquals( 2, $result['meta']['total'] );
114
		$this->assertEquals( 'subscription', $result['data'][0]['type'] );
115
		$this->assertEquals( 3, count( $result['data'][0]['attributes'] ) );
116
		$this->assertArrayNotHasKey( 'errors', $result );
117
	}
118
119
120
	public function testGetById()
121
	{
122
		$params = array( 'id' => $this->getSubscription()->getId() );
123
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
124
		$this->view->addHelper( 'param', $helper );
125
126
127
		$response = $this->object->get( $this->view->request(), $this->view->response() );
128
		$result = json_decode( (string) $response->getBody(), true );
129
130
		$this->assertEquals( 200, $response->getStatusCode() );
131
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
132
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
133
134
		$this->assertEquals( 1, $result['meta']['total'] );
135
		$this->assertEquals( 'subscription', $result['data']['type'] );
136
		$this->assertGreaterThan( 4, count( $result['data']['attributes'] ) );
137
		$this->assertArrayNotHasKey( 'errors', $result );
138
	}
139
140
141
	public function testGetControllerException()
142
	{
143
		$object = $this->getObject( 'search', $this->throwException( new \Aimeos\Controller\Frontend\Exception() ) );
144
145
		$response = $object->get( $this->view->request(), $this->view->response() );
146
		$result = json_decode( (string) $response->getBody(), true );
147
148
		$this->assertEquals( 403, $response->getStatusCode() );
149
		$this->assertArrayHasKey( 'errors', $result );
150
	}
151
152
153
	public function testGetMShopException()
154
	{
155
		$object = $this->getObject( 'search', $this->throwException( new \Aimeos\MShop\Exception() ) );
156
157
		$response = $object->get( $this->view->request(), $this->view->response() );
158
		$result = json_decode( (string) $response->getBody(), true );
159
160
		$this->assertEquals( 404, $response->getStatusCode() );
161
		$this->assertArrayHasKey( 'errors', $result );
162
	}
163
164
165
	public function testGetException()
166
	{
167
		$object = $this->getObject( 'search', $this->throwException( new \Exception() ) );
168
169
		$response = $object->get( $this->view->request(), $this->view->response() );
170
		$result = json_decode( (string) $response->getBody(), true );
171
172
		$this->assertEquals( 500, $response->getStatusCode() );
173
		$this->assertArrayHasKey( 'errors', $result );
174
	}
175
176
177
	public function testOptions()
178
	{
179
		$response = $this->object->options( $this->view->request(), $this->view->response() );
180
		$result = json_decode( (string) $response->getBody(), true );
181
182
		$this->assertEquals( 200, $response->getStatusCode() );
183
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
184
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
185
186
		$this->assertEquals( null, $result['meta']['prefix'] );
187
		$this->assertEquals( 0, count( $result['meta']['attributes'] ) );
188
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
189
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
190
		$this->assertArrayNotHasKey( 'errors', $result );
191
	}
192
193
194
	/**
195
	 * Returns a test object with a mocked subscription controller
196
	 *
197
	 * @param string $method Subscription controller method name to mock
198
	 * @param mixed $result Return value of the mocked method
199
	 */
200
	protected function getObject( $method, $result )
201
	{
202
		$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Subscription\Standard::class )
203
			->setConstructorArgs( [$this->context] )
204
			->setMethods( [$method] )
205
			->getMock();
206
207
		$cntl->expects( $this->once() )->method( $method )->will( $result );
208
209
		\Aimeos\Controller\Frontend\Subscription\Factory::injectController( '\Aimeos\Controller\Frontend\Subscription\Standard', $cntl );
210
211
		$object = new \Aimeos\Client\JsonApi\Subscription\Standard( $this->context, 'subscription' );
212
		$object->setView( $this->view );
213
214
215
		return $object;
216
	}
217
218
219
	protected function getSubscription()
220
	{
221
		$manager = \Aimeos\MShop::create( $this->context, 'subscription' );
222
		$search = $manager->createSearch()->setSlice( 0, 1 );
223
		$search->setConditions( $search->compare( '==', 'subscription.dateend', '2010-01-01' ) );
224
		$items = $manager->searchItems( $search );
225
226
		if( ( $item = reset( $items ) ) === false ) {
227
			throw new \RuntimeException( 'No subscription item found' );
228
		}
229
230
		return $item;
231
	}
232
}