Skip to content

Commit 87beb1d

Browse files
committed
Merge branch 'B2B-2464' into B2B-2463
2 parents 44d87c6 + 9a8b4ca commit 87beb1d

File tree

5 files changed

+116
-60
lines changed

5 files changed

+116
-60
lines changed

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

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77

88
namespace Magento\CatalogGraphQl\Model\Category;
99

10-
use Magento\Catalog\Api\CategoryRepositoryInterface;
11-
use Magento\Catalog\Api\Data\CategorySearchResultsInterface;
12-
use Magento\Catalog\Api\Data\CategorySearchResultsInterfaceFactory;
1310
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
1411
use Magento\CatalogGraphQl\Model\Resolver\Categories\DataProvider\Category\CollectionProcessorInterface;
1512
use Magento\CatalogGraphQl\Model\Category\Filter\SearchCriteria;
1613
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
14+
use Magento\Framework\DB\Select;
1715
use Magento\Framework\Exception\InputException;
1816
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1917
use Magento\GraphQl\Model\Query\ContextInterface;
@@ -39,16 +37,6 @@ class CategoryFilter
3937
*/
4038
private $extensionAttributesJoinProcessor;
4139

42-
/**
43-
* @var CategorySearchResultsInterfaceFactory
44-
*/
45-
private $categorySearchResultsFactory;
46-
47-
/**
48-
* @var CategoryRepositoryInterface
49-
*/
50-
private $categoryRepository;
51-
5240
/**
5341
* @var SearchCriteria
5442
*/
@@ -58,23 +46,17 @@ class CategoryFilter
5846
* @param CollectionFactory $categoryCollectionFactory
5947
* @param CollectionProcessorInterface $collectionProcessor
6048
* @param JoinProcessorInterface $extensionAttributesJoinProcessor
61-
* @param CategorySearchResultsInterfaceFactory $categorySearchResultsFactory
62-
* @param CategoryRepositoryInterface $categoryRepository
6349
* @param SearchCriteria $searchCriteria
6450
*/
6551
public function __construct(
6652
CollectionFactory $categoryCollectionFactory,
6753
CollectionProcessorInterface $collectionProcessor,
6854
JoinProcessorInterface $extensionAttributesJoinProcessor,
69-
CategorySearchResultsInterfaceFactory $categorySearchResultsFactory,
70-
CategoryRepositoryInterface $categoryRepository,
7155
SearchCriteria $searchCriteria
7256
) {
7357
$this->categoryCollectionFactory = $categoryCollectionFactory;
7458
$this->collectionProcessor = $collectionProcessor;
7559
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
76-
$this->categorySearchResultsFactory = $categorySearchResultsFactory;
77-
$this->categoryRepository = $categoryRepository;
7860
$this->searchCriteria = $searchCriteria;
7961
}
8062

