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\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 ); |
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
|
|
|
'locale.site' => 'locale.site.id,locale.site.code' |
102
|
|
|
), |
103
|
|
|
'sort' => 'product.id', |
104
|
|
|
'include' => 'product.attribute,product.catalog,product.media,product.price,product,product.property,product.property.type,stock,product.supplier,product.text,product.type,locale.site' |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params ); |
108
|
|
|
$this->view->addHelper( 'param', $helper ); |
109
|
|
|
|
110
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
111
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
112
|
|
|
|
113
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
114
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
115
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
116
|
|
|
|
117
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
118
|
|
|
$this->assertEquals( 'product', $result['data']['type'] ); |
119
|
|
|
$this->assertEquals( 7, count( $result['data']['relationships']['text']['data'] ) ); |
120
|
|
|
$this->assertEquals( 2, count( $result['data']['relationships']['price']['data'] ) ); |
121
|
|
|
$this->assertEquals( 3, count( $result['data']['relationships']['media']['data'] ) ); |
122
|
|
|
$this->assertEquals( 4, count( $result['data']['relationships']['product.property']['data'] ) ); |
123
|
|
|
$this->assertEquals( 2, count( $result['data']['relationships']['product']['data'] ) ); |
124
|
|
|
$this->assertEquals( 6, count( $result['data']['relationships']['attribute']['data'] ) ); |
125
|
|
|
$this->assertEquals( 5, count( $result['data']['relationships']['catalog']['data'] ) ); |
126
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['supplier']['data'] ) ); |
127
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['stock']['data'] ) ); |
128
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['text']['data'][0]['attributes'] ) ); |
129
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['locale.site']['data'] ) ); |
130
|
|
|
$this->assertEquals( 1, count( $result['data']['relationships']['product.type']['data'] ) ); |
131
|
|
|
$this->assertGreaterThanOrEqual( 49, count( $result['included'] ) ); |
132
|
|
|
|
133
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
public function testGetItems() |
138
|
|
|
{ |
139
|
|
|
$catId = \Aimeos\MShop::create( $this->context, 'catalog' )->find( 'cafe' )->getId(); |
140
|
|
|
$params = array( |
141
|
|
|
'filter' => array( 'f_catid' => $catId, 'f_listtype' => 'promotion' ), |
142
|
|
|
'fields' => array( |
143
|
|
|
'product' => 'product.id,product.code,product.label' |
144
|
|
|
), |
145
|
|
|
'sort' => '-code,-product.status', |
146
|
|
|
'include' => 'product.attribute,text,product,product.property,product.catalog' |
147
|
|
|
); |
148
|
|
|
$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params ); |
149
|
|
|
$this->view->addHelper( 'param', $helper ); |
150
|
|
|
|
151
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
152
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
153
|
|
|
|
154
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
155
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
156
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
157
|
|
|
|
158
|
|
|
$this->assertEquals( 2, $result['meta']['total'] ); |
159
|
|
|
$this->assertEquals( 2, count( $result['data'] ) ); |
160
|
|
|
$this->assertEquals( 'product', $result['data'][0]['type'] ); |
161
|
|
|
$this->assertEquals( 3, count( $result['data'][0]['attributes'] ) ); |
162
|
|
|
$this->assertEquals( 7, count( $result['data'][0]['relationships']['text']['data'] ) ); |
163
|
|
|
$this->assertEquals( 4, count( $result['data'][0]['relationships']['product.property']['data'] ) ); |
164
|
|
|
$this->assertEquals( 6, count( $result['data'][0]['relationships']['attribute']['data'] ) ); |
165
|
|
|
$this->assertEquals( 2, count( $result['data'][0]['relationships']['product']['data'] ) ); |
166
|
|
|
$this->assertEquals( 5, count( $result['data'][0]['relationships']['catalog']['data'] ) ); |
167
|
|
|
$this->assertEquals( 5, count( $result['data'][1]['relationships']['catalog']['data'] ) ); |
168
|
|
|
$this->assertGreaterThanOrEqual( 47, count( $result['included'] ) ); |
169
|
|
|
$this->assertEquals( 3, count( map( $result['included'] )->groupBy( 'type' )->get( 'catalog', [] ) ) ); |
170
|
|
|
|
171
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
public function testGetItemsCriteria() |
176
|
|
|
{ |
177
|
|
|
$catId = \Aimeos\MShop::create( $this->context, 'catalog' )->find( 'cafe' )->getId(); |
178
|
|
|
$params = array( |
179
|
|
|
'filter' => array( |
180
|
|
|
'f_catid' => $catId, |
181
|
|
|
'f_search' => 'Cafe', |
182
|
|
|
'f_listtype' => 'promotion', |
183
|
|
|
'==' => ['product.type' => 'default'], |
184
|
|
|
), |
185
|
|
|
'sort' => '-product.id', |
186
|
|
|
); |
187
|
|
|
$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params ); |
188
|
|
|
$this->view->addHelper( 'param', $helper ); |
189
|
|
|
|
190
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
191
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
192
|
|
|
|
193
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
194
|
|
|
$this->assertEquals( 2, $result['meta']['total'] ); |
195
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
public function testGetMShopException() |
200
|
|
|
{ |
201
|
|
|
$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Product\Standard::class ) |
202
|
|
|
->setConstructorArgs( [$this->context, 'product'] ) |
203
|
|
|
->onlyMethods( ['getItems'] ) |
204
|
|
|
->getMock(); |
205
|
|
|
|
206
|
|
|
$object->expects( $this->once() )->method( 'getItems' ) |
207
|
|
|
->will( $this->throwException( new \Aimeos\MShop\Exception() ) ); |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
$object->setView( $this->view ); |
211
|
|
|
|
212
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
213
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
217
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
|
221
|
|
|
public function testGetException() |
222
|
|
|
{ |
223
|
|
|
$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Product\Standard::class ) |
224
|
|
|
->setConstructorArgs( [$this->context, 'product'] ) |
225
|
|
|
->onlyMethods( ['getItems'] ) |
226
|
|
|
->getMock(); |
227
|
|
|
|
228
|
|
|
$object->expects( $this->once() )->method( 'getItems' ) |
229
|
|
|
->will( $this->throwException( new \Exception() ) ); |
230
|
|
|
|
231
|
|
|
|
232
|
|
|
$object->setView( $this->view ); |
233
|
|
|
|
234
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
235
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
239
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
|
243
|
|
|
public function testOptions() |
244
|
|
|
{ |
245
|
|
|
$response = $this->object->options( $this->view->request(), $this->view->response() ); |
246
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
247
|
|
|
|
248
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
249
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
250
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
251
|
|
|
|
252
|
|
|
$this->assertEquals( null, $result['meta']['prefix'] ); |
253
|
|
|
$this->assertEquals( 7, count( $result['meta']['filter'] ) ); |
254
|
|
|
$this->assertEquals( 4, count( $result['meta']['sort'] ) ); |
255
|
|
|
$this->assertArrayNotHasKey( 'attributes', $result['meta'] ); |
256
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|