7
7
8
8
namespace Magento \Store \Test \Unit \Block ;
9
9
10
+ use Magento \Directory \Helper \Data ;
11
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
10
12
use Magento \Framework \Data \Helper \PostHelper ;
11
13
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
12
14
use Magento \Framework \UrlInterface ;
13
15
use Magento \Framework \View \Element \Template \Context ;
14
16
use Magento \Store \Api \Data \StoreInterface ;
15
17
use Magento \Store \Block \Switcher ;
18
+ use Magento \Store \Model \ScopeInterface ;
16
19
use Magento \Store \Model \Store ;
17
20
use Magento \Store \Model \StoreManagerInterface ;
21
+ use Magento \Store \Model \Website ;
18
22
use PHPUnit \Framework \MockObject \MockObject ;
19
23
use PHPUnit \Framework \TestCase ;
20
24
25
+ /**
26
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27
+ */
21
28
class SwitcherTest extends TestCase
22
29
{
23
- /** @var Switcher */
24
- protected $ switcher ;
25
-
26
- /** @var Context|MockObject */
27
- protected $ context ;
30
+ /**
31
+ * @var Switcher
32
+ */
33
+ private $ switcher ;
28
34
29
- /** @var PostHelper|MockObject */
30
- protected $ corePostDataHelper ;
35
+ /**
36
+ * @var PostHelper|MockObject
37
+ */
38
+ private $ corePostDataHelperMock ;
31
39
32
- /** @var StoreManagerInterface|MockObject */
33
- protected $ storeManager ;
40
+ /**
41
+ * @var StoreManagerInterface|MockObject
42
+ */
43
+ private $ storeManagerMock ;
34
44
35
- /** @var UrlInterface|MockObject */
36
- protected $ urlBuilder ;
45
+ /**
46
+ * @var UrlInterface|MockObject
47
+ */
48
+ private $ urlBuilderMock ;
37
49
38
- /** @var StoreInterface|MockObject */
39
- private $ store ;
50
+ /**
51
+ * @var ScopeConfigInterface|MockObject
52
+ */
53
+ private $ scopeConfigMock ;
40
54
41
55
/**
42
56
* @return void
43
57
*/
44
58
protected function setUp (): void
45
59
{
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);
56
68
$ this ->switcher = (new ObjectManager ($ this ))->getObject (
57
69
Switcher::class,
58
70
[
59
- 'context ' => $ this -> context ,
60
- 'postDataHelper ' => $ this ->corePostDataHelper ,
71
+ 'context ' => $ contextMock ,
72
+ 'postDataHelper ' => $ this ->corePostDataHelperMock ,
61
73
]
62
74
);
63
75
}
64
76
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
+
65
132
/**
66
133
* @return void
67
134
*/
68
135
public function testGetTargetStorePostData ()
69
136
{
70
- $ store = $ this ->getMockBuilder (Store::class)
137
+ $ storeMock = $ this ->getMockBuilder (Store::class)
71
138
->disableOriginalConstructor ()
72
139
->getMock ();
73
- $ store ->expects ($ this ->any ())
74
- ->method ('getCode ' )
140
+ $ oldStoreMock = $ this ->getMockBuilder (StoreInterface::class)
141
+ ->disableOriginalConstructor ()
142
+ ->getMockForAbstractClass ();
143
+ $ storeMock ->method ('getCode ' )
75
144
->willReturn ('new-store ' );
76
145
$ storeSwitchUrl = 'http://domain.com/stores/store/redirect ' ;
77
- $ store ->expects ($ this ->atLeastOnce ())
146
+ $ storeMock ->expects ($ this ->atLeastOnce ())
78
147
->method ('getCurrentUrl ' )
79
148
->with (false )
80
149
->willReturn ($ storeSwitchUrl );
81
- $ this ->storeManager ->expects ($ this ->once ())
150
+ $ this ->storeManagerMock ->expects ($ this ->once ())
82
151
->method ('getStore ' )
83
- ->willReturn ($ this -> store );
84
- $ this -> store ->expects ($ this ->once ())
152
+ ->willReturn ($ oldStoreMock );
153
+ $ oldStoreMock ->expects ($ this ->once ())
85
154
->method ('getCode ' )
86
155
->willReturn ('old-store ' );
87
- $ this ->urlBuilder ->expects ($ this ->once ())
156
+ $ this ->urlBuilderMock ->expects ($ this ->once ())
88
157
->method ('getUrl ' )
89
158
->willReturn ($ storeSwitchUrl );
90
- $ this ->corePostDataHelper ->expects ($ this ->any ())
91
- ->method ('getPostData ' )
159
+ $ this ->corePostDataHelperMock ->method ('getPostData ' )
92
160
->with ($ storeSwitchUrl , ['___store ' => 'new-store ' , 'uenc ' => null , '___from_store ' => 'old-store ' ]);
93
161
94
- $ this ->switcher ->getTargetStorePostData ($ store );
162
+ $ this ->switcher ->getTargetStorePostData ($ storeMock );
95
163
}
96
164
97
165
/**
@@ -104,7 +172,7 @@ public function testIsStoreInUrl($isUseStoreInUrl)
104
172
105
173
$ storeMock ->expects ($ this ->once ())->method ('isUseStoreInUrl ' )->willReturn ($ isUseStoreInUrl );
106
174
107
- $ this ->storeManager -> expects ( $ this -> any ()) ->method ('getStore ' )->willReturn ($ storeMock );
175
+ $ this ->storeManagerMock ->method ('getStore ' )->willReturn ($ storeMock );
108
176
$ this ->assertEquals ($ this ->switcher ->isStoreInUrl (), $ isUseStoreInUrl );
109
177
// check value is cached
110
178
$ this ->assertEquals ($ this ->switcher ->isStoreInUrl (), $ isUseStoreInUrl );
@@ -114,7 +182,7 @@ public function testIsStoreInUrl($isUseStoreInUrl)
114
182
* @see self::testIsStoreInUrlDataProvider()
115
183
* @return array
116
184
*/
117
- public function isStoreInUrlDataProvider ()
185
+ public function isStoreInUrlDataProvider (): array
118
186
{
119
187
return [[true ], [false ]];
120
188
}
0 commit comments