@@ -95,22 +77,21 @@ public function getResult(array $criteria, StoreInterface $store, array $attribu
9577
$this->extensionAttributesJoinProcessor->process($collection);
9678
$this->collectionProcessor->process($collection, $searchCriteria, $attributeNames, $context);
9779

98-
/** @var CategorySearchResultsInterface $searchResult */
99-
$categories = $this->categorySearchResultsFactory->create();
100-
$categories->setSearchCriteria($searchCriteria);
101-
$categories->setItems($collection->getItems());
102-
$categories->setTotalCount($collection->getSize());
80+
// only fetch necessary category entity id
81+
$collection
82+
->getSelect()
83+
->reset(Select::COLUMNS)
84+
->columns(
85+
'e.entity_id'
86+
);
10387

104-
$categoryIds = [];
105-
foreach ($categories->getItems() as $category) {
106-
$categoryIds[] = (int)$category->getId();
107-
}
88+
$categoryIds = $collection->load()->getLoadedIds();
10889

10990
$totalPages = 0;
110-
if ($categories->getTotalCount() > 0 && $searchCriteria->getPageSize() > 0) {
111-
$totalPages = ceil($categories->getTotalCount() / $searchCriteria->getPageSize());
91+
if ($collection->getSize() > 0 && $searchCriteria->getPageSize() > 0) {
92+
$totalPages = ceil($collection->getSize() / $searchCriteria->getPageSize());
11293
}
113-
if ($searchCriteria->getCurrentPage() > $totalPages && $categories->getTotalCount() > 0) {
94+
if ($searchCriteria->getCurrentPage() > $totalPages && $collection->getSize() > 0) {
11495
throw new GraphQlInputException(
11596
__(
11697
'currentPage value %1 specified is greater than the %2 page(s) available.',
@@ -121,7 +102,7 @@ public function getResult(array $criteria, StoreInterface $store, array $attribu
121102

122103
return [
123104
'category_ids' => $categoryIds,
124-
'total_count' => $categories->getTotalCount(),
105+
'total_count' => $collection->getSize(),
125106
'page_info' => [
126107
'total_pages' => $totalPages,
127108
'page_size' => $searchCriteria->getPageSize(),

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ public function hydrateCategory(Category $category, $basicFieldsOnly = false) :
6060
if ($basicFieldsOnly) {
6161
$categoryData = $category->getData();
6262
} else {
63-
$categoryData = $this->dataObjectProcessor->buildOutputDataArray($category, CategoryInterface::class);
63+
$categoryData = $this->dataObjectProcessor->buildOutputDataArray(
64+
$category,
65+
CategoryInterface::class
66+
);
6467
}
68+
6569
$categoryData['id'] = $category->getId();
6670
$categoryData['uid'] = $this->uidEncoder->encode((string) $category->getId());
6771
$categoryData['children'] = [];

app/code/Magento/CatalogGraphQl/etc/graphql/di.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@
191191
<type name="Magento\Catalog\Api\ProductRepositoryInterface">
192192
<plugin name="availableProductsFilter" type="Magento\CatalogGraphQl\Plugin\AvailableProductsFilter" />
193193
</type>
194+
<type name="Magento\CatalogGraphQl\Model\Category\Hydrator">
195+
<arguments>
196+
<argument name="dataObjectProcessor" xsi:type="object">Magento\CatalogGraphQl\Category\DataObjectProcessor</argument>
197+
</arguments>
198+
</type>
194199
<virtualType name="Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ChildProduct"
195200
type="Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product">
196201
<arguments>
@@ -207,4 +212,16 @@
207212
</argument>
208213
</arguments>
209214
</virtualType>
215+
<virtualType
216+
name="Magento\CatalogGraphQl\Category\DataObjectProcessor"
217+
type="Magento\Framework\Reflection\DataObjectProcessor"
218+
>
219+
<arguments>
220+
<argument name="excludedMethodsClassMap" xsi:type="array">
221+
<item name="Magento\Catalog\Api\Data\CategoryInterface" xsi:type="array">
222+
<item name="getChildren" xsi:type="string">getChildren</item>
223+
</item>
224+
</argument>
225+
</arguments>
226+
</virtualType>
210227
</config>

lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,36 @@ class DataObjectProcessor
4646
*/
4747
private $processors;
4848

49+
/**
50+
* @var array[]
51+
*/
52+
private $excludedMethodsClassMap;
53+
4954
/**
5055
* @param MethodsMap $methodsMapProcessor
5156
* @param TypeCaster $typeCaster
5257
* @param FieldNamer $fieldNamer
5358
* @param CustomAttributesProcessor $customAttributesProcessor
5459
* @param ExtensionAttributesProcessor $extensionAttributesProcessor
5560
* @param array $processors
61+
* @param array $excludedMethodsClassMap
5662
*/
5763
public function __construct(
5864
MethodsMap $methodsMapProcessor,
5965
TypeCaster $typeCaster,
6066
FieldNamer $fieldNamer,
6167
CustomAttributesProcessor $customAttributesProcessor,
6268
ExtensionAttributesProcessor $extensionAttributesProcessor,
63-
array $processors = []
69+
array $processors = [],
70+
array $excludedMethodsClassMap = []
6471
) {
6572
$this->methodsMapProcessor = $methodsMapProcessor;
6673
$this->typeCaster = $typeCaster;
6774
$this->fieldNamer = $fieldNamer;
6875
$this->extensionAttributesProcessor = $extensionAttributesProcessor;
6976
$this->customAttributesProcessor = $customAttributesProcessor;
7077
$this->processors = $processors;
78+
$this->excludedMethodsClassMap = $excludedMethodsClassMap;
7179
}
7280

7381
/**
@@ -84,7 +92,13 @@ public function buildOutputDataArray($dataObject, $dataObjectType)
8492
$methods = $this->methodsMapProcessor->getMethodsMap($dataObjectType);
8593
$outputData = [];
8694

95+
$excludedMethodsForDataObjectType = $this->excludedMethodsClassMap[$dataObjectType] ?? [];
96+
8797
foreach (array_keys($methods) as $methodName) {
98+
if (in_array($methodName, $excludedMethodsForDataObjectType)) {
99+
continue;
100+
}
101+
88102
if (!$this->methodsMapProcessor->isMethodValidForDataField($dataObjectType, $methodName)) {
89103
continue;
90104
}

lib/internal/Magento/Framework/Reflection/Test/Unit/DataObjectProcessorTest.php

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class DataObjectProcessorTest extends TestCase
2626
*/
2727
private $dataObjectProcessor;
2828

29+
/**
30+
* @var MethodsMap
31+
*/
32+
private $methodsMapProcessor;
33+
2934
/**
3035
* @var ExtensionAttributesProcessor|MockObject
3136
*/
@@ -34,7 +39,7 @@ class DataObjectProcessorTest extends TestCase
3439
protected function setUp(): void
3540
{
3641
$objectManager = new ObjectManager($this);
37-
$methodsMapProcessor = $objectManager->getObject(
42+
$this->methodsMapProcessor = $objectManager->getObject(
3843
MethodsMap::class,
3944
[
4045
'fieldNamer' => $objectManager->getObject(FieldNamer::class),
@@ -48,35 +53,40 @@ protected function setUp(): void
4853
->willReturn(['unserializedData']);
4954

5055
$objectManager->setBackwardCompatibleProperty(
51-
$methodsMapProcessor,
56+
$this->methodsMapProcessor,
5257
'serializer',
5358
$serializerMock
5459
);
5560

5661
$this->extensionAttributesProcessorMock = $this->getMockBuilder(ExtensionAttributesProcessor::class)
5762
->disableOriginalConstructor()
5863
->getMock();
64+
}
65+
66+
/**
67+
* @param array $extensionAttributes
68+
* @param array $excludedMethodsClassMap
69+
* @param array $expectedOutput
70+
* @dataProvider buildOutputDataArrayDataProvider
71+
*/
72+
public function testBuildOutputDataArray(
73+
array $extensionAttributes,
74+
array $excludedMethodsClassMap,
75+
array $expectedOutput
76+
) {
77+
$objectManager = new ObjectManager($this);
5978

6079
$this->dataObjectProcessor = $objectManager->getObject(
6180
DataObjectProcessor::class,
6281
[
63-
'methodsMapProcessor' => $methodsMapProcessor,
82+
'methodsMapProcessor' => $this->methodsMapProcessor,
6483
'typeCaster' => $objectManager->getObject(TypeCaster::class),
6584
'fieldNamer' => $objectManager->getObject(FieldNamer::class),
66-
'extensionAttributesProcessor' => $this->extensionAttributesProcessorMock
85+
'extensionAttributesProcessor' => $this->extensionAttributesProcessorMock,
86+
'excludedMethodsClassMap' => $excludedMethodsClassMap,
6787
]
6888
);
69-
}
7089

71-
/**
72-
* @param array $extensionAttributes
73-
* @param array $expectedOutputDataArray
74-
*
75-
* @dataProvider buildOutputDataArrayDataProvider
76-
*/
77-
public function testBuildOutputDataArray($extensionAttributes, $expectedOutputDataArray)
78-
{
79-
$objectManager = new ObjectManager($this);
8090
/** @var TestDataObject $testDataObject */
8191
$testDataObject = $objectManager->getObject(
8292
TestDataObject::class,
@@ -87,40 +97,70 @@ public function testBuildOutputDataArray($extensionAttributes, $expectedOutputDa
8797
]
8898
);
8999

90-
$this->extensionAttributesProcessorMock->expects($this->once())
100+
if (in_array('getExtensionAttributes', $excludedMethodsClassMap[TestDataInterface::class] ?? [])) {
101+
$expectedTimes = $this->never();
102+
} else {
103+
$expectedTimes = $this->once();
104+
}
105+
106+
$this->extensionAttributesProcessorMock->expects($expectedTimes)
91107
->method('buildOutputDataArray')
92108
->willReturn($extensionAttributes);
93109

94110
$outputData = $this->dataObjectProcessor
95111
->buildOutputDataArray($testDataObject, TestDataInterface::class);
96-
$this->assertEquals($expectedOutputDataArray, $outputData);
112+
$this->assertEquals($expectedOutput, $outputData);
97113
}
98114

99115
/**
100116
* @return array
101117
*/
102118
public function buildOutputDataArrayDataProvider()
103119
{
104-
$expectedOutputDataArray = [
120+
$expectedOutput = [
105121
'id' => '1',
106122
'address' => 'someAddress',
107123
'default_shipping' => 'true',
108124
'required_billing' => 'false',
109125
];
110-
$extensionAttributeArray = [
126+
127+
$extensionAttributes = [
111128
'attribute1' => 'value1',
112-
'attribute2' => 'value2'
129+
'attribute2' => 'value2',
113130
];
114131

115132
return [
116-
'No Attributes' => [[], $expectedOutputDataArray],
117-
'With Attributes' => [
118-
$extensionAttributeArray,
133+
'No Extension Attributes or Excluded Methods' => [
134+
[],
135+
[],
136+
$expectedOutput,
137+
],
138+
'With Extension Attributes' => [
139+
$extensionAttributes,
140+
[],
119141
array_merge(
120-
$expectedOutputDataArray,
121-
['extension_attributes' => $extensionAttributeArray]
122-
)
123-
]
142+
$expectedOutput,
143+
['extension_attributes' => $extensionAttributes]
144+
),
145+
],
146+
'With Excluded Method' => [
147+
[],
148+
[
149+
TestDataInterface::class => [
150+
'getAddress',
151+
],
152+
],
153+
array_diff_key($expectedOutput, array_flip(['address'])),
154+
],
155+
'With getExtensionAttributes as Excluded Method' => [
156+
$extensionAttributes,
157+
[
158+
TestDataInterface::class => [
159+
'getExtensionAttributes',
160+
],
161+
],
162+
$expectedOutput,
163+
],
124164
];
125165
}
126166
}

0 commit comments

Comments
 (0)