|
| 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