88namespace Magento \Backend \Test \Unit \Block \Store ;
99
1010use Magento \Backend \Block \Store \Switcher ;
11+ use Magento \Framework \App \RequestInterface ;
12+ use Magento \Framework \Exception \LocalizedException ;
13+ use Magento \Store \Model \StoreFactory ;
14+ use Magento \Store \Model \Store ;
15+ use Magento \Store \Model \WebsiteFactory ;
16+ use Magento \Store \Model \Website ;
1117use Magento \Backend \Block \Template \Context ;
1218use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
1319use Magento \Store \Model \StoreManagerInterface ;
14- use Magento \Store \Model \Website ;
1520use PHPUnit \Framework \TestCase ;
1621
1722class SwitcherTest extends TestCase
@@ -23,20 +28,81 @@ class SwitcherTest extends TestCase
2328
2429 private $ storeManagerMock ;
2530
31+ /**
32+ * @var RequestInterface|MockObject
33+ */
34+ private $ requestMock ;
35+
36+ /**
37+ * @var WebsiteFactory|MockObject
38+ */
39+ private $ websiteFactoryMock ;
40+
41+ /**
42+ * @var StoreFactory|MockObject
43+ */
44+ private $ storeFactoryMock ;
45+
46+ /**
47+ * @var Website|MockObject
48+ */
49+ private $ websiteMock ;
50+
51+ /**
52+ * @var Store|MockObject
53+ */
54+ private $ storeMock ;
55+
2656 protected function setUp (): void
2757 {
2858 $ this ->storeManagerMock = $ this ->getMockForAbstractClass (StoreManagerInterface::class);
2959 $ objectHelper = new ObjectManager ($ this );
60+ $ this ->requestMock = $ this ->getMockBuilder (RequestInterface::class)
61+ ->getMockForAbstractClass ();
62+ $ this ->websiteFactoryMock = $ this ->getMockBuilder (WebsiteFactory::class)
63+ ->disableOriginalConstructor ()
64+ ->setMethods (['create ' ])
65+ ->getMock ();
66+ $ this ->storeFactoryMock = $ this ->getMockBuilder (StoreFactory::class)
67+ ->disableOriginalConstructor ()
68+ ->setMethods (['create ' ])
69+ ->getMock ();
70+ $ this ->websiteMock = $ this ->getMockBuilder (Website::class)
71+ ->disableOriginalConstructor ()
72+ ->setMethods (['load ' , 'getId ' , 'getName ' ])
73+ ->getMock ();
74+ $ this ->storeMock = $ this ->getMockBuilder (Store::class)
75+ ->disableOriginalConstructor ()
76+ ->setMethods (['load ' , 'getId ' , 'getName ' ])
77+ ->getMock ();
78+ $ this ->websiteFactoryMock ->expects ($ this ->any ())
79+ ->method ('create ' )
80+ ->willReturn ($ this ->websiteMock );
81+ $ this ->storeFactoryMock ->expects ($ this ->any ())
82+ ->method ('create ' )
83+ ->willReturn ($ this ->storeMock );
84+ $ this ->websiteMock ->expects ($ this ->any ())
85+ ->method ('load ' )
86+ ->willReturnSelf ();
87+ $ this ->storeMock ->expects ($ this ->any ())
88+ ->method ('load ' )
89+ ->willReturnSelf ();
3090 $ context = $ objectHelper ->getObject (
3191 Context::class,
3292 [
3393 'storeManager ' => $ this ->storeManagerMock ,
94+ 'request ' => $ this ->requestMock
3495 ]
3596 );
3697
3798 $ this ->switcherBlock = $ objectHelper ->getObject (
3899 Switcher::class,
39- ['context ' => $ context ]
100+ [
101+ 'context ' => $ context ,
102+ 'data ' => ['get_data_from_request ' => 1 ],
103+ 'websiteFactory ' => $ this ->websiteFactoryMock ,
104+ 'storeFactory ' => $ this ->storeFactoryMock
105+ ]
40106 );
41107 }
42108
@@ -58,4 +124,91 @@ public function testGetWebsitesIfSetWebsiteIds()
58124 $ expected = [1 => $ websiteMock ];
59125 $ this ->assertEquals ($ expected , $ this ->switcherBlock ->getWebsites ());
60126 }
127+
128+ /**
129+ * Test case for after current store name plugin
130+ *
131+ * @param array $requestedStore
132+ * @param string $expectedResult
133+ * @return void
134+ * @dataProvider getStoreNameDataProvider
135+ * @throws LocalizedException
136+ */
137+ public function testAfterGetCurrentStoreName (array $ requestedStore , string $ expectedResult ): void
138+ {
139+ $ this ->requestMock ->expects ($ this ->any ())
140+ ->method ('getParams ' )
141+ ->willReturn ($ requestedStore );
142+ $ this ->storeMock ->expects ($ this ->any ())
143+ ->method ('getId ' )
144+ ->willReturn ($ requestedStore );
145+ $ this ->storeMock ->expects ($ this ->any ())
146+ ->method ('getName ' )
147+ ->willReturn ($ expectedResult );
148+ $ this ->assertSame ($ expectedResult , $ this ->switcherBlock ->getCurrentStoreName ());
149+ }
150+
151+ /**
152+ * Data provider for getStoreName plugin
153+ *
154+ * @return array
155+ */
156+ public function getStoreNameDataProvider (): array
157+ {
158+ return [
159+ 'test storeName with valid requested store ' =>
160+ [
161+ ['store ' => 'test store ' ],
162+ 'base store '
163+ ],
164+ 'test storeName with invalid requested store ' =>
165+ [
166+ ['store ' => 'test store ' ],
167+ 'test store '
168+ ]
169+ ];
170+ }
171+
172+ /**
173+ * Test case for get current website name
174+ *
175+ * @param array $requestedWebsite
176+ * @param string $expectedResult
177+ * @return void
178+ * @dataProvider getWebsiteNameDataProvider
179+ */
180+ public function testGetCurrentWebsiteName (array $ requestedWebsite , string $ expectedResult ): void
181+ {
182+ $ this ->requestMock ->expects ($ this ->any ())
183+ ->method ('getParams ' )
184+ ->willReturn ($ requestedWebsite );
185+ $ this ->websiteMock ->expects ($ this ->any ())
186+ ->method ('getId ' )
187+ ->willReturn ($ requestedWebsite );
188+ $ this ->websiteMock ->expects ($ this ->any ())
189+ ->method ('getName ' )
190+ ->willReturn ($ expectedResult );
191+ $ this ->assertSame ($ expectedResult , $ this ->switcherBlock ->getCurrentWebsiteName ());
192+ }
193+
194+ /**
195+ * Data provider for getWebsiteName plugin
196+ *
197+ * @return array
198+ */
199+ public function getWebsiteNameDataProvider (): array
200+ {
201+ return [
202+ 'test websiteName with valid requested website ' =>
203+ [
204+ ['website ' => 'test website ' ],
205+ 'base website '
206+ ],
207+ 'test websiteName with invalid requested website ' =>
208+ [
209+ ['website ' => 'test website ' ],
210+ 'test website '
211+ ]
212+ ];
213+ }
61214}
0 commit comments