33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
67
78namespace Magento \Catalog \Test \Unit \Plugin \Model \ResourceModel ;
89
10+ use Magento \Catalog \Model \ResourceModel \Config as ConfigResourceModel ;
911use 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 ;
1016use Magento \Framework \Serialize \SerializerInterface ;
1117use 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