3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
6
7
7
8
namespace Magento \Catalog \Test \Unit \Plugin \Model \ResourceModel ;
8
9
10
+ use Magento \Catalog \Model \ResourceModel \Config as ConfigResourceModel ;
9
11
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 ;
10
16
use Magento \Framework \Serialize \SerializerInterface ;
11
17
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
18
+ use PHPUnit \Framework \MockObject \MockObject ;
19
+ use PHPUnit \Framework \TestCase ;
12
20
13
- class ConfigTest extends \ PHPUnit \ Framework \ TestCase
21
+ class ConfigTest extends TestCase
14
22
{
15
- /** @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */
16
- private $ cache ;
23
+ /**
24
+ * @var CacheInterface|MockObject
25
+ */
26
+ private $ cacheMock ;
17
27
18
- /** @var \Magento\Framework\App\Cache\StateInterface|\PHPUnit_Framework_MockObject_MockObject */
19
- private $ cacheState ;
28
+ /**
29
+ * @var StateInterface|MockObject
30
+ */
31
+ private $ cacheStateMock ;
20
32
21
- /** @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */
22
- private $ serializer ;
33
+ /**
34
+ * @var SerializerInterface|MockObject
35
+ */
36
+ private $ serializerMock ;
23
37
24
- /** @var \Magento\Catalog\Model\ResourceModel\Config|\PHPUnit_Framework_MockObject_MockObject */
25
- private $ subject ;
38
+ /**
39
+ * @var ConfigResourceModel|MockObject
40
+ */
41
+ private $ configResourceModelMock ;
26
42
27
43
protected function setUp ()
28
44
{
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);
33
49
}
34
50
35
51
public function testGetAttributesUsedInListingOnCacheDisabled ()
36
52
{
37
- $ this ->cache ->expects ($ this ->never ())->method ('load ' );
53
+ $ this ->cacheMock ->expects ($ this ->never ())->method ('load ' );
38
54
39
55
$ this ->assertEquals (
40
56
['attributes ' ],
41
57
$ this ->getConfig (false )->aroundGetAttributesUsedInListing (
42
- $ this ->subject ,
58
+ $ this ->configResourceModelMock ,
43
59
$ this ->mockPluginProceed (['attributes ' ])
44
60
)
45
61
);
@@ -51,21 +67,21 @@ public function testGetAttributesUsedInListingFromCache()
51
67
$ storeId = 'store ' ;
52
68
$ attributes = ['attributes ' ];
53
69
$ 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
57
73
. $ entityTypeId
58
74
. '_ ' . $ 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 ())
61
77
->method ('unserialize ' )
62
78
->with ($ serializedAttributes )
63
79
->willReturn ($ attributes );
64
80
65
81
$ this ->assertEquals (
66
82
$ attributes ,
67
83
$ this ->getConfig (true )->aroundGetAttributesUsedInListing (
68
- $ this ->subject ,
84
+ $ this ->configResourceModelMock ,
69
85
$ this ->mockPluginProceed ()
70
86
)
71
87
);
@@ -77,44 +93,44 @@ public function testGetAttributesUsedInListingWithCacheSave()
77
93
$ storeId = 'store ' ;
78
94
$ attributes = ['attributes ' ];
79
95
$ 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
83
99
. $ entityTypeId
84
100
. '_ ' . $ 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 ())
87
103
->method ('unserialize ' );
88
- $ this ->serializer ->expects ($ this ->once ())
104
+ $ this ->serializerMock ->expects ($ this ->once ())
89
105
->method ('serialize ' )
90
106
->with ($ attributes )
91
107
->willReturn ($ serializedAttributes );
92
- $ this ->cache -> expects ( $ this -> any ()) ->method ('save ' )->with (
108
+ $ this ->cacheMock ->method ('save ' )->with (
93
109
$ serializedAttributes ,
94
110
$ cacheId ,
95
111
[
96
- \ Magento \ Eav \ Model \ Cache \ Type::CACHE_TAG ,
97
- \ Magento \ Eav \ Model \ Entity \ Attribute::CACHE_TAG
112
+ Type::CACHE_TAG ,
113
+ Attribute::CACHE_TAG
98
114
]
99
115
);
100
116
101
117
$ this ->assertEquals (
102
118
$ attributes ,
103
119
$ this ->getConfig (true )->aroundGetAttributesUsedInListing (
104
- $ this ->subject ,
120
+ $ this ->configResourceModelMock ,
105
121
$ this ->mockPluginProceed ($ attributes )
106
122
)
107
123
);
108
124
}
109
125
110
126
public function testGetAttributesUsedForSortByOnCacheDisabled ()
111
127
{
112
- $ this ->cache ->expects ($ this ->never ())->method ('load ' );
128
+ $ this ->cacheMock ->expects ($ this ->never ())->method ('load ' );
113
129
114
130
$ this ->assertEquals (
115
131
['attributes ' ],
116
132
$ this ->getConfig (false )->aroundGetAttributesUsedForSortBy (
117
- $ this ->subject ,
133
+ $ this ->configResourceModelMock ,
118
134
$ this ->mockPluginProceed (['attributes ' ])
119
135
)
120
136
);
@@ -126,20 +142,20 @@ public function testGetAttributesUsedForSortByFromCache()
126
142
$ storeId = 'store ' ;
127
143
$ attributes = ['attributes ' ];
128
144
$ 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
132
148
. $ 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 ())
135
151
->method ('unserialize ' )
136
152
->with ($ serializedAttributes )
137
153
->willReturn ($ attributes );
138
154
139
155
$ this ->assertEquals (
140
156
$ attributes ,
141
157
$ this ->getConfig (true )->aroundGetAttributesUsedForSortBy (
142
- $ this ->subject ,
158
+ $ this ->configResourceModelMock ,
143
159
$ this ->mockPluginProceed ()
144
160
)
145
161
);
@@ -151,60 +167,64 @@ public function testGetAttributesUsedForSortByWithCacheSave()
151
167
$ storeId = 'store ' ;
152
168
$ attributes = ['attributes ' ];
153
169
$ 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
157
173
. $ 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 ())
160
176
->method ('unserialize ' );
161
- $ this ->serializer ->expects ($ this ->once ())
177
+ $ this ->serializerMock ->expects ($ this ->once ())
162
178
->method ('serialize ' )
163
179
->with ($ attributes )
164
180
->willReturn ($ serializedAttributes );
165
- $ this ->cache -> expects ( $ this -> any ()) ->method ('save ' )->with (
181
+ $ this ->cacheMock ->method ('save ' )->with (
166
182
$ serializedAttributes ,
167
183
$ cacheId ,
168
184
[
169
- \ Magento \ Eav \ Model \ Cache \ Type::CACHE_TAG ,
170
- \ Magento \ Eav \ Model \ Entity \ Attribute::CACHE_TAG
185
+ Type::CACHE_TAG ,
186
+ Attribute::CACHE_TAG
171
187
]
172
188
);
173
189
174
190
$ this ->assertEquals (
175
191
$ attributes ,
176
192
$ this ->getConfig (true )->aroundGetAttributesUsedForSortBy (
177
- $ this ->subject ,
193
+ $ this ->configResourceModelMock ,
178
194
$ this ->mockPluginProceed ($ attributes )
179
195
)
180
196
);
181
197
}
182
198
183
199
/**
184
200
* @param bool $cacheEnabledFlag
185
- * @return \Magento\Catalog\Plugin\Model\ResourceModel\Config
201
+ *
202
+ * @return Config
186
203
*/
187
204
protected function getConfig ($ cacheEnabledFlag )
188
205
{
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
+
191
210
return (new ObjectManager ($ this ))->getObject (
192
- \ Magento \ Catalog \ Plugin \ Model \ ResourceModel \ Config::class,
211
+ Config::class,
193
212
[
194
- 'cache ' => $ this ->cache ,
195
- 'cacheState ' => $ this ->cacheState ,
196
- 'serializer ' => $ this ->serializer ,
213
+ 'cache ' => $ this ->cacheMock ,
214
+ 'cacheState ' => $ this ->cacheStateMock ,
215
+ 'serializer ' => $ this ->serializerMock ,
197
216
]
198
217
);
199
218
}
200
219
201
220
/**
202
221
* @param mixed $returnValue
222
+ *
203
223
* @return callable
204
224
*/
205
225
protected function mockPluginProceed ($ returnValue = null )
206
226
{
207
- return function () use ($ returnValue ) {
227
+ return static function () use ($ returnValue ) {
208
228
return $ returnValue ;
209
229
};
210
230
}
0 commit comments