Skip to content

Commit b32113c

Browse files
ENGCOM-7139: Cleanup ObjectManager usage - Magento_Catalog ViewModel,Plugin #27319
- Merge Pull Request #27319 from Bartlomiejsz/magento2:feature/objectmanager_cleanup_catalog_1 - Merged commits: 1. d634cac
2 parents ecaa3b7 + d634cac commit b32113c

File tree

4 files changed

+155
-136
lines changed

4 files changed

+155
-136
lines changed

app/code/Magento/Catalog/Plugin/Model/ResourceModel/Config.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Catalog\Plugin\Model\ResourceModel;
79

8-
use Magento\Framework\App\ObjectManager;
10+
use Magento\Eav\Model\Cache\Type;
11+
use Magento\Eav\Model\Entity\Attribute;
12+
use Magento\Framework\App\Cache\StateInterface;
13+
use Magento\Framework\App\CacheInterface;
914
use Magento\Framework\Serialize\SerializerInterface;
1015

1116
/**
@@ -21,43 +26,43 @@ class Config
2126
/**#@-*/
2227

2328
/**#@-*/
24-
protected $cache;
29+
private $cache;
2530

2631
/**
27-
* @var bool|null
32+
* @var bool
2833
*/
29-
protected $isCacheEnabled = null;
34+
private $isCacheEnabled;
3035

3136
/**
3237
* @var SerializerInterface
3338
*/
3439
private $serializer;
3540

3641
/**
37-
* @param \Magento\Framework\App\CacheInterface $cache
38-
* @param \Magento\Framework\App\Cache\StateInterface $cacheState
42+
* @param CacheInterface $cache
43+
* @param StateInterface $cacheState
3944
* @param SerializerInterface $serializer
4045
*/
4146
public function __construct(
42-
\Magento\Framework\App\CacheInterface $cache,
43-
\Magento\Framework\App\Cache\StateInterface $cacheState,
44-
SerializerInterface $serializer = null
47+
CacheInterface $cache,
48+
StateInterface $cacheState,
49+
SerializerInterface $serializer
4550
) {
4651
$this->cache = $cache;
47-
$this->isCacheEnabled = $cacheState->isEnabled(\Magento\Eav\Model\Cache\Type::TYPE_IDENTIFIER);
48-
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
52+
$this->isCacheEnabled = $cacheState->isEnabled(Type::TYPE_IDENTIFIER);
53+
$this->serializer = $serializer;
4954
}
5055

