Skip to content

Commit 7833f43

Browse files
author
Vasilii Burlacu
committed
Adjusted Unit test for RouteParamsResolver after added changes
1 parent ecfbe24 commit 7833f43

File tree

2 files changed

+65
-51
lines changed

2 files changed

+65
-51
lines changed

app/code/Magento/Store/Test/Unit/Url/Plugin/RouteParamsResolverTest.php

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,36 @@ class RouteParamsResolverTest extends \PHPUnit_Framework_TestCase
2222
*/
2323
protected $queryParamsResolverMock;
2424

25+
/**
26+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\Store
27+
*/
28+
protected $storeMock;
29+
2530
/**
2631
* @var \Magento\Store\Url\Plugin\RouteParamsResolver
2732
*/
2833
protected $model;
2934

35+
/**
36+
* @return void
37+
*/
3038
protected function setUp()
3139
{
32-
$this->scopeConfigMock = $this->getMock('Magento\Framework\App\Config\ScopeConfigInterface');
33-
$this->storeManagerMock = $this->getMock('Magento\Store\Model\StoreManagerInterface');
34-
$this->queryParamsResolverMock = $this->getMock('Magento\Framework\Url\QueryParamsResolverInterface');
40+
$this->scopeConfigMock = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
41+
42+
$this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
43+
->setMethods(['getCode'])
44+
->disableOriginalConstructor()
45+
->getMock();
46+
$this->storeMock->expects($this->any())->method('getCode')->willReturn('custom_store');
47+
48+
$this->storeManagerMock = $this->getMock(\Magento\Store\Model\StoreManagerInterface::class);
49+
$this->storeManagerMock
50+
->expects($this->once())
51+
->method('getStore')
52+
->willReturn($this->storeMock);
53+
54+
$this->queryParamsResolverMock = $this->getMock(\Magento\Framework\Url\QueryParamsResolverInterface::class);
3555
$this->model = new \Magento\Store\Url\Plugin\RouteParamsResolver(
3656
$this->scopeConfigMock,
3757
$this->storeManagerMock,
@@ -40,11 +60,15 @@ protected function setUp()
4060
}
4161

4262
/**
43-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
63+
* @throws \Magento\Framework\Exception\NoSuchEntityException
64+
*
65+
* @return void
4466
*/
45-
public function testAroundSetRouteParamsScopeInParams()
67+
public function testBeforeSetRouteParamsScopeInParams()
4668
{
4769
$storeCode = 'custom_store';
70+
$data = ['_scope' => $storeCode, '_scope_to_url' => true];
71+
4872
$this->scopeConfigMock
4973
->expects($this->once())
5074
->method('getValue')
@@ -55,33 +79,33 @@ public function testAroundSetRouteParamsScopeInParams()
5579
)
5680
->will($this->returnValue(false));
5781
$this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false);
58-
$data = ['_scope' => $storeCode, '_scope_to_url' => true];
82+
5983
/** @var \PHPUnit_Framework_MockObject_MockObject $routeParamsResolverMock */
60-
$routeParamsResolverMock = $this->getMockBuilder('Magento\Framework\Url\RouteParamsResolver')
84+
$routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
6185
->setMethods(['setScope', 'getScope'])
6286
->disableOriginalConstructor()
6387
->getMock();
6488
$routeParamsResolverMock->expects($this->once())->method('setScope')->with($storeCode);
6589
$routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);
6690

67-
$this->queryParamsResolverMock->expects($this->once())->method('setQueryParam')->with('___store', $storeCode);
91+
$this->queryParamsResolverMock->expects($this->never())->method('setQueryParam');
6892

69-
$this->model->aroundSetRouteParams(
93+
$this->model->beforeSetRouteParams(
7094
$routeParamsResolverMock,
71-
function ($data, $unsetOldParams) {
72-
$this->assertArrayNotHasKey('_scope_to_url', $data, 'This data item should have been unset.');
73-
$this->assertArrayNotHasKey('_scope', $data, 'This data item should have been unset.');
74-
},
7595
$data
7696
);
7797
}
7898

7999
/**
80-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
100+
* @throws \Magento\Framework\Exception\NoSuchEntityException
101+
*
102+
* @return void
81103
*/
82-
public function testAroundSetRouteParamsScopeUseStoreInUrl()
104+
public function testBeforeSetRouteParamsScopeUseStoreInUrl()
83105
{
84106
$storeCode = 'custom_store';
107+
$data = ['_scope' => $storeCode, '_scope_to_url' => true];
108+
85109
$this->scopeConfigMock
86110
->expects($this->once())
87111
->method('getValue')
@@ -91,34 +115,35 @@ public function testAroundSetRouteParamsScopeUseStoreInUrl()
91115
$storeCode
92116
)
93117
->will($this->returnValue(true));
118+
94119
$this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false);
95-
$data = ['_scope' => $storeCode, '_scope_to_url' => true];
120+
96121
/** @var \PHPUnit_Framework_MockObject_MockObject $routeParamsResolverMock */
97-
$routeParamsResolverMock = $this->getMockBuilder('Magento\Framework\Url\RouteParamsResolver')
122+
$routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
98123
->setMethods(['setScope', 'getScope'])
99124
->disableOriginalConstructor()
100125
->getMock();
101126
$routeParamsResolverMock->expects($this->once())->method('setScope')->with($storeCode);
102127
$routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);
103128

