Passed
Push — master ( a14c92...413d3b )
by Aimeos
05:44 queued 03:20
created

StandardTest::testGetView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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