Passed
Push — master ( 36a836...c7a44e )
by Aimeos
16:28 queued 07:56
created

StandardTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2022
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
		\Aimeos\Controller\Frontend::cache( true );
22
23
		$this->context = \TestHelper::context();
24
		$this->view = $this->context->view();
25
26
		$this->object = new \Aimeos\Client\JsonApi\Product\Standard( $this->context, 'product' );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Client\JsonApi\Pr...Standard::__construct() has too many arguments starting with 'product'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
		$this->object = /** @scrutinizer ignore-call */ new \Aimeos\Client\JsonApi\Product\Standard( $this->context, 'product' );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

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