Skip to content

Commit b68b98a

Browse files
committed
Merge remote-tracking branch 'mpi/MC-18784' into PR_2019_08_20
2 parents 3ad1158 + eb28168 commit b68b98a

File tree

2 files changed

+137
-46
lines changed

2 files changed

+137
-46
lines changed

app/code/Magento/Catalog/Model/Category/Tree.php

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@
55
*/
66
namespace Magento\Catalog\Model\Category;
77

8+
use Magento\Catalog\Api\Data\CategoryTreeInterface;
9+
use Magento\Catalog\Api\Data\CategoryTreeInterfaceFactory;
10+
use Magento\Catalog\Model\Category;
11+
use Magento\Catalog\Model\ResourceModel\Category\Collection;
12+
use Magento\Catalog\Model\ResourceModel\Category\TreeFactory;
13+
use Magento\Framework\App\ObjectManager;
814
use Magento\Framework\Data\Tree\Node;
15+
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Magento\Store\Model\StoreManagerInterface;
918

1019
/**
1120
* Retrieve category data represented in tree structure
@@ -18,54 +27,54 @@ class Tree
1827
protected $categoryTree;
1928

2029
/**
21-
* @var \Magento\Store\Model\StoreManagerInterface
30+
* @var StoreManagerInterface
2231
*/
2332
protected $storeManager;
2433

2534
/**
26-
* @var \Magento\Catalog\Model\ResourceModel\Category\Collection
35+
* @var Collection
2736
*/
2837
protected $categoryCollection;
2938

3039
/**
31-
* @var \Magento\Catalog\Api\Data\CategoryTreeInterfaceFactory
40+
* @var CategoryTreeInterfaceFactory
3241
*/
3342
protected $treeFactory;
3443

3544
/**
36-
* @var \Magento\Catalog\Model\ResourceModel\Category\TreeFactory
45+
* @var TreeFactory
3746
*/
3847
private $treeResourceFactory;
3948

4049
/**
4150
* @param \Magento\Catalog\Model\ResourceModel\Category\Tree $categoryTree
42-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
43-
* @param \Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection
44-
* @param \Magento\Catalog\Api\Data\CategoryTreeInterfaceFactory $treeFactory
45-
* @param \Magento\Catalog\Model\ResourceModel\Category\TreeFactory|null $treeResourceFactory
51+
* @param StoreManagerInterface $storeManager
52+
* @param Collection $categoryCollection
53+
* @param CategoryTreeInterfaceFactory $treeFactory
54+
* @param TreeFactory|null $treeResourceFactory
4655
*/
4756
public function __construct(
4857
\Magento\Catalog\Model\ResourceModel\Category\Tree $categoryTree,
49-
\Magento\Store\Model\StoreManagerInterface $storeManager,
50-
\Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection,
51-
\Magento\Catalog\Api\Data\CategoryTreeInterfaceFactory $treeFactory,
52-
\Magento\Catalog\Model\ResourceModel\Category\TreeFactory $treeResourceFactory = null
58+
StoreManagerInterface $storeManager,
59+
Collection $categoryCollection,
60+
CategoryTreeInterfaceFactory $treeFactory,
61+
TreeFactory $treeResourceFactory = null
5362
) {
5463
$this->categoryTree = $categoryTree;
5564
$this->storeManager = $storeManager;
5665
$this->categoryCollection = $categoryCollection;
5766
$this->treeFactory = $treeFactory;
58-
$this->treeResourceFactory = $treeResourceFactory ?? \Magento\Framework\App\ObjectManager::getInstance()
59-
->get(\Magento\Catalog\Model\ResourceModel\Category\TreeFactory::class);
67+
$this->treeResourceFactory = $treeResourceFactory ?? ObjectManager::getInstance()
68+
->get(TreeFactory::class);
6069
}
6170

