Completed
Push — master ( b254ca...3fe05c )
by Aimeos
02:44
created

StandardTest::testSetView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Product;
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\Product\Standard( $this->context, 'product' );
25
		$this->object->setView( $this->view );
26
	}
27
28
29
	public function testGetView()
30
	{
31
		$this->assertInstanceOf( '\Aimeos\MW\View\Iface', $this->object->getView() );
32
	}
33
34
35
	public function testSetView()
36
	{
37
		$result = $this->object->setView( $this->view );
38
		$this->assertInstanceOf( '\Aimeos\Client\JsonApi\Iface', $result );
39
	}
40
41
42
	public function testAggregateAttribute()
43
	{
44
		$params = array( 'aggregate' => 'index.attribute.id' );
45
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
46
		$this->view->addHelper( 'param', $helper );
47
48
		$response = $this->object->get( $this->view->request(), $this->view->response() );
49
		$result = json_decode( (string) $response->getBody(), true );
50
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( 14, $result['meta']['total'] );
57
		$this->assertEquals( 14, count( $result['data'] ) );
58
		$this->assertGreaterThan( 0, $result['data'][0]['id'] );
59
		$this->assertGreaterThan( 0, $result['data'][0]['attributes'] );
60
		$this->assertEquals( 'index.attribute.id', $result['data'][0]['type'] );
61
62
		$this->assertArrayNotHasKey( 'errors', $result );
63
	}
64
65
66
	public function testAggregateCatalog()
67
	{
68
		$params = array( 'aggregate' => 'index.catalog.id' );
69
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
70
		$this->view->addHelper( 'param', $helper );
71
72
		$response = $this->object->get( $this->view->request(), $this->view->response() );
73
		$result = json_decode( (string) $response->getBody(), true );
74
75
76
		$this->assertEquals( 200, $response->getStatusCode() );
77
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
78
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
79
80
		$this->assertEquals( 4, $result['meta']['total'] );
81
		$this->assertEquals( 4, count( $result['data'] ) );
82
		$this->assertGreaterThan( 0, $result['data'][0]['id'] );
83
		$this->assertGreaterThan( 0, $result['data'][0]['attributes'] );
84
		$this->assertEquals( 'index.catalog.id', $result['data'][0]['type'] );
85
86
		$this->assertArrayNotHasKey( 'errors', $result );
87
	}
88
89
90
	public function testGetItem()
91
	{
92
		$prodId = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNE' )->getId();
93
		$params = array(
94
			'id' => $prodId,
95
			'fields' => array(
96
				'product' => 'product.id,product.label'
97
			),
98
			'sort' => 'product.id',
99
			'include' => 'attribute,media,price,product,product/property,text'
100
		);
101
102
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
103
		$this->view->addHelper( 'param', $helper );
104
105
		$response = $this->object->get( $this->view->request(), $this->view->response() );
106
		$result = json_decode( (string) $response->getBody(), true );
107
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( 1, $result['meta']['total'] );
114
		$this->assertEquals( 'product', $result['data']['type'] );
115
		$this->assertEquals( 6, count( $result['data']['relationships']['text']['data'] ) );
116
		$this->assertEquals( 2, count( $result['data']['relationships']['price']['data'] ) );
117
		$this->assertEquals( 4, count( $result['data']['relationships']['media']['data'] ) );
118
		$this->assertEquals( 4, count( $result['data']['relationships']['product/property']['data'] ) );
119
		$this->assertEquals( 5, count( $result['data']['relationships']['product']['data'] ) );
120
		$this->assertEquals( 5, count( $result['data']['relationships']['attribute']['data'] ) );
121
		$this->assertEquals( 26, count( $result['included'] ) );
122
123
		$this->assertArrayNotHasKey( 'errors', $result );
124
	}
125
126
127
	public function testGetItems()
