Skip to content

Commit a770989

Browse files
Merge remote-tracking branch '36028/fix-for-issue-#35906' into comm_voted_v2
2 parents ad0f51f + 5fa608c commit a770989

File tree

2 files changed

+147
-3
lines changed

2 files changed

+147
-3
lines changed

app/code/Magento/CatalogGraphQl/Model/Category/DepthCalculator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use GraphQL\Language\AST\FieldNode;
1212
use GraphQL\Language\AST\InlineFragmentNode;
1313
use GraphQL\Language\AST\NodeKind;
14+
use GraphQL\Language\AST\SelectionNode;
1415
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1516

1617
/**
@@ -66,21 +67,21 @@ private function calculateRecursive(ResolveInfo $resolveInfo, Node $node) : int
6667
* Add inline fragment fields into calculating of category depth
6768
*
6869
* @param ResolveInfo $resolveInfo
69-
* @param InlineFragmentNode $inlineFragmentField
70+
* @param SelectionNode $inlineFragmentField
7071
* @param array $depth
7172
* @return int
7273
*/
7374
private function addInlineFragmentDepth(
7475
ResolveInfo $resolveInfo,
75-
InlineFragmentNode $inlineFragmentField,
76+
SelectionNode $inlineFragmentField,
7677
$depth = []
7778
): int {
7879
$selections = $inlineFragmentField->selectionSet->selections;
7980
/** @var FieldNode $field */
8081
foreach ($selections as $field) {
8182
if ($field->kind === NodeKind::INLINE_FRAGMENT) {
8283
$depth[] = $this->addInlineFragmentDepth($resolveInfo, $field, $depth);
83-
} elseif ($field->selectionSet && $field->selectionSet->selections) {
84+
} elseif (!empty($field->selectionSet) && $field->selectionSet->selections) {
8485
$depth[] = $this->calculate($resolveInfo, $field);
8586
}
8687
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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\CatalogGraphQl\Test\Unit\Model\Resolver\Category;
9+
10+
use GraphQL\Language\AST\FieldNode;
11+
use GraphQL\Language\AST\InlineFragmentNode;
12+
use GraphQL\Language\AST\NodeKind;
13+
use GraphQL\Language\AST\NodeList;
14+
use GraphQL\Language\AST\SelectionSetNode;
15+
use Magento\CatalogGraphQl\Model\Category\DepthCalculator;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class DepthCalculatorTest extends TestCase
21+
{
22+
/**
23+
* @var DepthCalculator
24+
*/
25+
private DepthCalculator $depthCalculator;
26+
27+
/**
28+
* @var ResolveInfo|MockObject
29+
*/
30+
private $resolveInfoMock;
31+
32+
/**
33+
* @var FieldNode|MockObject
34+
*/
35+
private $fieldNodeMock;
36+
37+
/**
38+
* Await for depth of '1' if selectionSet is null
39+
* @return void
40+
*/
41+
public function testCalculateWithNullAsSelectionSet(): void
42+
{
43+
$this->fieldNodeMock->kind = NodeKind::FIELD;
44+
/** @var SelectionSetNode $selectionSetNode */
45+
$selectionSetNode = new SelectionSetNode([]);
46+
$selectionSetNode->selections = $this->getSelectionsArrayForNullCase();
47+
$this->fieldNodeMock->selectionSet = $selectionSetNode;
48+
$result = $this->depthCalculator->calculate($this->resolveInfoMock, $this->fieldNodeMock);
49+
$this->assertSame(1, $result);
50+
}
51+
52+
/**
53+
* Await for depth of '2' if selectionSet is not null
54+
* @return void
55+
*/
56+
public function testCalculateNonNullAsSelectionSet(): void
57+
{
58+
$this->fieldNodeMock->kind = NodeKind::FIELD;
59+
$selectionSetNode = $this->getSelectionSetNode();
60+
$selectionSetNode->selections = $this->getSelectionsArrayForNonNullCase();
61+
$this->fieldNodeMock->selectionSet = $selectionSetNode;
62+
$result = $this->depthCalculator->calculate($this->resolveInfoMock, $this->fieldNodeMock);
63+
$this->assertEquals(2, $result);
64+
}
65+
66+
/**
67+
* @return NodeList
68+
*/
69+
private function getSelectionsArrayForNullCase()
70+
{
71+
$selectionSetNode = $this->getSelectionSetNode();
72+
$selectionSetNode->selections = $this->getNodeList();
73+
$inlineFragmentNode = $this->getNewInlineFragmentNode();
74+
$inlineFragmentNode->selectionSet = $selectionSetNode;
75+
return new NodeList([
76+
$this->getNewFieldNode(),
77+
$inlineFragmentNode
78+
]);
79+
}
80+
81+
/**
82+
* @return FieldNode
83+
*/
84+
private function getNewFieldNode()
85+
{
86+
return new FieldNode([]);
87+
}
88+
89+
/**
90+
* @return InlineFragmentNode
91+
*/
92+
private function getNewInlineFragmentNode()
93+
{
94+
return new InlineFragmentNode([]);
95+
}
96+
97+
/**
98+
* @return NodeList
99+
*/
100+
private function getSelectionsArrayForNonNullCase()
101+
{
102+
$newFieldNode = $this->getNewFieldNode();
103+
$newFieldNode->selectionSet = $this->getSelectionSetNode();
104+
$newFieldNode->selectionSet->selections = $this->getNodeList();
105+
$newFieldNode->selectionSet->selections[] = $this->getNewFieldNode();
106+
$selectionSetNode = $this->getSelectionSetNode();
107+
$selectionSetNode->selections = new NodeList([$newFieldNode]);
108+
$inlineFragmentNode = $this->getNewInlineFragmentNode();
109+
$inlineFragmentNode->selectionSet = $selectionSetNode;
110+
return new NodeList([
111+
$newFieldNode,
112+
$inlineFragmentNode
113+
]);
114+
}
115+
116+
/**
117+
* @inheritDoc
118+
*/
119+
protected function setUp(): void
120+
{
121+
$this->depthCalculator = new DepthCalculator();
122+
$this->resolveInfoMock = $this->createMock(ResolveInfo::class);
123+
$this->fieldNodeMock = $this->getMockBuilder(FieldNode::class)
124+
->disableOriginalConstructor()
125+
->getMock();
126+
}
127+
128+
/**
129+
* @return \GraphQL\Language\AST\SelectionSetNode
130+
*/
131+
protected function getSelectionSetNode($nodes = []): SelectionSetNode
132+
{
133+
return new SelectionSetNode($nodes);
134+
}
135+
136+
/**
137+
* @return \GraphQL\Language\AST\NodeList
138+
*/
139+
protected function getNodeList(): NodeList
140+
{
141+
return new NodeList([]);
142+
}
143+
}

0 commit comments

Comments
 (0)