StandardTest::testGetItemsCriteria()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
c 0
b 0
f 0
rs 9.9
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-2025
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 );
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,attribute.type'
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( 1, count( $result['data']['relationships']['attribute.type']['data'] ) );
70
		$this->assertEquals( 6, count( $result['included'] ) );
71
72
		$this->assertArrayNotHasKey( 'errors', $result );
73
	}
74
75
76
	public function testGetItemProperties()
77
	{
78
		$attrManager = \Aimeos\MShop::create( $this->context, 'attribute' );
79
		$attrId = $attrManager->find( 'testurl', [], 'product', 'download' )->getId();
80
81
		$params = array(
82
			'id' => $attrId,
83
			'fields' => array(
84
				'attribute' => 'attribute.id,attribute.property.value,attribute.property.type.code'
85
			),
86
			'sort' => 'attribute.id',
87
			'include' => 'attribute/property'
88
		);
89
90
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
91
		$this->view->addHelper( 'param', $helper );
92
93
		$response = $this->object->get( $this->view->request(), $this->view->response() );
94
		$result = json_decode( (string) $response->getBody(), true );
95
96
		$this->assertEquals( 200, $response->getStatusCode() );
97
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
98
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
99
100
		$this->assertEquals( 1, $result['meta']['total'] );
101
		$this->assertEquals( 'attribute', $result['data']['type'] );
102
		$this->assertEquals( 2, count( $result['data']['relationships']['attribute.property']['data'] ) );
103
		$this->assertEquals( 2, count( $result['included'] ) );
104
105
		$this->assertArrayNotHasKey( 'errors', $result );
106
	}
107
108
109
	public function testGetItems()
110
	{
111
		$this->context->config()->set( 'client/jsonapi/attribute/types', ['size', 'length', 'width'] );
112
113
		$params = array(
114
			'fields' => array(
115
				'attribute' => 'attribute.id,attribute.type,attribute.code'
116
			),
117
			'include' => 'media,price,text',
118
			'sort' => '-attribute.type,attribute.position',
119
		);
120
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
121
		$this->view->addHelper( 'param', $helper );
122
123
		$response = $this->object->get( $this->view->request(), $this->view->response() );
124
		$result = json_decode( (string) $response->getBody(), true );
125
126
		$this->assertEquals( 200, $response->getStatusCode() );
127
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
128
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
129
130
		$this->assertEquals( 17, $result['meta']['total'] );
131
		$this->assertEquals( 17, count( $result['data'] ) );
132
		$this->assertEquals( 'attribute', $result['data'][0]['type'] );
133
		$this->assertEquals( 3, count( $result['data'][0]['attributes'] ) );
134
		$this->assertEquals( 'size', $result['data'][0]['attributes']['attribute.type'] );
135
		$this->assertEquals( 'xs', $result['data'][0]['attributes']['attribute.code'] );
136
		$this->assertEquals( 23, count( $result['included'] ) );
137
138
		foreach( $result['data'] as $entry ) {
139
			$this->assertContains( $entry['attributes']['attribute.type'], ['size', 'length', 'width'] );
140
		}
141
142
		$this->assertArrayNotHasKey( 'errors', $result );
143
	}
144
145
146
	public function testGetItemsCriteria()
147
	{
148
		$params = array(
149
			'filter' => array(
150
				'==' => array( 'attribute.type' => 'size' ),
151
			),
152
			'sort' => 'attribute.position',
153
		);
154
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
155
		$this->view->addHelper( 'param', $helper );
156
157
		$response = $this->object->get( $this->view->request(), $this->view->response() );
158
		$result = json_decode( (string) $response->getBody(), true );
159
160
161
		$this->assertEquals( 200, $response->getStatusCode() );
162
		$this->assertEquals( 6, $result['meta']['total'] );
163
		$this->assertArrayNotHasKey( 'errors', $result );
164
	}
165
166
167
	public function testGetMShopException()
168
	{
169
		$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Attribute\Standard::class )
170
			->setConstructorArgs( [$this->context, 'attribute'] )
171
			->onlyMethods( ['getItems'] )
172
			->getMock();
173
174
		$object->expects( $this->once() )->method( 'getItems' )
175
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
176
177
		$object->setView( $this->view );
178
179
		$response = $object->get( $this->view->request(), $this->view->response() );
180
		$result = json_decode( (string) $response->getBody(), true );
181
182
183
		$this->assertEquals( 404, $response->getStatusCode() );
184
		$this->assertArrayHasKey( 'errors', $result );
185
	}
186
187
188
	public function testGetException()
189
	{
190
		$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Attribute\Standard::class )
191
			->setConstructorArgs( [$this->context, 'attribute'] )
192
			->onlyMethods( ['getItems'] )
193
			->getMock();
194
195
		$object->expects( $this->once() )->method( 'getItems' )
196
			->will( $this->throwException( new \Exception() ) );
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( 500, $response->getStatusCode() );
205
		$this->assertArrayHasKey( 'errors', $result );
206
	}
207
208
209
	public function testOptions()
210
	{
211
		$response = $this->object->options( $this->view->request(), $this->view->response() );
212
		$result = json_decode( (string) $response->getBody(), true );
213
214
		$this->assertEquals( 200, $response->getStatusCode() );
215
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
216
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
217
218
		$this->assertEquals( null, $result['meta']['prefix'] );
219
		$this->assertArrayNotHasKey( 'attributes', $result['meta'] );
220
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
221
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
222
		$this->assertArrayNotHasKey( 'errors', $result );
223
	}
224
}
225