|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Catalog; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\TestFramework\ObjectManager; |
| 13 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 14 | + |
| 15 | +/** |
| 16 | + * Test of getting child products info of configurable product on category request |
| 17 | + */ |
| 18 | +class CategoryProductsVariantsTest extends GraphQlAbstract |
| 19 | +{ |
| 20 | + /** |
| 21 | + * |
| 22 | + * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php |
| 23 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 24 | + */ |
| 25 | + public function testGetSimpleProductsFromCategory() |
| 26 | + { |
| 27 | + |
| 28 | + $query |
| 29 | + = <<<QUERY |
| 30 | +{ |
| 31 | + category(id: 2) { |
| 32 | + id |
| 33 | + name |
| 34 | + products { |
| 35 | + items { |
| 36 | + sku |
| 37 | + ... on ConfigurableProduct { |
| 38 | + variants { |
| 39 | + product { |
| 40 | + sku |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | +QUERY; |
| 49 | + |
| 50 | + $response = $this->graphQlQuery($query); |
| 51 | + |
| 52 | + /** @var ProductRepositoryInterface $productRepository */ |
| 53 | + $productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class); |
| 54 | + $product = $productRepository->get('simple_10', false, null, true); |
| 55 | + |
| 56 | + $this->assertArrayHasKey('variants', $response['category']['products']['items'][0]); |
| 57 | + $this->assertCount(2, $response['category']['products']['items'][0]['variants']); |
| 58 | + $this->assertSimpleProductFields($product, $response['category']['products']['items'][0]['variants'][0]); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param ProductInterface $product |
| 63 | + * @param array $actualResponse |
| 64 | + */ |
| 65 | + private function assertSimpleProductFields($product, $actualResponse) |
| 66 | + { |
| 67 | + $assertionMap = [ |
| 68 | + [ |
| 69 | + 'response_field' => 'product', 'expected_value' => [ |
| 70 | + "sku" => $product->getSku() |
| 71 | + ] |
| 72 | + ], |
| 73 | + ]; |
| 74 | + |
| 75 | + $this->assertResponseFields($actualResponse, $assertionMap); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param array $actualResponse |
| 80 | + * @param array $assertionMap |
| 81 | + */ |
| 82 | + private function assertResponseFields($actualResponse, $assertionMap) |
| 83 | + { |
| 84 | + foreach ($assertionMap as $key => $assertionData) { |
| 85 | + $expectedValue = isset($assertionData['expected_value']) |
| 86 | + ? $assertionData['expected_value'] |
| 87 | + : $assertionData; |
| 88 | + $responseField = isset($assertionData['response_field']) ? $assertionData['response_field'] : $key; |
| 89 | + self::assertNotNull( |
| 90 | + $expectedValue, |
| 91 | + "Value of '{$responseField}' field must not be NULL" |
| 92 | + ); |
| 93 | + self::assertEquals( |
| 94 | + $expectedValue, |
| 95 | + $actualResponse[$responseField], |
| 96 | + "Value of '{$responseField}' field in response does not match expected value: " |
| 97 | + . var_export($expectedValue, true) |
| 98 | + ); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments