Skip to content

Commit dd4493a

Browse files
committed
ACPT-1572: Slow saving of stores/websites
- Extend test coverage;
1 parent 160a120 commit dd4493a

File tree

5 files changed

+345
-9
lines changed

5 files changed

+345
-9
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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\Store\Test\Unit\Model;
9+
10+
use Magento\Config\Model\ResourceModel\Config\Data;
11+
use Magento\Framework\Api\AttributeValueFactory;
12+
use Magento\Framework\Api\ExtensionAttributesFactory;
13+
use Magento\Framework\Data\Collection\AbstractDb;
14+
use Magento\Framework\Event\ManagerInterface;
15+
use Magento\Framework\MessageQueue\PoisonPill\PoisonPillPutInterface;
16+
use Magento\Framework\Model\Context;
17+
use Magento\Framework\Model\ResourceModel\AbstractResource;
18+
use Magento\Framework\Registry;
19+
use Magento\Store\Model\Group;
20+
use Magento\Store\Model\ResourceModel\Store\CollectionFactory;
21+
use Magento\Store\Model\StoreManagerInterface;
22+
use Magento\Store\Model\Validation\StoreValidator;
23+
use PHPUnit\Framework\TestCase;
24+
25+
class GroupTest extends TestCase
26+
{
27+
private Group $model;
28+
29+
private Context $context;
30+
31+
private Registry $registry;
32+
33+
private ExtensionAttributesFactory $extensionFactory;
34+
35+
private AttributeValueFactory $customAttributeFactory;
36+
37+
private Data $configDataResource;
38+
39+
private CollectionFactory $storeListFactory;
40+
41+
private StoreManagerInterface $storeManager;
42+
43+
private AbstractResource $resource;
44+
45+
private AbstractDb $resourceCollection;
46+
47+
private ManagerInterface $eventManager;
48+
49+
private PoisonPillPutInterface $pillPut;
50+
51+
private StoreValidator $modelValidator;
52+
53+
protected function setUp(): void
54+
{
55+
$this->context = $this->getMockBuilder(Context::class)
56+
->disableOriginalConstructor()
57+
->getMock();
58+
59+
$this->registry = $this->getMockBuilder(Registry::class)
60+
->disableOriginalConstructor()
61+
->getMock();
62+
63+
$this->extensionFactory = $this->getMockBuilder(ExtensionAttributesFactory::class)
64+
->disableOriginalConstructor()
65+
->getMock();
66+
67+
$this->customAttributeFactory = $this->getMockBuilder(AttributeValueFactory::class)
68+
->disableOriginalConstructor()
69+
->getMock();
70+
71+
$this->configDataResource = $this->getMockBuilder(Data::class)
72+
->disableOriginalConstructor()
73+
->getMock();
74+
75+
$this->storeListFactory = $this->getMockBuilder(CollectionFactory::class)
76+
->disableOriginalConstructor()
77+
->getMock();
78+
79+
$this->storeManager = $this->getMockForAbstractClass(StoreManagerInterface::class);
80+
81+
$this->resource = $this->getMockBuilder(AbstractResource::class)
82+
->addMethods(['getIdFieldName'])
83+
->disableOriginalConstructor()
84+
->getMockForAbstractClass();
85+
86+
$this->resourceCollection = $this->getMockBuilder(AbstractDb::class)
87+
->disableOriginalConstructor()
88+
->getMock();
89+
90+
$this->eventManager = $this->getMockForAbstractClass(ManagerInterface::class);
91+
92+
$this->pillPut = $this->getMockForAbstractClass(PoisonPillPutInterface::class);
93+
94+
$this->modelValidator = $this->getMockBuilder(StoreValidator::class)
95+
->disableOriginalConstructor()
96+
->getMock();
97+
98+
$this->model = new Group(
99+
$this->context,
100+
$this->registry,
101+
$this->extensionFactory,
102+
$this->customAttributeFactory,
103+
$this->configDataResource,
104+
$this->storeListFactory,
105+
$this->storeManager,
106+
$this->resource,
107+
$this->resourceCollection,
108+
[],
109+
$this->eventManager,
110+
$this->pillPut,
111+
$this->modelValidator
112+
);
113+
}
114+
115+
public function testGetCacheTags()
116+
{
117+
$this->assertEquals([Group::CACHE_TAG], $this->model->getCacheTags());
118+
}
119+
}

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

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@
77

