Skip to content

Commit f7523ca

Browse files
ShradddhaShradddha
authored andcommitted
AC-12092:: Remove Deprecations- PhpUnit10 Unit Tests
1 parent 80ba57d commit f7523ca

File tree

7 files changed

+227
-116
lines changed

7 files changed

+227
-116
lines changed

app/code/Magento/Indexer/Test/Unit/Model/Indexer/DependencyDecoratorTest.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,15 @@ public static function overloadDataProvider()
111111
}
112112

113113
/**
114-
* @param string $methodName
114+
* @param string|\Closure $methodName
115115
* @param mixed $result
116116
* @dataProvider transitMethodsDataProvider
117117
*/
118-
public function testTransitMethods(string $methodName, $result)
118+
public function testTransitMethods(string|\Closure $methodName, $result)
119119
{
120+
if (is_callable($result)) {
121+
$result = $result($this);
122+
}
120123
$this->indexerMock
121124
->expects($this->once())
122125
->method($methodName)
@@ -125,10 +128,23 @@ public function testTransitMethods(string $methodName, $result)
125128
$this->assertSame($result, $this->dependencyDecorator->{$methodName}());
126129
}
127130

131+
protected function getMockForViewClass()
132+
{
133+
return $this->getMockBuilder(View::class)
134+
->disableOriginalConstructor()
135+
->getMock();
136+
}
137+
138+
protected function getMockForStateInterfaceClass()
139+
{
140+
return $this->getMockBuilder(StateInterface::class)
141+
->getMockForAbstractClass();
142+
}
143+
128144
/**
129145
* @return array
130146
*/
131-
public function transitMethodsDataProvider()
147+
public static function transitMethodsDataProvider()
132148
{
133149
return [
134150
['getId', 'indexer_1'],
@@ -138,11 +154,8 @@ public function transitMethodsDataProvider()
138154
['getFields', ['one', 'two']],
139155
['getSources', ['one', 'two']],
140156
['getHandlers', ['one', 'two']],
141-
['getView', $this->getMockBuilder(View::class)
142-
->disableOriginalConstructor()
143-
->getMock()],
144-
['getState', $this->getMockBuilder(StateInterface::class)
145-
->getMockForAbstractClass()],
157+
['getView', static fn (self $testCase) => $testCase->getMockForViewClass()],
158+
['getState', static fn (self $testCase) => $testCase->getMockForStateInterfaceClass()],
146159
['isScheduled', true],
147160
['isValid', false],
148161
['isInvalid', true],
@@ -183,6 +196,9 @@ public static function transitMethodsWithParamsAndEmptyReturnDataProvider()
183196
*/
184197
public function testTransitMethodsWithParamsAndSelfReturn(string $methodName, array $params)
185198
{
199+
if (!empty($params) && is_callable($params[0])) {
200+
$params[0] = $params[0]($this);
201+
}
186202
$this->indexerMock
187203
->expects($this->once())
188204
->method($methodName)
@@ -193,14 +209,13 @@ public function testTransitMethodsWithParamsAndSelfReturn(string $methodName, ar
193209
/**
194210
* @return array
195211
*/
196-
public function transitMethodsWithParamsAndSelfReturnDataProvider()
212+
public static function transitMethodsWithParamsAndSelfReturnDataProvider()
197213
{
198214
return [
199215
[
200216
'setState',
201217
[
202-
$this->getMockBuilder(StateInterface::class)
203-
->getMockForAbstractClass()
218+
static fn (self $testCase) => $testCase->getMockForStateInterfaceClass()
204219
]
205220
],
206221
['load', ['indexer_1']],

lib/internal/Magento/Framework/App/Test/Unit/PageCache/KernelTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ function ($value) {
193193
/**
194194
* @return array
195195
*/
196-
public function dataProviderForResultWithCachedData(): array
196+
public static function dataProviderForResultWithCachedData(): array
197197
{
198198
$data = [
199199
'context' => [
@@ -240,7 +240,7 @@ public function testLoadWithoutCachedData($id, $cache, $isGet, $isHead): void
240240
/**
241241
* @return array
242242
*/
243-
public function dataProviderForResultWithoutCachedData(): array
243+
public static function dataProviderForResultWithoutCachedData(): array
244244
{
245245
return [
246246
['existing key', [], false, false],

lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ public function testConstructActionsNoCached()
108108
*/
109109
public function testGet($module, $area, $namespace, $action, $data, $isInstantiable, $expected)
110110
{
111+
if ($expected!=null) {
112+
$expected = $expected($this);
113+
}
111114
$this->reflectionClass->method('isInstantiable')->willReturn($isInstantiable);
112115

113116
$this->cacheMock->expects($this->once())
@@ -127,10 +130,7 @@ public function testGet($module, $area, $namespace, $action, $data, $isInstantia
127130
));
128131
}
129132

130-
/**
131-
* @return array
132-
*/
133-
public function getDataProvider()
133+
protected function getMockForActionInterface()
134134
{
135135
$mockClassName = 'Mock_Action_Class';
136136
$actionClassObject = $this->getMockForAbstractClass(
@@ -144,6 +144,17 @@ public function getDataProvider()
144144
);
145145

146146
$actionClass = get_class($actionClassObject);
147+
return $actionClass;
148+
}
149+
150+
/**
151+
* @return array
152+
*/
153+
public static function getDataProvider()
154+
{
155+
$mockClassName = 'Mock_Action_Class';
156+
157+
$actionClass = static fn (self $testCase) => $testCase->getMockForActionInterface();
147158

148159
return [
149160
[
@@ -196,7 +207,7 @@ public function getDataProvider()
196207
null,
197208
'adminhtml_product',
198209
'index',
199-
'magento\module\controller\adminhtml\product\index' => '$mockClassName',
210+
['magento\module\controller\adminhtml\product\index' => '$mockClassName'],
200211
false,
201212
null
202213
],

lib/internal/Magento/Framework/App/Test/Unit/Router/BaseTest.php

Lines changed: 71 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,22 @@ protected function setUp(): void
9595

9696
/**
9797
* @dataProvider matchDataProvider
98-
* @param MockObject|Http $requestMock
98+
* @param MockObject|\Closure $requestMock
9999
* @param string $defaultPath
100100
* @param string $moduleFrontName
101101
* @param string|null $actionPath
102102
* @param string|null $actionName
103103
* @param string|null $moduleName
104104
*/
105105
public function testMatch(
106-
MockObject $requestMock,
106+
MockObject|\Closure $requestMock,
107107
string $defaultPath,
108108
string $moduleFrontName,
109109
?string $actionPath,
110110
?string $actionName,
111111
?string $moduleName
112112
) {
113+
$requestMock = $requestMock($this);
113114
$actionInstance = 'Magento_TestFramework_ActionInstance';
114115

115116
$defaultReturnMap = [
@@ -147,33 +148,45 @@ public function testMatch(
147148
$this->assertEquals($actionMock, $this->model->match($requestMock));
148149
}
149150

150-
public function matchDataProvider(): array
151+
protected function getMockForHttpClass($moduleFrontName, $actionPath, $actionName, $originalPathInfo)
152+
{
153+
$requestMock = $this->createMock(Http::class);
154+
$requestMock->expects($this->atLeastOnce())->method('getModuleName')->willReturn($moduleFrontName);
155+
$requestMock->expects($this->atLeastOnce())->method('getControllerName')->willReturn($actionPath);
156+
$requestMock->expects($this->atLeastOnce())->method('getActionName')->willReturn($actionName);
157+
if ($moduleFrontName!=null && $moduleFrontName!='') {
158+
$requestMock->expects($this->atLeastOnce())
159+
->method('getPathInfo')
160+
->willReturn($moduleFrontName . '/' . $actionPath . '/' . $actionName . '/key/val/key2/val2/');
161+
}
162+
else {
163+
$requestMock->expects($this->atLeastOnce())->method('getPathInfo')->willReturn('');
164+
}
165+
166+
if ($originalPathInfo) {
167+
$requestMock->expects($this->atLeastOnce())->method('getOriginalPathInfo')->willReturn('');
168+
}
169+
return $requestMock;
170+
}
171+
172+
public static function matchDataProvider(): array
151173
{
152174
$moduleFrontName = 'module_front_name';
153175
$actionPath = 'action_path';
154176
$actionName = 'action_name';
155177
$moduleName = 'module_name';
156178

157-
$requestMock = $this->createMock(Http::class);
158-
$requestMock->expects($this->atLeastOnce())->method('getModuleName')->willReturn($moduleFrontName);
159-
$requestMock->expects($this->atLeastOnce())->method('getControllerName')->willReturn($actionPath);
160-
$requestMock->expects($this->atLeastOnce())->method('getActionName')->willReturn($actionName);
161-
$requestMock->expects($this->atLeastOnce())
162-
->method('getPathInfo')
163-
->willReturn($moduleFrontName . '/' . $actionPath . '/' . $actionName . '/key/val/key2/val2/');
164-
165-
$emptyRequestMock = $this->createMock(Http::class);
166-
$emptyRequestMock->expects($this->atLeastOnce())->method('getModuleName')->willReturn('');
167-
$emptyRequestMock->expects($this->atLeastOnce())->method('getControllerName')->willReturn('');
168-
$emptyRequestMock->expects($this->atLeastOnce())->method('getActionName')->willReturn('');
169-
$emptyRequestMock->expects($this->atLeastOnce())->method('getPathInfo')->willReturn('');
170-
171-
$emptyRequestMock2 = $this->createMock(Http::class);
172-
$emptyRequestMock2->expects($this->atLeastOnce())->method('getModuleName')->willReturn('');
173-
$emptyRequestMock2->expects($this->atLeastOnce())->method('getControllerName')->willReturn('');
174-
$emptyRequestMock2->expects($this->atLeastOnce())->method('getActionName')->willReturn('');
175-
$emptyRequestMock2->expects($this->atLeastOnce())->method('getPathInfo')->willReturn('');
176-
$emptyRequestMock2->expects($this->atLeastOnce())->method('getOriginalPathInfo')->willReturn('');
179+
$requestMock = static fn (self $testCase) => $testCase->getMockForHttpClass(
180+
$moduleFrontName, $actionPath, $actionName, false
181+
);
182+
183+
$emptyRequestMock = static fn (self $testCase) => $testCase->getMockForHttpClass(
184+
'', '', '', false
185+
);
186+
187+
$emptyRequestMock2 = static fn (self $testCase) => $testCase->getMockForHttpClass(
188+
'', '', '', true
189+
);
177190

178191
return [
179192
[
@@ -205,21 +218,22 @@ public function matchDataProvider(): array
205218

206219
/**
207220
* @dataProvider matchEmptyActionDataProvider
208-
* @param MockObject|Http $requestMock
221+
* @param MockObject|\Closure $requestMock
209222
* @param string $defaultPath
210223
* @param string $moduleFrontName
211224
* @param string|null $actionPath
212225
* @param string|null $actionName
213226
* @param string|null $moduleName
214227
*/
215228
public function testMatchEmptyAction(
216-
MockObject $requestMock,
229+
MockObject|\Closure $requestMock,
217230
string $defaultPath,
218231
string $moduleFrontName,
219232
?string $actionPath,
220233
?string $actionName,
221234
?string $moduleName
222235
) {
236+
$requestMock = $requestMock($this);
223237
$defaultReturnMap = [
224238
['module', $moduleFrontName],
225239
['controller', $actionPath],
@@ -240,29 +254,43 @@ public function testMatchEmptyAction(
240254
$this->assertNull($this->model->match($requestMock));
241255
}
242256

243-
public function matchEmptyActionDataProvider(): array
257+
protected function getMockForHttpClassTwo($moduleFrontName, $actionPath, $actionName, $bool)
258+
{
259+
$requestMock = $this->createMock(Http::class);
260+
$requestMock->expects($this->atLeastOnce())->method('getModuleName')->willReturn($moduleFrontName);
261+
if ($bool) {
262+
$requestMock->expects($this->atLeastOnce())->method('getControllerName')->willReturn($actionPath);
263+
$requestMock->expects($this->atLeastOnce())->method('getActionName')->willReturn($actionName);
264+
}
265+
if ($moduleFrontName!='') {
266+
$requestMock->expects($this->atLeastOnce())
267+
->method('getPathInfo')
268+
->willReturn($moduleFrontName . '/' . $actionPath . '/' . $actionName . '/');
269+
}
270+
else {
271+
$requestMock->expects($this->atLeastOnce())->method('getPathInfo')->willReturn('0');
272+
}
273+
274+
return $requestMock;
275+
}
276+
277+
public static function matchEmptyActionDataProvider(): array
244278
{
245279
$moduleFrontName = 'module_front_name';
246280
$actionPath = 'action_path';
247281
$actionName = 'action_name';
248282

249-
$requestMock1 = $this->createMock(Http::class);
250-
$requestMock1->expects($this->atLeastOnce())->method('getModuleName')->willReturn($moduleFrontName);
251-
$requestMock1->expects($this->atLeastOnce())->method('getControllerName')->willReturn($actionPath);
252-
$requestMock1->expects($this->atLeastOnce())->method('getActionName')->willReturn($actionName);
253-
$requestMock1->expects($this->atLeastOnce())
254-
->method('getPathInfo')
255-
->willReturn($moduleFrontName . '/' . $actionPath . '/' . $actionName . '/');
256-
257-
$requestMock2 = $this->createMock(Http::class);
258-
$requestMock2->expects($this->atLeastOnce())->method('getModuleName')->willReturn($moduleFrontName);
259-
$requestMock2->expects($this->atLeastOnce())
260-
->method('getPathInfo')
261-
->willReturn($moduleFrontName . '/' . $actionPath . '/' . $actionName . '/');
262-
263-
$requestMock3 = $this->createMock(Http::class);
264-
$requestMock3->expects($this->atLeastOnce())->method('getModuleName')->willReturn('');
265-
$requestMock3->expects($this->atLeastOnce())->method('getPathInfo')->willReturn('0');
283+
$requestMock1 = static fn (self $testCase) => $testCase->getMockForHttpClassTwo(
284+
$moduleFrontName, $actionPath, $actionName, true
285+
);
286+
287+
$requestMock2 = static fn (self $testCase) => $testCase->getMockForHttpClassTwo(
288+
$moduleFrontName, $actionPath, $actionName, false
289+
);
290+
291+
$requestMock3 = static fn (self $testCase) => $testCase->getMockForHttpClassTwo(
292+
'', '', '', false
293+
);
266294

267295
return [
268296
[

0 commit comments

Comments
 (0)