6271
/**
6372
* Get root node by category.
6473
*
65-
* @param \Magento\Catalog\Model\Category|null $category
74+
* @param Category|null $category
6675
* @return Node|null
67-
* @throws \Magento\Framework\Exception\LocalizedException
68-
* @throws \Magento\Framework\Exception\NoSuchEntityException
76+
* @throws LocalizedException
77+
* @throws NoSuchEntityException
6978
*/
7079
public function getRootNode($category = null)
7180
{
@@ -86,28 +95,28 @@ public function getRootNode($category = null)
8695
/**
8796
* Get node by category.
8897
*
89-
* @param \Magento\Catalog\Model\Category $category
98+
* @param Category $category
9099
* @return Node
91-
* @throws \Magento\Framework\Exception\LocalizedException
92-
* @throws \Magento\Framework\Exception\NoSuchEntityException
100+
* @throws LocalizedException
101+
* @throws NoSuchEntityException
93102
*/
94-
protected function getNode(\Magento\Catalog\Model\Category $category)
103+
protected function getNode(Category $category)
95104
{
96105
$nodeId = $category->getId();
97106
$categoryTree = $this->treeResourceFactory->create();
98107
$node = $categoryTree->loadNode($nodeId);
99108
$node->loadChildren();
100109
$this->prepareCollection();
101-
$this->categoryTree->addCollectionData($this->categoryCollection);
110+
$categoryTree->addCollectionData($this->categoryCollection);
102111
return $node;
103112
}
104113

105114
/**
106115
* Prepare category collection.
107116
*
108117
* @return void
109-
* @throws \Magento\Framework\Exception\LocalizedException
110-
* @throws \Magento\Framework\Exception\NoSuchEntityException
118+
* @throws LocalizedException
119+
* @throws NoSuchEntityException
111120
*/
112121
protected function prepareCollection()
113122
{
@@ -128,16 +137,16 @@ protected function prepareCollection()
128137
/**
129138
* Get tree by node.
130139
*
131-
* @param \Magento\Framework\Data\Tree\Node $node
140+
* @param Node $node
132141
* @param int $depth
133142
* @param int $currentLevel
134-
* @return \Magento\Catalog\Api\Data\CategoryTreeInterface
143+
* @return CategoryTreeInterface
135144
*/
136145
public function getTree($node, $depth = null, $currentLevel = 0)
137146
{
138-
/** @var \Magento\Catalog\Api\Data\CategoryTreeInterface[] $children */
147+
/** @var CategoryTreeInterface[] $children */
139148
$children = $this->getChildren($node, $depth, $currentLevel);
140-
/** @var \Magento\Catalog\Api\Data\CategoryTreeInterface $tree */
149+
/** @var CategoryTreeInterface $tree */
141150
$tree = $this->treeFactory->create();
142151
$tree->setId($node->getId())
143152
->setParentId($node->getParentId())
@@ -153,10 +162,10 @@ public function getTree($node, $depth = null, $currentLevel = 0)
153162
/**
154163
* Get node children.
155164
*
156-
* @param \Magento\Framework\Data\Tree\Node $node
165+
* @param Node $node
157166
* @param int $depth
158167
* @param int $currentLevel
159-
* @return \Magento\Catalog\Api\Data\CategoryTreeInterface[]|[]
168+
* @return CategoryTreeInterface[]|[]
160169
*/
161170
protected function getChildren($node, $depth, $currentLevel)
162171
{

dev/tests/api-functional/testsuite/Magento/Catalog/Api/CategoryManagementTest.php

Lines changed: 98 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,23 @@
99

1010
use Magento\TestFramework\TestCase\WebapiAbstract;
1111
use Magento\TestFramework\Helper\Bootstrap;
12-
use Magento\TestFramework\ObjectManager;
1312

13+
/**
14+
* Tests CategoryManagement
15+
*/
1416
class CategoryManagementTest extends WebapiAbstract
1517
{
1618
const RESOURCE_PATH = '/V1/categories';
1719

1820
const SERVICE_NAME = 'catalogCategoryManagementV1';
1921

2022
/**
23+
* Tests getTree operation
24+
*
2125
* @dataProvider treeDataProvider
2226
* @magentoApiDataFixture Magento/Catalog/_files/category_tree.php
2327
*/
24-
public function testTree($rootCategoryId, $depth, $expectedLevel, $expectedId)
28+
public function testTree($rootCategoryId, $depth, $expected)
2529
{
2630
$requestData = ['rootCategoryId' => $rootCategoryId, 'depth' => $depth];
2731
$serviceInfo = [
@@ -36,24 +40,102 @@ public function testTree($rootCategoryId, $depth, $expectedLevel, $expectedId)
3640
]
3741
];
3842
$result = $this->_webApiCall($serviceInfo, $requestData);
39-
40-
for ($i = 0; $i < $expectedLevel; $i++) {
41-
if (!array_key_exists(0, $result['children_data'])) {
42-
$this->fail('Category "' . $result['name'] . '" doesn\'t have children but expected to have');
43-
}
44-
$result = $result['children_data'][0];
45-
}
46-
$this->assertEquals($expectedId, $result['id']);
47-
$this->assertEmpty($result['children_data']);
43+
$expected = array_replace_recursive($result, $expected);
44+
$this->assertEquals($expected, $result);
4845
}
4946

50-
public function treeDataProvider()
47+
/**
48+
* @return array
49+
*/
50+
public function treeDataProvider(): array
5151
{
5252
return [
53-
[2, 100, 3, 402],
54-
[2, null, 3, 402],
55-
[400, 1, 1, 401],
56-
[401, 0, 0, 401],
53+
[
54+
2,
55+
100,
56+
[
57+
'id' => 2,
58+
'name' => 'Default Category',
59+
'children_data' => [
60+
[
61+
'id' => 400,
62+
'name' => 'Category 1',
63+
'children_data' => [
64+
[
65+
'id' => 401,
66+
'name' => 'Category 1.1',
67+
'children_data' => [
68+
[
69+
'id' => 402,
70+
'name' => 'Category 1.1.1',
71+
'children_data' => [
72+
73+
]
74+
]
75+
]
76+
]
77+
]
78+
]
79+
]
80+
]
81+
],
82+
[
83+
2,
84+
null,
85+
[
86+
'id' => 2,
87+
'name' => 'Default Category',
88+
'children_data' => [
89+
[
90+
'id' => 400,
91+
'name' => 'Category 1',
92+
'children_data' => [
93+
[
94+
'id' => 401,
95+
'name' => 'Category 1.1',
96+
'children_data' => [
97+
[
98+
'id' => 402,
99+
'name' => 'Category 1.1.1',
100+
'children_data' => [
101+
102+
]
103+
]
104+
]
105+
]
106+
]
107+
]
108+
]
109+
]
110+
],
111+
[
112+
400,
113+
1,
114+
[
115+
'id' => 400,
116+
'name' => 'Category 1',
117+
'children_data' => [
118+
[
119+
'id' => 401,
120+
'name' => 'Category 1.1',
121+
'children_data' => [
122+
123+
]
124+
]
125+
]
126+
]
127+
],
128+
[
129+
400,
130+
0,
131+
[
132+
'id' => 400,
133+
'name' => 'Category 1',
134+
'children_data' => [
135+
136+
]
137+
]
138+
],
57139
];
58140
}
59141

0 commit comments

Comments
 (0)