88
namespace Magento\Store\Test\Unit\Model;
99

10+
use Magento\Framework\App\Config;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\Cache\FrontendInterface;
1013
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1114
use Magento\Store\Api\Data\StoreInterface;
15+
use Magento\Store\Api\GroupRepositoryInterface;
1216
use Magento\Store\Api\StoreRepositoryInterface;
1317
use Magento\Store\Api\StoreResolverInterface;
18+
use Magento\Store\Api\WebsiteRepositoryInterface;
19+
use Magento\Store\Model\Group;
20+
use Magento\Store\Model\Store;
1421
use Magento\Store\Model\StoreManager;
22+
use Magento\Store\Model\StoreResolver;
23+
use Magento\Store\Model\Website;
1524
use PHPUnit\Framework\MockObject\MockObject;
1625
use PHPUnit\Framework\TestCase;
1726

@@ -32,6 +41,26 @@ class StoreManagerTest extends TestCase
3241
*/
3342
protected $storeResolverMock;
3443

44+
/**
45+
* @var FrontendInterface|MockObject
46+
*/
47+
private $cache;
48+
49+
/**
50+
* @var GroupRepositoryInterface
51+
*/
52+
private $groupRepository;
53+
54+
/**
55+
* @var WebsiteRepositoryInterface
56+
*/
57+
private $websiteRepository;
58+
59+
/**
60+
* @var ScopeConfigInterface
61+
*/
62+
private $scopeConfig;
63+
3564
protected function setUp(): void
3665
{
3766
$objectManager = new ObjectManager($this);
@@ -43,11 +72,27 @@ protected function setUp(): void
4372
->disableOriginalConstructor()
4473
->setMethods([])
4574
->getMockForAbstractClass();
75+
$this->cache = $this->getMockBuilder(FrontendInterface::class)
76+
->getMockForAbstractClass();
77+
$this->scopeConfig = $this->getMockBuilder(Config::class)
78+
->disableOriginalConstructor()
79+
->getMock();
80+
$this->websiteRepository = $this->getMockBuilder(WebsiteRepositoryInterface::class)
81+
->disableOriginalConstructor()
82+
->getMockForAbstractClass();
83+
$this->groupRepository = $this->getMockBuilder(GroupRepositoryInterface::class)
84+
->disableOriginalConstructor()
85+
->getMockForAbstractClass();
86+
4687
$this->model = $objectManager->getObject(
4788
StoreManager::class,
4889
[
4990
'storeRepository' => $this->storeRepositoryMock,
50-
'storeResolver' => $this->storeResolverMock
91+
'storeResolver' => $this->storeResolverMock,
92+
'cache' => $this->cache,
93+
'scopeConfig' => $this->scopeConfig,
94+
'websiteRepository' => $this->websiteRepository,
95+
'groupRepository' => $this->groupRepository
5196
]
5297
);
5398
}
@@ -95,6 +140,20 @@ public function testGetStoreObjectStoreParameter()
95140
$this->assertEquals($storeMock, $actualStore);
96141
}
97142