128
	{
129
		$catId = \Aimeos\MShop\Factory::createManager( $this->context, 'catalog' )->findItem( 'cafe' )->getId();
130
		$params = array(
131
			'filter' => array( 'f_catid' => $catId ),
132
			'fields' => array(
133
				'product' => 'product.id,product.label'
134
			),
135
			'sort' => '-product.id',
136
			'include' => 'attribute,text,product,product/property'
137
		);
138
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
139
		$this->view->addHelper( 'param', $helper );
140
141
		$response = $this->object->get( $this->view->request(), $this->view->response() );
142
		$result = json_decode( (string) $response->getBody(), true );
143
144
		$this->assertEquals( 200, $response->getStatusCode() );
145
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
146
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
147
148
		$this->assertEquals( 2, $result['meta']['total'] );
149
		$this->assertEquals( 2, count( $result['data'] ) );
150
		$this->assertEquals( 'product', $result['data'][0]['type'] );
151
		$this->assertEquals( 2, count( $result['data'][0]['attributes'] ) );
152
		$this->assertEquals( 6, count( $result['data'][0]['relationships']['text']['data'] ) );
153
		$this->assertEquals( 4, count( $result['data'][0]['relationships']['product/property']['data'] ) );
154
		$this->assertEquals( 5, count( $result['data'][0]['relationships']['attribute']['data'] ) );
155
		$this->assertEquals( 5, count( $result['data'][0]['relationships']['product']['data'] ) );
156
		$this->assertEquals( 32, count( $result['included'] ) );
157
158
		$this->assertArrayNotHasKey( 'errors', $result );
159
	}
160
161
162
	public function testGetItemsCriteria()
163
	{
164
		$catId = \Aimeos\MShop\Factory::createManager( $this->context, 'catalog' )->findItem( 'cafe' )->getId();
165
		$params = array(
166
			'filter' => array(
167
				'f_catid' => $catId,
168
				'f_search' => 'Cafe',
169
				'f_listtype' => ['unittype13', 'unittype19'],
170
				'==' => array( 'product.type.code' => 'default' ),
171
			),
172
			'sort' => '-product.id',
173
		);
174
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
175
		$this->view->addHelper( 'param', $helper );
176
177
		$response = $this->object->get( $this->view->request(), $this->view->response() );
178
		$result = json_decode( (string) $response->getBody(), true );
179
180
		$this->assertEquals( 200, $response->getStatusCode() );
181
		$this->assertEquals( 2, $result['meta']['total'] );
182
		$this->assertArrayNotHasKey( 'errors', $result );
183
	}
184
185
186
	public function testGetMShopException()
187
	{
188
		$object = $this->getMockBuilder( '\Aimeos\Client\JsonApi\Product\Standard' )
189
			->setConstructorArgs( [$this->context, 'product'] )
190
			->setMethods( ['getItems'] )
191
			->getMock();
192
193
		$object->expects( $this->once() )->method( 'getItems' )
194
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
195
196
197
		$object->setView( $this->view );
198
199
		$response = $object->get( $this->view->request(), $this->view->response() );
200
		$result = json_decode( (string) $response->getBody(), true );
201
202
203
		$this->assertEquals( 404, $response->getStatusCode() );
204
		$this->assertArrayHasKey( 'errors', $result );
205
	}
206
207
208
	public function testGetException()
209
	{
210
		$object = $this->getMockBuilder( '\Aimeos\Client\JsonApi\Product\Standard' )
211
			->setConstructorArgs( [$this->context, 'product'] )
212
			->setMethods( ['getItems'] )
213
			->getMock();
214
215
		$object->expects( $this->once() )->method( 'getItems' )
216
			->will( $this->throwException( new \Exception() ) );
217
218
219
		$object->setView( $this->view );
220
221
		$response = $object->get( $this->view->request(), $this->view->response() );
222
		$result = json_decode( (string) $response->getBody(), true );
223
224
225
		$this->assertEquals( 500, $response->getStatusCode() );
226
		$this->assertArrayHasKey( 'errors', $result );
227
	}
228
229
230
	public function testOptions()
231
	{
232
		$response = $this->object->options( $this->view->request(), $this->view->response() );
233
		$result = json_decode( (string) $response->getBody(), true );
234
235
		$this->assertEquals( 200, $response->getStatusCode() );
236
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
237
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
238
239
		$this->assertEquals( null, $result['meta']['prefix'] );
240
		$this->assertEquals( 6, count( $result['meta']['filter'] ) );
241
		$this->assertEquals( 3, count( $result['meta']['sort'] ) );
242
		$this->assertArrayNotHasKey( 'attributes', $result['meta'] );
243
		$this->assertArrayNotHasKey( 'errors', $result );
244
	}
245
}