77
88namespace Magento \Store \Test \Unit \Block ;
99
10+ use Magento \Directory \Helper \Data ;
11+ use Magento \Framework \App \Config \ScopeConfigInterface ;
1012use Magento \Framework \Data \Helper \PostHelper ;
1113use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
1214use Magento \Framework \UrlInterface ;
1315use Magento \Framework \View \Element \Template \Context ;
1416use Magento \Store \Api \Data \StoreInterface ;
1517use Magento \Store \Block \Switcher ;
18+ use Magento \Store \Model \ScopeInterface ;
1619use Magento \Store \Model \Store ;
1720use Magento \Store \Model \StoreManagerInterface ;
21+ use Magento \Store \Model \Website ;
1822use PHPUnit \Framework \MockObject \MockObject ;
1923use PHPUnit \Framework \TestCase ;
2024
25+ /**
26+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+ */
2128class SwitcherTest extends TestCase
2229{
23- /** @var Switcher */
24- protected $ switcher ;
25-
26- /** @var Context|MockObject */
27- protected $ context ;
30+ /**
31+ * @var Switcher
32+ */
33+ private $ switcher ;
2834
29- /** @var PostHelper|MockObject */
30- protected $ corePostDataHelper ;
35+ /**
36+ * @var PostHelper|MockObject
37+ */
38+ private $ corePostDataHelperMock ;
3139
32- /** @var StoreManagerInterface|MockObject */
33- protected $ storeManager ;
40+ /**
41+ * @var StoreManagerInterface|MockObject
42+ */
43+ private $ storeManagerMock ;
3444
35- /** @var UrlInterface|MockObject */
36- protected $ urlBuilder ;
45+ /**
46+ * @var UrlInterface|MockObject
47+ */
48+ private $ urlBuilderMock ;
3749
38- /** @var StoreInterface|MockObject */
39- private $ store ;
50+ /**
51+ * @var ScopeConfigInterface|MockObject
52+ */
53+ private $ scopeConfigMock ;
4054
4155 /**
4256 * @return void
4357 */
4458 protected function setUp (): void
4559 {
46- $ this ->storeManager = $ this ->getMockBuilder (StoreManagerInterface::class)
47- ->getMock ();
48- $ this ->urlBuilder = $ this ->getMockForAbstractClass (UrlInterface::class);
49- $ this ->context = $ this ->createMock (Context::class);
50- $ this ->context ->expects ($ this ->any ())->method ('getStoreManager ' )->willReturn ($ this ->storeManager );
51- $ this ->context ->expects ($ this ->any ())->method ('getUrlBuilder ' )->willReturn ($ this ->urlBuilder );
52- $ this ->corePostDataHelper = $ this ->createMock (PostHelper::class);
53- $ this ->store = $ this ->getMockBuilder (StoreInterface::class)
54- ->disableOriginalConstructor ()
55- ->getMockForAbstractClass ();
60+ $ this ->storeManagerMock = $ this ->getMockBuilder (StoreManagerInterface::class)->getMock ();
61+ $ this ->urlBuilderMock = $ this ->createMock (UrlInterface::class);
62+ $ this ->scopeConfigMock = $ this ->createMock (ScopeConfigInterface::class);
63+ $ contextMock = $ this ->createMock (Context::class);
64+ $ contextMock ->method ('getStoreManager ' )->willReturn ($ this ->storeManagerMock );
65+ $ contextMock ->method ('getUrlBuilder ' )->willReturn ($ this ->urlBuilderMock );
66+ $ contextMock ->method ('getScopeConfig ' )->willReturn ($ this ->scopeConfigMock );
67+ $ this ->corePostDataHelperMock = $ this ->createMock (PostHelper::class);
5668 $ this ->switcher = (new ObjectManager ($ this ))->getObject (
5769 Switcher::class,
5870 [
59- 'context ' => $ this -> context ,
60- 'postDataHelper ' => $ this ->corePostDataHelper ,
71+ 'context ' => $ contextMock ,
72+ 'postDataHelper ' => $ this ->corePostDataHelperMock ,
6173 ]
6274 );
6375 }
6476
77+ public function testGetStoresSortOrder ()
78+ {
79+ $ groupId = 1 ;
80+ $ storesSortOrder = [
81+ 1 => 2 ,
82+ 2 => 4 ,
83+ 3 => 1 ,
84+ 4 => 3
85+ ];
86+
87+ $ currentStoreMock = $ this ->getMockBuilder (Store::class)
88+ ->disableOriginalConstructor ()
89+ ->getMock ();
90+ $ currentStoreMock ->method ('getGroupId ' )->willReturn ($ groupId );
91+ $ currentStoreMock ->method ('isUseStoreInUrl ' )->willReturn (false );
92+ $ this ->storeManagerMock ->method ('getStore ' )
93+ ->willReturn ($ currentStoreMock );
94+
95+ $ currentWebsiteMock = $ this ->getMockBuilder (Website::class)
96+ ->disableOriginalConstructor ()
97+ ->getMock ();
98+ $ this ->storeManagerMock ->method ('getWebsite ' )
99+ ->willReturn ($ currentWebsiteMock );
100+
101+ $ stores = [];
102+ foreach ($ storesSortOrder as $ storeId => $ sortOrder ) {
103+ $ storeMock = $ this ->getMockBuilder (Store::class)
104+ ->disableOriginalConstructor ()
105+ ->setMethods (['getId ' , 'getGroupId ' , 'getSortOrder ' , 'isActive ' , 'getUrl ' ])
106+ ->getMock ();
107+ $ storeMock ->method ('getId ' )->willReturn ($ storeId );
108+ $ storeMock ->method ('getGroupId ' )->willReturn ($ groupId );
109+ $ storeMock ->method ('getSortOrder ' )->willReturn ($ sortOrder );
110+ $ storeMock ->method ('isActive ' )->willReturn (true );
111+ $ storeMock ->method ('getUrl ' )->willReturn ('https://example.org ' );
112+ $ stores [] = $ storeMock ;
113+ }
114+
115+ $ scopeConfigMap = array_map (static function ($ item ) {
116+ return [
117+ Data::XML_PATH_DEFAULT_LOCALE ,
118+ ScopeInterface::SCOPE_STORE ,
119+ $ item ,
120+ 'en_US '
121+ ];
122+ }, $ stores );
123+ $ this ->scopeConfigMock ->method ('getValue ' )
124+ ->willReturnMap ($ scopeConfigMap );
125+
126+ $ currentWebsiteMock ->method ('getStores ' )
127+ ->willReturn ($ stores );
128+
129+ $ this ->assertEquals ([3 , 1 , 4 , 2 ], array_keys ($ this ->switcher ->getStores ()));
130+ }
131+
65132 /**
66133 * @return void
67134 */
68135 public function testGetTargetStorePostData ()
69136 {
70- $ store = $ this ->getMockBuilder (Store::class)
137+ $ storeMock = $ this ->getMockBuilder (Store::class)
71138 ->disableOriginalConstructor ()
72139 ->getMock ();
73- $ store ->expects ($ this ->any ())
74- ->method ('getCode ' )
140+ $ oldStoreMock = $ this ->getMockBuilder (StoreInterface::class)
141+ ->disableOriginalConstructor ()
142+ ->getMockForAbstractClass ();
143+ $ storeMock ->method ('getCode ' )
75144 ->willReturn ('new-store ' );
76145 $ storeSwitchUrl = 'http://domain.com/stores/store/redirect ' ;
77- $ store ->expects ($ this ->atLeastOnce ())
146+ $ storeMock ->expects ($ this ->atLeastOnce ())
78147 ->method ('getCurrentUrl ' )
79148 ->with (false )
80149 ->willReturn ($ storeSwitchUrl );
81- $ this ->storeManager ->expects ($ this ->once ())
150+ $ this ->storeManagerMock ->expects ($ this ->once ())
82151 ->method ('getStore ' )
83- ->willReturn ($ this -> store );
84- $ this -> store ->expects ($ this ->once ())
152+ ->willReturn ($ oldStoreMock );
153+ $ oldStoreMock ->expects ($ this ->once ())
85154 ->method ('getCode ' )
86155 ->willReturn ('old-store ' );
87- $ this ->urlBuilder ->expects ($ this ->once ())
156+ $ this ->urlBuilderMock ->expects ($ this ->once ())
88157 ->method ('getUrl ' )
89158 ->willReturn ($ storeSwitchUrl );
90- $ this ->corePostDataHelper ->expects ($ this ->any ())
91- ->method ('getPostData ' )
159+ $ this ->corePostDataHelperMock ->method ('getPostData ' )
92160 ->with ($ storeSwitchUrl , ['___store ' => 'new-store ' , 'uenc ' => null , '___from_store ' => 'old-store ' ]);
93161
94- $ this ->switcher ->getTargetStorePostData ($ store );
162+ $ this ->switcher ->getTargetStorePostData ($ storeMock );
95163 }
96164
97165 /**
@@ -104,7 +172,7 @@ public function testIsStoreInUrl($isUseStoreInUrl)
104172
105173 $ storeMock ->expects ($ this ->once ())->method ('isUseStoreInUrl ' )->willReturn ($ isUseStoreInUrl );
106174
107- $ this ->storeManager -> expects ( $ this -> any ()) ->method ('getStore ' )->willReturn ($ storeMock );
175+ $ this ->storeManagerMock ->method ('getStore ' )->willReturn ($ storeMock );
108176 $ this ->assertEquals ($ this ->switcher ->isStoreInUrl (), $ isUseStoreInUrl );
109177 // check value is cached
110178 $ this ->assertEquals ($ this ->switcher ->isStoreInUrl (), $ isUseStoreInUrl );
@@ -114,7 +182,7 @@ public function testIsStoreInUrl($isUseStoreInUrl)
114182 * @see self::testIsStoreInUrlDataProvider()
115183 * @return array
116184 */
117- public function isStoreInUrlDataProvider ()
185+ public function isStoreInUrlDataProvider (): array
118186 {
119187 return [[true ], [false ]];
120188 }
0 commit comments