143+
public function testReinitStores()
144+
{
145+
$this->cache->expects($this->once())->method('clean')->with(
146+
\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
147+
[StoreResolver::CACHE_TAG, Store::CACHE_TAG, Website::CACHE_TAG, Group::CACHE_TAG]
148+
);
149+
$this->scopeConfig->expects($this->once())->method('clean');
150+
$this->storeRepositoryMock->expects($this->once())->method('clean');
151+
$this->websiteRepository->expects($this->once())->method('clean');
152+
$this->groupRepository->expects($this->once())->method('clean');
153+
154+
$this->model->reinitStores();
155+
}
156+
98157
/**
99158
* @dataProvider getStoresDataProvider
100159
*/

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,11 @@ public function testGetScopeTypeName()
735735
$this->assertEquals('Store View', $this->store->getScopeTypeName());
736736
}
737737

738+
public function testGetCacheTags()
739+
{
740+
$this->assertEquals([Store::CACHE_TAG], $this->store->getCacheTags());
741+
}
742+
738743
/**
739744
* @param array $availableCodes
740745
* @param string $currencyCode

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

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77

88
namespace Magento\Store\Test\Unit\Model;
99

10+
use Magento\Framework\App\Cache\Type\Config;
11+
use Magento\Framework\App\Cache\TypeListInterface;
1012
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use Magento\PageCache\Model\Cache\Type;
1114
use Magento\Store\Model\ResourceModel\Website\Collection;
1215
use Magento\Store\Model\ScopeInterface;
16+
use Magento\Store\Model\StoreManagerInterface;
1317
use Magento\Store\Model\Website;
1418
use Magento\Store\Model\WebsiteFactory;
1519
use PHPUnit\Framework\MockObject\MockObject;
@@ -32,6 +36,16 @@ class WebsiteTest extends TestCase
3236
*/
3337
protected $websiteFactory;
3438

39+
/**
40+
* @var StoreManagerInterface|MockObject
41+
*/
42+
private $storeManager;
43+
44+
/**
45+
* @var TypeListInterface|MockObject
46+
*/
47+
private $typeList;
48+
3549
protected function setUp(): void
3650
{
3751
$this->objectManagerHelper = new ObjectManager($this);
@@ -41,10 +55,17 @@ protected function setUp(): void
4155
->setMethods(['create', 'getCollection', '__wakeup'])
4256
->getMock();
4357

58+
$this->storeManager = $this->getMockForAbstractClass(StoreManagerInterface::class);
59+
$this->typeList = $this->getMockForAbstractClass(TypeListInterface::class);
60+
4461
/** @var Website $websiteModel */
4562
$this->model = $this->objectManagerHelper->getObject(
4663
Website::class,
47-
['websiteFactory' => $this->websiteFactory]
64+
[
65+
'websiteFactory' => $this->websiteFactory,
66+
'storeManager' => $this->storeManager,
67+
'typeList' => $this->typeList
68+
]
4869
);
4970
}
5071

@@ -76,4 +97,43 @@ public function testGetScopeTypeName()
7697
{
7798
$this->assertEquals('Website', $this->model->getScopeTypeName());
7899
}
100+
101+
public function testGetCacheTags()
102+
{
103+
$this->assertEquals([Website::CACHE_TAG], $this->model->getCacheTags());
104+
}
105+
106+
public function testAfterSaveNewObject()
107+
{
108+
$this->storeManager->expects($this->once())
109+
->method('reinitStores');
110+
111+
$this->model->afterSave();
112+
}
113+
114+
public function testAfterSaveObject()
115+
{
116+
$this->model->setId(1);
117+
118+
$this->storeManager->expects($this->never())
119+
->method('reinitStores');
120+
121+
$this->typeList->expects($this->once())
122+
->method('invalidate')
123+
->with([Type::TYPE_IDENTIFIER, Config::TYPE_IDENTIFIER]);
124+
125+
$this->model->afterSave();
126+
}
127+
128+
public function testAfterDelete()
129+
{
130+
$this->typeList->expects($this->exactly(2))
131+
->method('cleanType')
132+
->withConsecutive(
133+
[Type::TYPE_IDENTIFIER],
134+
[Config::TYPE_IDENTIFIER]
135+
);
136+
137+
$this->model->afterDelete();
138+
}
79139
}

0 commit comments

Comments
 (0)