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

StandardTest::testOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 14
c 0
b 0
f 0
rs 9.9332
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-2022
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Attribute;
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
		\Aimeos\MShop::cache( true );
23
24
		$this->context = \TestHelper::context();
25
		$this->view = $this->context->view();
26
27
		$this->object = new \Aimeos\Client\JsonApi\Attribute\Standard( $this->context, 'attribute' );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Client\JsonApi\At...Standard::__construct() has too many arguments starting with 'attribute'. ( Ignorable by Annotation )

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

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

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...
28
		$this->object->setView( $this->view );
29
	}
30
31
32
	protected function tearDown() : void
33
	{
34
		\Aimeos\MShop::cache( false );
35
		\Aimeos\Controller\Frontend::cache( false );
36
		unset( $this->view, $this->object, $this->context );
37
	}
38
39
40
	public function testGetItem()
41
	{
42
		$attrManager = \Aimeos\MShop::create( $this->context, 'attribute' );
43
		$attrId = $attrManager->find( 'xs', [], 'product', 'size' )->getId();
44
45
		$params = array(
46
			'id' => $attrId,
47
			'fields' => array(
48
				'attribute' => 'attribute.id,attribute.label'
49
			),
50
			'sort' => 'attribute.id',
51
			'include' => 'media,price,text'
52
		);
53
54
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
55
		$this->view->addHelper( 'param', $helper );
56
57
		$response = $this->object->get( $this->view->request(), $this->view->response() );
58
		$result = json_decode( (string) $response->getBody(), true );
59
60
		$this->assertEquals( 200, $response->getStatusCode() );
61
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
62
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
63
64
		$this->assertEquals( 1, $result['meta']['total'] );
65
		$this->assertEquals( 'attribute', $result['data']['type'] );
66
		$this->assertEquals( 3, count( $result['data']['relationships']['text']['data'] ) );
67
		$this->assertEquals( 1, count( $result['data']['relationships']['price']['data'] ) );
68
		$this->assertEquals( 1, count( $result['data']['relationships']['media']['data'] ) );
69
		$this->assertEquals( 5, count( $result['included'] ) );
70
71
		$this->assertArrayNotHasKey( 'errors', $result );
72
	}
73
74
75
	public function testGetItemProperties()
76
	{
77
		$attrManager = \Aimeos\MShop::create( $this->context, 'attribute' );
78
		$attrId = $attrManager->find( 'testurl', [], 'product', 'download' )->getId();
79
80
		$params = array(
81
			'id' => $attrId,
82
			'fields' => array(
83
				'attribute' => 'attribute.id,attribute.property.value,attribute.property.type.code'
84
			),
85
			'sort' => 'attribute.id',
86
			'include' => 'attribute/property'
87
		);
88
89
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
90
		$this->view->addHelper( 'param', $helper );
91
92
		$response = $this->object->get( $this->view->request(), $this->view->response() );
93
		$result = json_decode( (string) $response->getBody(), true );
94
95
		$this->assertEquals( 200, $response->getStatusCode() );
96
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
97
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
98
99
		$this->assertEquals( 1, $result['meta']['total'] );
100
		$this->assertEquals( 'attribute', $result['data']['type'] );
101
		$this->assertEquals( 2, count( $result['data']['relationships']['attribute/property']['data'] ) );
102
		$this->assertEquals( 2, count( $result['included'] ) );
103
104
		$this->assertArrayNotHasKey( 'errors', $result );
105
	}
106
107
108
	public function testGetItems()
109
	{
110
		$this->context->config()->set( 'client/jsonapi/attribute/types', ['size', 'length', 'width'] );
111
112
		$params = array(
113
			'fields' => array(
114
				'attribute' => 'attribute.id,attribute.type,attribute.code'
115
			),
116
			'include' => 'media,price,text',
117
			'sort' => '-attribute.type,attribute.position',
118
		);
119
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
120
		$this->view->addHelper( 'param', $helper );
121
122
		$response = $this->object->get( $this->view->request(), $this->view->response() );
123
		$result = json_decode( (string) $response->getBody(), true );
124
125
		$this->assertEquals( 200, $response->getStatusCode() );
126
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
127
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
128
129
		$this->assertEquals( 17, $result['meta']['total'] );
130
		$this->assertEquals( 17, count( $result['data'] ) );
131
		$this->assertEquals( 'attribute', $result['data'][0]['type'] );
132
		$this->assertEquals( 3, count( $result['data'][0]['attributes'] ) );
133
		$this->assertEquals( 'size', $result['data'][0]['attributes']['attribute.type'] );
134
		$this->assertEquals( 'xs', $result['data'][0]['attributes']['attribute.code'] );
135
		$this->assertEquals( 23, count( $result['included'] ) );
136
137
		foreach( $result['data'] as $entry ) {
138
			$this->assertContains( $entry['attributes']['attribute.type'], ['size', 'length', 'width'] );
139
		}
140
141
		$this->assertArrayNotHasKey( 'errors', $result );
142
	}
143
144
145
	public function testGetItemsCriteria()
146
	{
147
		$params = array(
148
			'filter' => array(
149
				'==' => array( 'attribute.type' => 'size' ),
150
			),
151
			'sort' => 'attribute.position',
152
		);
153
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
154
		$this->view->addHelper( 'param', $helper );
155
156
		$response = $this->object->get( $this->view->request(), $this->view->response() );
157
		$result = json_decode( (string) $response->getBody(), true );
158
159
160
		$this->assertEquals( 200, $response->getStatusCode() );
161
		$this->assertEquals( 6, $result['meta']['total'] );
162
		$this->assertArrayNotHasKey( 'errors', $result );
163
	}
164
165
166
	public function testGetMShopException()
167
	{
168
		$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Attribute\Standard::class )
169
			->setConstructorArgs( [$this->context, 'attribute'] )
170
			->setMethods( ['getItems'] )
171
			->getMock();
172
173
		$object->expects( $this->once() )->method( 'getItems' )
174
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
175
176
		$object->setView( $this->view );
177
178
		$response = $object->get( $this->view->request(), $this->view->response() );
179
		$result = json_decode( (string) $response->getBody(), true );
180
181
182
		$this->assertEquals( 404, $response->getStatusCode() );
183
		$this->assertArrayHasKey( 'errors', $result );
184
	}
185
186
187
	public function testGetException()
188
	{
189
		$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Attribute\Standard::class )
190
			->setConstructorArgs( [$this->context, 'attribute'] )
191
			->setMethods( ['getItems'] )
192
			->getMock();
193
194
		$object->expects( $this->once() )->method( 'getItems' )
195
			->will( $this->throwException( new \Exception() ) );
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( 500, $response->getStatusCode() );
204
		$this->assertArrayHasKey( 'errors', $result );
205
	}
206
207
208
	public function testOptions()
209
	{
210
		$response = $this->object->options( $this->view->request(), $this->view->response() );
211
		$result = json_decode( (string) $response->getBody(), true );
212
213
		$this->assertEquals( 200, $response->getStatusCode() );
214
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
215
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
216
217
		$this->assertEquals( null, $result['meta']['prefix'] );
218
		$this->assertArrayNotHasKey( 'attributes', $result['meta'] );
219
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
220
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
221
		$this->assertArrayNotHasKey( 'errors', $result );
222
	}
223
}
224