5156
/**
5257
* Cache attribute used in listing.
5358
*
5459
* @param \Magento\Catalog\Model\ResourceModel\Config $config
55-
* @param \Closure $proceed
60+
* @param callable $proceed
5661
* @return array
5762
*/
5863
public function aroundGetAttributesUsedInListing(
5964
\Magento\Catalog\Model\ResourceModel\Config $config,
60-
\Closure $proceed
65+
callable $proceed
6166
) {
6267
$cacheId = self::PRODUCT_LISTING_ATTRIBUTES_CACHE_ID . $config->getEntityTypeId() . '_' . $config->getStoreId();
6368
if ($this->isCacheEnabled && ($attributes = $this->cache->load($cacheId))) {
@@ -69,8 +74,8 @@ public function aroundGetAttributesUsedInListing(
6974
$this->serializer->serialize($attributes),
7075
$cacheId,
7176
[
72-
\Magento\Eav\Model\Cache\Type::CACHE_TAG,
73-
\Magento\Eav\Model\Entity\Attribute::CACHE_TAG
77+
Type::CACHE_TAG,
78+
Attribute::CACHE_TAG
7479
]
7580
);
7681
}
@@ -81,12 +86,12 @@ public function aroundGetAttributesUsedInListing(
8186
* Cache attributes used for sorting.
8287
*
8388
* @param \Magento\Catalog\Model\ResourceModel\Config $config
84-
* @param \Closure $proceed
89+
* @param callable $proceed
8590
* @return array
8691
*/
8792
public function aroundGetAttributesUsedForSortBy(
8893
\Magento\Catalog\Model\ResourceModel\Config $config,
89-
\Closure $proceed
94+
callable $proceed
9095
) {
9196
$cacheId = self::PRODUCT_LISTING_SORT_BY_ATTRIBUTES_CACHE_ID . $config->getEntityTypeId() . '_'
9297
. $config->getStoreId();
@@ -99,8 +104,8 @@ public function aroundGetAttributesUsedForSortBy(
99104
$this->serializer->serialize($attributes),
100105
$cacheId,
101106
[
102-
\Magento\Eav\Model\Cache\Type::CACHE_TAG,
103-
\Magento\Eav\Model\Entity\Attribute::CACHE_TAG
107+
Type::CACHE_TAG,
108+
Attribute::CACHE_TAG
104109
]
105110
);
106111
}

app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/ConfigTest.php

Lines changed: 77 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,59 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Catalog\Test\Unit\Plugin\Model\ResourceModel;
89

10+
use Magento\Catalog\Model\ResourceModel\Config as ConfigResourceModel;
911
use Magento\Catalog\Plugin\Model\ResourceModel\Config;
12+
use Magento\Eav\Model\Cache\Type;
13+
use Magento\Eav\Model\Entity\Attribute;
14+
use Magento\Framework\App\Cache\StateInterface;
15+
use Magento\Framework\App\CacheInterface;
1016
use Magento\Framework\Serialize\SerializerInterface;
1117
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
18+
use PHPUnit\Framework\MockObject\MockObject;
19+
use PHPUnit\Framework\TestCase;
1220

13-
class ConfigTest extends \PHPUnit\Framework\TestCase
21+
class ConfigTest extends TestCase
1422
{
15-
/** @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */
16-
private $cache;
23+
/**
24+
* @var CacheInterface|MockObject
25+
*/
26+
private $cacheMock;
1727

18-
/** @var \Magento\Framework\App\Cache\StateInterface|\PHPUnit_Framework_MockObject_MockObject */
19-
private $cacheState;
28+
/**
29+
* @var StateInterface|MockObject
30+
*/
31+
private $cacheStateMock;
2032

21-
/** @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */
22-
private $serializer;
33+
/**
34+
* @var SerializerInterface|MockObject
35+
*/
36+
private $serializerMock;
2337

24-
/** @var \Magento\Catalog\Model\ResourceModel\Config|\PHPUnit_Framework_MockObject_MockObject */
25-
private $subject;
38+
/**
39+
* @var ConfigResourceModel|MockObject
40+
*/
41+
private $configResourceModelMock;
2642

2743
protected function setUp()
2844
{
29-
$this->cache = $this->createMock(\Magento\Framework\App\CacheInterface::class);
30-
$this->cacheState = $this->createMock(\Magento\Framework\App\Cache\StateInterface::class);
31-
$this->serializer = $this->createMock(SerializerInterface::class);
32-
$this->subject = $this->createMock(\Magento\Catalog\Model\ResourceModel\Config::class);
45+
$this->cacheMock = $this->createMock(CacheInterface::class);
46+
$this->cacheStateMock = $this->createMock(StateInterface::class);
47+
$this->serializerMock = $this->createMock(SerializerInterface::class);
48+
$this->configResourceModelMock = $this->createMock(ConfigResourceModel::class);
3349
}
3450

3551
public function testGetAttributesUsedInListingOnCacheDisabled()
3652
{
37-
$this->cache->expects($this->never())->method('load');
53+
$this->cacheMock->expects($this->never())->method('load');
3854

3955
$this->assertEquals(
4056
['attributes'],
4157
$this->getConfig(false)->aroundGetAttributesUsedInListing(
42-
$this->subject,
58+
$this->configResourceModelMock,
4359
$this->mockPluginProceed(['attributes'])
4460
)
4561
);
@@ -51,21 +67,21 @@ public function testGetAttributesUsedInListingFromCache()
5167
$storeId = 'store';
5268
$attributes = ['attributes'];
5369
$serializedAttributes = '["attributes"]';
54-
$this->subject->expects($this->any())->method('getEntityTypeId')->willReturn($entityTypeId);
55-
$this->subject->expects($this->any())->method('getStoreId')->willReturn($storeId);
56-
$cacheId = \Magento\Catalog\Plugin\Model\ResourceModel\Config::PRODUCT_LISTING_ATTRIBUTES_CACHE_ID
70+
$this->configResourceModelMock->method('getEntityTypeId')->willReturn($entityTypeId);
71+
$this->configResourceModelMock->method('getStoreId')->willReturn($storeId);
72+
$cacheId = Config::PRODUCT_LISTING_ATTRIBUTES_CACHE_ID
5773
. $entityTypeId
5874
. '_' . $storeId;
59-
$this->cache->expects($this->any())->method('load')->with($cacheId)->willReturn($serializedAttributes);
60-
$this->serializer->expects($this->once())
75+
$this->cacheMock->method('load')->with($cacheId)->willReturn($serializedAttributes);
76+
$this->serializerMock->expects($this->once())
6177
->method('unserialize')
6278
->with($serializedAttributes)
6379
->willReturn($attributes);
6480

6581
$this->assertEquals(
6682
$attributes,
6783
$this->getConfig(true)->aroundGetAttributesUsedInListing(
68-
$this->subject,
84+
$this->configResourceModelMock,
6985
$this->mockPluginProceed()
7086
)
7187
);
@@ -77,44 +93,44 @@ public function testGetAttributesUsedInListingWithCacheSave()
7793
$storeId = 'store';
7894
$attributes = ['attributes'];
7995
$serializedAttributes = '["attributes"]';
80-
$this->subject->expects($this->any())->method('getEntityTypeId')->willReturn($entityTypeId);
81-
$this->subject->expects($this->any())->method('getStoreId')->willReturn($storeId);
82-
$cacheId = \Magento\Catalog\Plugin\Model\ResourceModel\Config::PRODUCT_LISTING_ATTRIBUTES_CACHE_ID
96+
$this->configResourceModelMock->method('getEntityTypeId')->willReturn($entityTypeId);
97+
$this->configResourceModelMock->method('getStoreId')->willReturn($storeId);
98+
$cacheId = Config::PRODUCT_LISTING_ATTRIBUTES_CACHE_ID
8399
. $entityTypeId
84100
. '_' . $storeId;
85-
$this->cache->expects($this->any())->method('load')->with($cacheId)->willReturn(false);
86-
$this->serializer->expects($this->never())
101+
$this->cacheMock->method('load')->with($cacheId)->willReturn(false);
102+
$this->serializerMock->expects($this->never())
87103
->method('unserialize');
88-
$this->serializer->expects($this->once())
104+
$this->serializerMock->expects($this->once())
89105
->method('serialize')
90106
->with($attributes)
91107
->willReturn($serializedAttributes);
92-
$this->cache->expects($this->any())->method('save')->with(
108+
$this->cacheMock->method('save')->with(
93109
$serializedAttributes,
94110
$cacheId,
95111
[
96-
\Magento\Eav\Model\Cache\Type::CACHE_TAG,
97-
\Magento\Eav\Model\Entity\Attribute::CACHE_TAG
112+
Type::CACHE_TAG,
113+
Attribute::CACHE_TAG
98114
]
99115
);
100116

101117
$this->assertEquals(
102118
$attributes,
103119
$this->getConfig(true)->aroundGetAttributesUsedInListing(
104-
$this->subject,
120+
$this->configResourceModelMock,
105121
$this->mockPluginProceed($attributes)
106122
)
107123
);
108124
}
109125

110126
public function testGetAttributesUsedForSortByOnCacheDisabled()
111127
{
112-
$this->cache->expects($this->never())->method('load');
128+
$this->cacheMock->expects($this->never())->method('load');
113129

114130
$this->assertEquals(
115131
['attributes'],
116132
$this->getConfig(false)->aroundGetAttributesUsedForSortBy(
117-
$this->subject,
133+
$this->configResourceModelMock,
118134
$this->mockPluginProceed(['attributes'])
119135
)
120136
);
@@ -126,20 +142,20 @@ public function testGetAttributesUsedForSortByFromCache()
126142
$storeId = 'store';
127143
$attributes = ['attributes'];
128144
$serializedAttributes = '["attributes"]';
129-
$this->subject->expects($this->any())->method('getEntityTypeId')->willReturn($entityTypeId);
130-
$this->subject->expects($this->any())->method('getStoreId')->willReturn($storeId);
131-
$cacheId = \Magento\Catalog\Plugin\Model\ResourceModel\Config::PRODUCT_LISTING_SORT_BY_ATTRIBUTES_CACHE_ID
145+
$this->configResourceModelMock->method('getEntityTypeId')->willReturn($entityTypeId);
146+
$this->configResourceModelMock->method('getStoreId')->willReturn($storeId);
147+
$cacheId = Config::PRODUCT_LISTING_SORT_BY_ATTRIBUTES_CACHE_ID
132148
. $entityTypeId . '_' . $storeId;
133-
$this->cache->expects($this->any())->method('load')->with($cacheId)->willReturn($serializedAttributes);
134-
$this->serializer->expects($this->once())
149+
$this->cacheMock->method('load')->with($cacheId)->willReturn($serializedAttributes);
150+
$this->serializerMock->expects($this->once())
135151
->method('unserialize')
136152
->with($serializedAttributes)
137153
->willReturn($attributes);
138154

139155
$this->assertEquals(
140156
$attributes,
141157
$this->getConfig(true)->aroundGetAttributesUsedForSortBy(
142-
$this->subject,
158+
$this->configResourceModelMock,
143159
$this->mockPluginProceed()
144160
)
145161
);
@@ -151,60 +167,64 @@ public function testGetAttributesUsedForSortByWithCacheSave()
151167
$storeId = 'store';
152168
$attributes = ['attributes'];
153169
$serializedAttributes = '["attributes"]';
154-
$this->subject->expects($this->any())->method('getEntityTypeId')->willReturn($entityTypeId);
155-
$this->subject->expects($this->any())->method('getStoreId')->willReturn($storeId);
156-
$cacheId = \Magento\Catalog\Plugin\Model\ResourceModel\Config::PRODUCT_LISTING_SORT_BY_ATTRIBUTES_CACHE_ID
170+
$this->configResourceModelMock->method('getEntityTypeId')->willReturn($entityTypeId);
171+
$this->configResourceModelMock->method('getStoreId')->willReturn($storeId);
172+
$cacheId = Config::PRODUCT_LISTING_SORT_BY_ATTRIBUTES_CACHE_ID
157173
. $entityTypeId . '_' . $storeId;
158-
$this->cache->expects($this->any())->method('load')->with($cacheId)->willReturn(false);
159-
$this->serializer->expects($this->never())
174+
$this->cacheMock->method('load')->with($cacheId)->willReturn(false);
175+
$this->serializerMock->expects($this->never())
160176
->method('unserialize');
161-
$this->serializer->expects($this->once())
177+
$this->serializerMock->expects($this->once())
162178
->method('serialize')
163179
->with($attributes)
164180
->willReturn($serializedAttributes);
165-
$this->cache->expects($this->any())->method('save')->with(
181+
$this->cacheMock->method('save')->with(
166182
$serializedAttributes,
167183
$cacheId,
168184
[
169-
\Magento\Eav\Model\Cache\Type::CACHE_TAG,
170-
\Magento\Eav\Model\Entity\Attribute::CACHE_TAG
185+
Type::CACHE_TAG,
186+
Attribute::CACHE_TAG
171187
]
172188
);
173189

174190
$this->assertEquals(
175191
$attributes,
176192
$this->getConfig(true)->aroundGetAttributesUsedForSortBy(
177-
$this->subject,
193+
$this->configResourceModelMock,
178194
$this->mockPluginProceed($attributes)
179195
)
180196
);
181197
}
182198

183199
/**
184200
* @param bool $cacheEnabledFlag
185-
* @return \Magento\Catalog\Plugin\Model\ResourceModel\Config
201+
*
202+
* @return Config
186203
*/
187204
protected function getConfig($cacheEnabledFlag)
188205
{
189-
$this->cacheState->expects($this->any())->method('isEnabled')
190-
->with(\Magento\Eav\Model\Cache\Type::TYPE_IDENTIFIER)->willReturn($cacheEnabledFlag);
206+
$this->cacheStateMock->method('isEnabled')
207+
->with(Type::TYPE_IDENTIFIER)
208+
->willReturn($cacheEnabledFlag);
209+
191210
return (new ObjectManager($this))->getObject(
192-
\Magento\Catalog\Plugin\Model\ResourceModel\Config::class,
211+
Config::class,
193212
[
194-
'cache' => $this->cache,
195-
'cacheState' => $this->cacheState,
196-
'serializer' => $this->serializer,
213+
'cache' => $this->cacheMock,
214+
'cacheState' => $this->cacheStateMock,
215+
'serializer' => $this->serializerMock,
197216
]
198217
);
199218
}
200219

201220
/**
202221
* @param mixed $returnValue
222+
*
203223
* @return callable
204224
*/
205225
protected function mockPluginProceed($returnValue = null)
206226
{
207-
return function () use ($returnValue) {
227+
return static function () use ($returnValue) {
208228
return $returnValue;
209229
};
210230
}

0 commit comments

Comments
 (0)