Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Additional checks for fragments added in category tree #101

Merged
merged 2 commits into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/code/Magento/CatalogGraphQl/Model/AttributesJoiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function join(FieldNode $fieldNode, AbstractCollection $collection) : voi

/** @var FieldNode $field */
foreach ($query as $field) {
if ($field->kind === 'InlineFragment') {
continue;
}

if (!$collection->isAttributeAdded($field->name->value)) {
$collection->addAttributeToSelect($field->name->value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function calculate(FieldNode $fieldNode) : int
$depth = count($selections) ? 1 : 0;
$childrenDepth = [0];
foreach ($selections as $node) {
if ($node->kind === 'InlineFragment') {
continue;
}

$childrenDepth[] = $this->calculate($node);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ private function joinAttributesRecursively(Collection $collection, FieldNode $fi

/** @var FieldNode $node */
foreach ($subSelection as $node) {
if ($node->kind === 'InlineFragment') {
continue;
}

$this->joinAttributesRecursively($collection, $node);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Catalog;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test of getting child products info of configurable product on category request
*/
class CategoryProductsVariantsTest extends GraphQlAbstract
{
/**
*
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function testGetSimpleProductsFromCategory()
{

$query
= <<<QUERY
{
category(id: 2) {
id
name
products {
items {
sku
... on ConfigurableProduct {
variants {
product {
sku
}
}
}
}
}
}
}
QUERY;

$response = $this->graphQlQuery($query);

/** @var ProductRepositoryInterface $productRepository */
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
$product = $productRepository->get('simple_10', false, null, true);

$this->assertArrayHasKey('variants', $response['category']['products']['items'][0]);
$this->assertCount(2, $response['category']['products']['items'][0]['variants']);
$this->assertSimpleProductFields($product, $response['category']['products']['items'][0]['variants'][0]);
}

/**
* @param ProductInterface $product
* @param array $actualResponse
*/
private function assertSimpleProductFields($product, $actualResponse)
{
$assertionMap = [
[
'response_field' => 'product', 'expected_value' => [
"sku" => $product->getSku()
]
],
];

$this->assertResponseFields($actualResponse, $assertionMap);
}

/**
* @param array $actualResponse
* @param array $assertionMap
*/
private function assertResponseFields($actualResponse, $assertionMap)
{
foreach ($assertionMap as $key => $assertionData) {
$expectedValue = isset($assertionData['expected_value'])
? $assertionData['expected_value']
: $assertionData;
$responseField = isset($assertionData['response_field']) ? $assertionData['response_field'] : $key;
self::assertNotNull(
$expectedValue,
"Value of '{$responseField}' field must not be NULL"
);
self::assertEquals(
$expectedValue,
$actualResponse[$responseField],
"Value of '{$responseField}' field in response does not match expected value: "
. var_export($expectedValue, true)
);
}
}
}