104-
$this->queryParamsResolverMock->expects($this->never())->method('setQueryParam');
129+
$this->queryParamsResolverMock->expects($this->once())->method('setQueryParam')->with('___store', $storeCode);
105130

106-
$this->model->aroundSetRouteParams(
131+
$this->model->beforeSetRouteParams(
107132
$routeParamsResolverMock,
108-
function ($data, $unsetOldParams) {
109-
$this->assertArrayNotHasKey('_scope_to_url', $data, 'This data item should have been unset.');
110-
$this->assertArrayNotHasKey('_scope', $data, 'This data item should have been unset.');
111-
},
112133
$data
113134
);
114135
}
115136

116137
/**
117-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
138+
* @throws \Magento\Framework\Exception\NoSuchEntityException
139+
*
140+
* @return void
118141
*/
119-
public function testAroundSetRouteParamsSingleStore()
142+
public function testBeforeSetRouteParamsSingleStore()
120143
{
121144
$storeCode = 'custom_store';
145+
$data = ['_scope' => $storeCode, '_scope_to_url' => true];
146+
122147
$this->scopeConfigMock
123148
->expects($this->once())
124149
->method('getValue')
@@ -129,9 +154,9 @@ public function testAroundSetRouteParamsSingleStore()
129154
)
130155
->will($this->returnValue(false));
131156
$this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(true);
132-
$data = ['_scope' => $storeCode, '_scope_to_url' => true];
157+
133158
/** @var \PHPUnit_Framework_MockObject_MockObject $routeParamsResolverMock */
134-
$routeParamsResolverMock = $this->getMockBuilder('Magento\Framework\Url\RouteParamsResolver')
159+
$routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
135160
->setMethods(['setScope', 'getScope'])
136161
->disableOriginalConstructor()
137162
->getMock();
@@ -140,22 +165,22 @@ public function testAroundSetRouteParamsSingleStore()
140165

141166
$this->queryParamsResolverMock->expects($this->never())->method('setQueryParam');
142167

143-
$this->model->aroundSetRouteParams(
168+
$this->model->beforeSetRouteParams(
144169
$routeParamsResolverMock,
145-
function ($data, $unsetOldParams) {
146-
$this->assertArrayNotHasKey('_scope_to_url', $data, 'This data item should have been unset.');
147-
$this->assertArrayNotHasKey('_scope', $data, 'This data item should have been unset.');
148-
},
149170
$data
150171
);
151172
}
152173

153174
/**
154-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
175+
* @throws \Magento\Framework\Exception\NoSuchEntityException
176+
*
177+
* @return void
155178
*/
156-
public function testAroundSetRouteParamsNoScopeInParams()
179+
public function testBeforeSetRouteParamsNoScopeInParams()
157180
{
158181
$storeCode = 'custom_store';
182+
$data = ['_scope_to_url' => true];
183+
159184
$this->scopeConfigMock
160185
->expects($this->once())
161186
->method('getValue')
@@ -164,19 +189,12 @@ public function testAroundSetRouteParamsNoScopeInParams()
164189
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
165190
$storeCode
166191
)
167-
->will($this->returnValue(false));
192+
->will($this->returnValue(true));
193+
168194
$this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false);
169-
/** @var \PHPUnit_Framework_MockObject_MockObject| $routeParamsResolverMock */
170-
$storeMock = $this->getMockBuilder('Magento\Store\Model\Store')
171-
->setMethods(['getCode'])
172-
->disableOriginalConstructor()
173-
->getMock();
174-
$storeMock->expects($this->any())->method('getCode')->willReturn($storeCode);
175-
$this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
176195

177-
$data = ['_scope_to_url' => true];
178196
/** @var \PHPUnit_Framework_MockObject_MockObject $routeParamsResolverMock */
179-
$routeParamsResolverMock = $this->getMockBuilder('Magento\Framework\Url\RouteParamsResolver')
197+
$routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
180198
->setMethods(['setScope', 'getScope'])
181199
->disableOriginalConstructor()
182200
->getMock();
@@ -185,11 +203,8 @@ public function testAroundSetRouteParamsNoScopeInParams()
185203

186204
$this->queryParamsResolverMock->expects($this->once())->method('setQueryParam')->with('___store', $storeCode);
187205

188-
$this->model->aroundSetRouteParams(
206+
$this->model->beforeSetRouteParams(
189207
$routeParamsResolverMock,
190-
function ($data, $unsetOldParams) {
191-
$this->assertArrayNotHasKey('_scope_to_url', $data, 'This data item should have been unset.');
192-
},
193208
$data
194209
);
195210
}

app/code/Magento/Store/Url/Plugin/RouteParamsResolver.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ public function __construct(
5757
* @return \Magento\Framework\Url\RouteParamsResolver
5858
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5959
*/
60-
public function aroundSetRouteParams(
60+
public function beforeSetRouteParams(
6161
\Magento\Framework\Url\RouteParamsResolver $subject,
62-
\Closure $proceed,
6362
array $data,
6463
$unsetOldParams = true
6564
) {
@@ -86,6 +85,6 @@ public function aroundSetRouteParams(
8685
}
8786
unset($data['_scope_to_url']);
8887

89-
return $proceed($data, $unsetOldParams);
88+
return [$data, $unsetOldParams];
9089
}
9190
}

0 commit comments

Comments
 (0)