Skip to content

Commit 5c2fd11

Browse files
committed
Merge remote-tracking branch 'magento-tango/MC-18070' into PR23-20190718
2 parents 7aed331 + db81564 commit 5c2fd11

File tree

2 files changed

+97
-45
lines changed

2 files changed

+97
-45
lines changed

app/code/Magento/Store/Model/ScopeTreeProvider.php

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,43 @@
77

88
use Magento\Framework\App\Config\ScopeConfigInterface;
99
use Magento\Framework\App\ScopeTreeProviderInterface;
10-
use Magento\Store\Model\Group;
11-
use Magento\Store\Model\Store;
12-
use Magento\Store\Model\Website;
10+
use Magento\Store\Api\GroupRepositoryInterface;
11+
use Magento\Store\Api\StoreRepositoryInterface;
12+
use Magento\Store\Api\WebsiteRepositoryInterface;
1313

14+
/**
15+
* Class for building scopes tree.
16+
*/
1417
class ScopeTreeProvider implements ScopeTreeProviderInterface
1518
{
1619
/**
17-
* @var StoreManagerInterface
20+
* @var WebsiteRepositoryInterface
21+
*/
22+
private $websiteRepository;
23+
24+
/**
25+
* @var GroupRepositoryInterface
26+
*/
27+
private $groupRepository;
28+
29+
/**
30+
* @var StoreRepositoryInterface
1831
*/
19-
protected $storeManager;
32+
private $storeRepository;
2033

2134
/**
22-
* @param StoreManagerInterface $storeManager
35+
* @param WebsiteRepositoryInterface $websiteRepository
36+
* @param GroupRepositoryInterface $groupRepository
37+
* @param StoreRepositoryInterface $storeRepository
2338
*/
2439
public function __construct(
25-
StoreManagerInterface $storeManager
40+
WebsiteRepositoryInterface $websiteRepository,
41+
GroupRepositoryInterface $groupRepository,
42+
StoreRepositoryInterface $storeRepository
2643
) {
27-
$this->storeManager = $storeManager;
44+
$this->websiteRepository = $websiteRepository;
45+
$this->groupRepository = $groupRepository;
46+
$this->storeRepository = $storeRepository;
2847
}
2948

3049
/**
@@ -38,24 +57,37 @@ public function get()
3857
'scopes' => [],
3958
];
4059

60+
$groups = [];
61+
foreach ($this->groupRepository->getList() as $group) {
62+
$groups[$group->getWebsiteId()][] = $group;
63+
}
64+
$stores = [];
65+
foreach ($this->storeRepository->getList() as $store) {
66+
$stores[$store->getStoreGroupId()][] = $store;
67+
}
68+
4169
/** @var Website $website */
42-
foreach ($this->storeManager->getWebsites() as $website) {
70+
foreach ($this->websiteRepository->getList() as $website) {
71+
if (!$website->getId()) {
72+
continue;
73+
}
74+
4375
$websiteScope = [
4476
'scope' => ScopeInterface::SCOPE_WEBSITES,
4577
'scope_id' => $website->getId(),
4678
'scopes' => [],
4779
];
4880

4981
/** @var Group $group */
50-
foreach ($website->getGroups() as $group) {
82+
foreach ($groups[$website->getId()] as $group) {
5183
$groupScope = [
5284
'scope' => ScopeInterface::SCOPE_GROUP,
5385
'scope_id' => $group->getId(),
5486
'scopes' => [],
5587
];
5688

5789
/** @var Store $store */
58-
foreach ($group->getStores() as $store) {
90+
foreach ($stores[$group->getId()] as $store) {
5991
$storeScope = [
6092
'scope' => ScopeInterface::SCOPE_STORES,
6193
'scope_id' => $store->getId(),
@@ -67,6 +99,7 @@ public function get()
6799
}
68100
$defaultScope['scopes'][] = $websiteScope;
69101
}
102+
70103
return $defaultScope;
71104
}
72105
}

app/code/Magento/Store/Test/Unit/Model/ScopeTreeProviderTest.php

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
*/
66
namespace Magento\Store\Test\Unit\Model;
77

8-
use Magento\Store\Api\Data\WebsiteInterface;
98
use Magento\Store\Api\Data\GroupInterface;
109
use Magento\Store\Api\Data\StoreInterface;
10+
use Magento\Store\Api\Data\WebsiteInterface;
11+
use Magento\Store\Api\GroupRepositoryInterface;
12+
use Magento\Store\Api\StoreRepositoryInterface;
13+
use Magento\Store\Api\WebsiteRepositoryInterface;
1114
use Magento\Store\Model\Group;
1215
use Magento\Store\Model\ScopeTreeProvider;
1316
use Magento\Store\Model\Store;
@@ -16,20 +19,42 @@
1619
use Magento\Framework\App\Config\ScopeConfigInterface;
1720
use Magento\Store\Model\Website;
1821

22+
/**
23+
* @covers \Magento\Store\Model\ScopeTreeProvider
24+
*/
1925
class ScopeTreeProviderTest extends \PHPUnit\Framework\TestCase
2026
{
21-
/** @var ScopeTreeProvider */
22-
protected $model;
27+
/**
28+
* @var ScopeTreeProvider
29+
*/
30+
private $model;
2331

24-
/** @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
25-
protected $storeManagerMock;
32+
/**
33+
* @var \PHPUnit_Framework_MockObject_MockObject|WebsiteRepositoryInterface
34+
*/
35+
private $websiteRepositoryMock;
36+
37+
/**
38+
* @var \PHPUnit_Framework_MockObject_MockObject|GroupRepositoryInterface
39+
*/
40+
private $groupRepositoryMock;
41+
42+
/**
43+
* @var \PHPUnit_Framework_MockObject_MockObject|StoreRepositoryInterface
44+
*/
45+
private $storeRepositoryMock;
2646

2747
protected function setUp()
2848
{
29-
$this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
30-
->getMockForAbstractClass();
49+
$this->websiteRepositoryMock = $this->createMock(WebsiteRepositoryInterface::class);
50+
$this->groupRepositoryMock = $this->createMock(GroupRepositoryInterface::class);
51+
$this->storeRepositoryMock = $this->createMock(StoreRepositoryInterface::class);
3152

32-
$this->model = new ScopeTreeProvider($this->storeManagerMock);
53+
$this->model = new ScopeTreeProvider(
54+
$this->websiteRepositoryMock,
55+
$this->groupRepositoryMock,
56+
$this->storeRepositoryMock
57+
);
3358
}
3459

3560
public function testGet()
@@ -58,40 +83,34 @@ public function testGet()
5883
'scopes' => [$websiteData],
5984
];
6085

61-
/** @var Website|\PHPUnit_Framework_MockObject_MockObject $websiteMock */
62-
$websiteMock = $this->getMockBuilder(\Magento\Store\Model\Website::class)
63-
->disableOriginalConstructor()
64-
->getMock();
65-
$websiteMock->expects($this->any())
86+
$websiteMock = $this->createMock(WebsiteInterface::class);
87+
$websiteMock->expects($this->atLeastOnce())
6688
->method('getId')
6789
->willReturn($websiteId);
90+
$this->websiteRepositoryMock->expects($this->once())
91+
->method('getList')
92+
->willReturn([$websiteMock]);
6893

69-
/** @var Group|\PHPUnit_Framework_MockObject_MockObject $groupMock */
70-
$groupMock = $this->getMockBuilder(\Magento\Store\Model\Group::class)
71-
->disableOriginalConstructor()
72-
->getMock();
73-
$groupMock->expects($this->any())
94+
$groupMock = $this->createMock(GroupInterface::class);
95+
$groupMock->expects($this->atLeastOnce())
7496
->method('getId')
7597
->willReturn($groupId);
98+
$groupMock->expects($this->atLeastOnce())
99+
->method('getWebsiteId')
100+
->willReturn($websiteId);
101+
$this->groupRepositoryMock->expects($this->once())
102+
->method('getList')
103+
->willReturn([$groupMock, $groupMock]);
76104

77-
/** @var Store|\PHPUnit_Framework_MockObject_MockObject $storeMock */
78-
$storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
79-
->disableOriginalConstructor()
80-
->getMock();
81-
$storeMock->expects($this->any())
105+
$storeMock = $this->createMock(StoreInterface::class);
106+
$storeMock->expects($this->atLeastOnce())
82107
->method('getId')
83108
->willReturn($storeId);
84-
85-
$this->storeManagerMock->expects($this->any())
86-
->method('getWebsites')
87-
->willReturn([$websiteMock]);
88-
89-
$websiteMock->expects($this->any())
90-
->method('getGroups')
91-
->willReturn([$groupMock, $groupMock]);
92-
93-
$groupMock->expects($this->any())
94-
->method('getStores')
109+
$storeMock->expects($this->atLeastOnce())
110+
->method('getStoreGroupId')
111+
->willReturn($groupId);
112+
$this->storeRepositoryMock->expects($this->once())
113+
->method('getList')
95114
->willReturn([$storeMock, $storeMock, $storeMock]);
96115

97116
$this->assertEquals($result, $this->model->get());

0 commit comments

Comments
 (0)