Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RouteParamsResolverTest.php
Go to the documentation of this file.
1 <?php
7 
8 class RouteParamsResolverTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $scopeConfigMock;
14 
18  protected $storeManagerMock;
19 
24 
28  protected $storeMock;
29 
33  protected $model;
34 
35  protected function setUp()
36  {
37  $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
38 
39  $this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
40  ->setMethods(['getCode'])
41  ->disableOriginalConstructor()
42  ->getMock();
43  $this->storeMock->expects($this->any())->method('getCode')->willReturn('custom_store');
44 
45  $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
46  $this->storeManagerMock
47  ->expects($this->once())
48  ->method('getStore')
49  ->willReturn($this->storeMock);
50 
51  $this->queryParamsResolverMock = $this->createMock(\Magento\Framework\Url\QueryParamsResolverInterface::class);
52  $this->model = new \Magento\Store\Url\Plugin\RouteParamsResolver(
53  $this->scopeConfigMock,
54  $this->storeManagerMock,
55  $this->queryParamsResolverMock
56  );
57  }
58 
59  public function testBeforeSetRouteParamsScopeInParams()
60  {
61  $storeCode = 'custom_store';
62  $data = ['_scope' => $storeCode, '_scope_to_url' => true];
63 
64  $this->scopeConfigMock
65  ->expects($this->once())
66  ->method('getValue')
67  ->with(
68  \Magento\Store\Model\Store::XML_PATH_STORE_IN_URL,
69  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
71  )
72  ->will($this->returnValue(false));
73  $this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false);
74 
76  $routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
77  ->setMethods(['setScope', 'getScope'])
78  ->disableOriginalConstructor()
79  ->getMock();
80  $routeParamsResolverMock->expects($this->once())->method('setScope')->with($storeCode);
81  $routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);
82 
83  $this->queryParamsResolverMock->expects($this->never())->method('setQueryParam');
84 
85  $this->model->beforeSetRouteParams(
86  $routeParamsResolverMock,
87  $data
88  );
89  }
90 
91  public function testBeforeSetRouteParamsScopeUseStoreInUrl()
92  {
93  $storeCode = 'custom_store';
94  $data = ['_scope' => $storeCode, '_scope_to_url' => true];
95 
96  $this->scopeConfigMock
97  ->expects($this->once())
98  ->method('getValue')
99  ->with(
100  \Magento\Store\Model\Store::XML_PATH_STORE_IN_URL,
101  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
102  $storeCode
103  )
104  ->will($this->returnValue(true));
105 
106  $this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false);
107 
109  $routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
110  ->setMethods(['setScope', 'getScope'])
111  ->disableOriginalConstructor()
112  ->getMock();
113  $routeParamsResolverMock->expects($this->once())->method('setScope')->with($storeCode);
114  $routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);
115 
116  $this->queryParamsResolverMock->expects($this->once())->method('setQueryParam')->with('___store', $storeCode);
117 
118  $this->model->beforeSetRouteParams(
119  $routeParamsResolverMock,
120  $data
121  );
122  }
123 
124  public function testBeforeSetRouteParamsSingleStore()
125  {
126  $storeCode = 'custom_store';
127  $data = ['_scope' => $storeCode, '_scope_to_url' => true];
128 
129  $this->scopeConfigMock
130  ->expects($this->once())
131  ->method('getValue')
132  ->with(
133  \Magento\Store\Model\Store::XML_PATH_STORE_IN_URL,
134  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
135  $storeCode
136  )
137  ->will($this->returnValue(false));
138  $this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(true);
139 
141  $routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
142  ->setMethods(['setScope', 'getScope'])
143  ->disableOriginalConstructor()
144  ->getMock();
145  $routeParamsResolverMock->expects($this->once())->method('setScope')->with($storeCode);
146  $routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);
147 
148  $this->queryParamsResolverMock->expects($this->never())->method('setQueryParam');
149 
150  $this->model->beforeSetRouteParams(
151  $routeParamsResolverMock,
152  $data
153  );
154  }
155 
156  public function testBeforeSetRouteParamsNoScopeInParams()
157  {
158  $storeCode = 'custom_store';
159  $data = ['_scope_to_url' => true];
160 
161  $this->scopeConfigMock
162  ->expects($this->once())
163  ->method('getValue')
164  ->with(
165  \Magento\Store\Model\Store::XML_PATH_STORE_IN_URL,
166  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
167  $storeCode
168  )
169  ->will($this->returnValue(true));
170 
171  $this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false);
172 
174  $routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
175  ->setMethods(['setScope', 'getScope'])
176  ->disableOriginalConstructor()
177  ->getMock();
178  $routeParamsResolverMock->expects($this->never())->method('setScope');
179  $routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn(false);
180 
181  $this->queryParamsResolverMock->expects($this->once())->method('setQueryParam')->with('___store', $storeCode);
182 
183  $this->model->beforeSetRouteParams(
184  $routeParamsResolverMock,
185  $data
186  );
187  }
188 }
$storeCode
Definition: indexer.php:15