|
9 | 9 |
|
10 | 10 | use Magento\Framework\DataObject;
|
11 | 11 | use Magento\TestFramework\TestCase\GraphQlAbstract;
|
| 12 | +use Magento\Catalog\Api\Data\ProductInterface; |
12 | 13 | use Magento\Catalog\Api\ProductRepositoryInterface;
|
| 14 | +use Magento\TestFramework\ObjectManager; |
13 | 15 |
|
14 | 16 | class CategoryTest extends GraphQlAbstract
|
15 | 17 | {
|
@@ -172,7 +174,6 @@ public function testCategoryProducts()
|
172 | 174 | new_from_date
|
173 | 175 | new_to_date
|
174 | 176 | options_container
|
175 |
| - |
176 | 177 | price {
|
177 | 178 | minimalPrice {
|
178 | 179 | amount {
|
@@ -268,5 +269,149 @@ public function testCategoryProducts()
|
268 | 269 | $this->assertEquals(2, $response['category']['products']['total_count']);
|
269 | 270 | $this->assertEquals(1, $response['category']['products']['page_info']['current_page']);
|
270 | 271 | $this->assertEquals(20, $response['category']['products']['page_info']['page_size']);
|
| 272 | + |
| 273 | + /** |
| 274 | + * @var ProductRepositoryInterface $productRepository |
| 275 | + */ |
| 276 | + $productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class); |
| 277 | + $firstProductSku = 'simple'; |
| 278 | + $firstProduct = $productRepository->get($firstProductSku, false, null, true); |
| 279 | + $this->assertBaseFields($firstProduct, $response['category']['products']['items'][0]); |
| 280 | + $this->assertAttributes($response['category']['products']['items'][0]); |
| 281 | + $this->assertWebsites($firstProduct, $response['category']['products']['items'][0]['websites']); |
| 282 | + |
| 283 | + $secondProductSku = '12345'; |
| 284 | + $secondProduct = $productRepository->get($secondProductSku, false, null, true); |
| 285 | + $this->assertBaseFields($secondProduct, $response['category']['products']['items'][1]); |
| 286 | + $this->assertAttributes($response['category']['products']['items'][1]); |
| 287 | + $this->assertWebsites($secondProduct, $response['category']['products']['items'][1]['websites']); |
| 288 | + } |
| 289 | + |
| 290 | + /** |
| 291 | + * @param ProductInterface $product |
| 292 | + * @param array $actualResponse |
| 293 | + */ |
| 294 | + private function assertBaseFields($product, $actualResponse) |
| 295 | + { |
| 296 | + |
| 297 | + $assertionMap = [ |
| 298 | + ['response_field' => 'attribute_set_id', 'expected_value' => $product->getAttributeSetId()], |
| 299 | + ['response_field' => 'created_at', 'expected_value' => $product->getCreatedAt()], |
| 300 | + ['response_field' => 'id', 'expected_value' => $product->getId()], |
| 301 | + ['response_field' => 'name', 'expected_value' => $product->getName()], |
| 302 | + ['response_field' => 'price', 'expected_value' => |
| 303 | + [ |
| 304 | + 'minimalPrice' => [ |
| 305 | + 'amount' => [ |
| 306 | + 'value' => $product->getPrice(), |
| 307 | + 'currency' => 'USD' |
| 308 | + ], |
| 309 | + 'adjustments' => [] |
| 310 | + ], |
| 311 | + 'regularPrice' => [ |
| 312 | + 'amount' => [ |
| 313 | + 'value' => $product->getPrice(), |
| 314 | + 'currency' => 'USD' |
| 315 | + ], |
| 316 | + 'adjustments' => [] |
| 317 | + ], |
| 318 | + 'maximalPrice' => [ |
| 319 | + 'amount' => [ |
| 320 | + 'value' => $product->getPrice(), |
| 321 | + 'currency' => 'USD' |
| 322 | + ], |
| 323 | + 'adjustments' => [] |
| 324 | + ], |
| 325 | + ] |
| 326 | + ], |
| 327 | + ['response_field' => 'sku', 'expected_value' => $product->getSku()], |
| 328 | + ['response_field' => 'type_id', 'expected_value' => $product->getTypeId()], |
| 329 | + ['response_field' => 'updated_at', 'expected_value' => $product->getUpdatedAt()], |
| 330 | +// ['response_field' => 'weight', 'expected_value' => $product->getWeight()], |
| 331 | + ]; |
| 332 | + |
| 333 | + $this->assertResponseFields($actualResponse, $assertionMap); |
| 334 | + } |
| 335 | + |
| 336 | + /** |
| 337 | + * @param ProductInterface $product |
| 338 | + * @param array $actualResponse |
| 339 | + */ |
| 340 | + private function assertWebsites($product, $actualResponse) |
| 341 | + { |
| 342 | + $assertionMap = [ |
| 343 | + [ |
| 344 | + 'id' => current($product->getExtensionAttributes()->getWebsiteIds()), |
| 345 | + 'name' => 'Main Website', |
| 346 | + 'code' => 'base', |
| 347 | + 'sort_order' => 0, |
| 348 | + 'default_group_id' => '1', |
| 349 | + 'is_default' => true, |
| 350 | + ] |
| 351 | + ]; |
| 352 | + |
| 353 | + $this->assertEquals($actualResponse, $assertionMap); |
| 354 | + } |
| 355 | + |
| 356 | + /** |
| 357 | + * @param array $actualResponse |
| 358 | + */ |
| 359 | + private function assertAttributes($actualResponse) |
| 360 | + { |
| 361 | + $eavAttributes = [ |
| 362 | + 'url_key', |
| 363 | + 'description', |
| 364 | + 'meta_description', |
| 365 | + 'meta_keyword', |
| 366 | + 'meta_title', |
| 367 | + 'short_description', |
| 368 | + 'tax_class_id', |
| 369 | + 'country_of_manufacture', |
| 370 | + 'gift_message_available', |
| 371 | + 'new_from_date', |
| 372 | + 'new_to_date', |
| 373 | + 'options_container', |
| 374 | + 'special_price', |
| 375 | + 'special_from_date', |
| 376 | + 'special_to_date', |
| 377 | + ]; |
| 378 | + |
| 379 | + foreach($eavAttributes as $eavAttribute){ |
| 380 | + $this->assertArrayHasKey($eavAttribute, $actualResponse); |
| 381 | + } |
| 382 | + } |
| 383 | + |
| 384 | + /** |
| 385 | + * @param array $actualResponse |
| 386 | + * @param array $assertionMap ['response_field_name' => 'response_field_value', ...] |
| 387 | + * OR [['response_field' => $field, 'expected_value' => $value], ...] |
| 388 | + */ |
| 389 | + private function assertResponseFields($actualResponse, $assertionMap) |
| 390 | + { |
| 391 | + foreach ($assertionMap as $key => $assertionData) { |
| 392 | + $expectedValue = isset($assertionData['expected_value']) |
| 393 | + ? $assertionData['expected_value'] |
| 394 | + : $assertionData; |
| 395 | + $responseField = isset($assertionData['response_field']) ? $assertionData['response_field'] : $key; |
| 396 | + self::assertNotNull( |
| 397 | + $expectedValue, |
| 398 | + "Value of '{$responseField}' field must not be NULL" |
| 399 | + ); |
| 400 | + self::assertEquals( |
| 401 | + $expectedValue, |
| 402 | + $actualResponse[$responseField], |
| 403 | + "Value of '{$responseField}' field in response does not match expected value: " |
| 404 | + . var_export($expectedValue, true) |
| 405 | + ); |
| 406 | + } |
| 407 | + } |
| 408 | + |
| 409 | + private function eavAttributesToGraphQlSchemaFieldTranslator($attributeCode) |
| 410 | + { |
| 411 | + if(isset($this->eavAttributesToGraphQlSchemaFieldMap[$attributeCode])){ |
| 412 | + return $this->eavAttributesToGraphQlSchemaFieldMap[$attributeCode]; |
| 413 | + } |
| 414 | + |
| 415 | + return $attributeCode; |
271 | 416 | }
|
272 | 417 | }
|
0 commit comments