Skip to content

Commit 581c532

Browse files
Raj MohanRaj Mohan
authored andcommitted
AC-12092: Deprecations related to not static functionality removal in PHPUnit 10
1 parent 1d1acab commit 581c532

File tree

6 files changed

+147
-56
lines changed

6 files changed

+147
-56
lines changed

app/code/Magento/Customer/Test/Unit/Model/Metadata/Form/AbstractDataTest.php

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Customer\Test\Unit\Model\Metadata\Form;
99

10+
use Closure;
1011
use Laminas\I18n\Validator\Alpha;
1112
use Laminas\Validator\Date;
1213
use Laminas\Validator\Digits;
@@ -376,7 +377,7 @@ public function testGetIsAjaxRequest($ajaxRequest): void
376377
}
377378

378379
/**
379-
* @param RequestInterface $request
380+
* @param Closure|null $request
380381
* @param string $attributeCode
381382
* @param bool|string $requestScope
382383
* @param bool $requestScopeOnly
@@ -392,6 +393,9 @@ public function testGetRequestValue(
392393
$requestScopeOnly,
393394
$expectedValue
394395
): void {
396+
if ($request != null) {
397+
$request = $request($this);
398+
}
395399
$this->attributeMock->expects(
396400
$this->once()
397401
)->method(
@@ -405,12 +409,14 @@ public function testGetRequestValue(
405409
}
406410

407411
/**
412+
* @param $expectedValue
413+
*
408414
* @return array
409415
*/
410-
public function getRequestValueDataProvider(): array
416+
public function getRequestMock($expectedValue): array
411417
{
412-
$expectedValue = 'EXPECTED_VALUE';
413418
$requestMock = $this->getMockBuilder(RequestInterface::class)
419+
->disableOriginalConstructor()
414420
->getMock();
415421
$requestMock->method('getParam')
416422
->willReturnCallback(
@@ -428,20 +434,56 @@ function ($arg) use ($expectedValue) {
428434
}
429435
}
430436
);
431-
432437
$requestMockHttp = $this->getMockBuilder(Http::class)
433438
->disableOriginalConstructor()
434439
->getMock();
435440
$requestMockHttp
436-
->expects($this->once())
441+
->expects($this->any())
437442
->method('getParams')
438443
->willReturn(['REQUEST' => ['SCOPE' => ['ATTR_CODE' => $expectedValue]]]);
439444

440445
return [
441-
[$requestMock, 'ATTR_CODE', false, false, $expectedValue],
442-
[$requestMock, 'ATTR_CODE', 'REQUEST_SCOPE', false, $expectedValue],
443-
[$requestMock, 'ATTR_CODE', 'REQUEST_SCOPE', false, false],
444-
[$requestMockHttp, 'ATTR_CODE', 'REQUEST/SCOPE', false, $expectedValue]
446+
'requestMock' => $requestMock,
447+
'requestMockHttp' => $requestMockHttp
448+
];
449+
}
450+
451+
/**
452+
* @return array
453+
*/
454+
public static function getRequestValueDataProvider(): array
455+
{
456+
$expectedValue = 'EXPECTED_VALUE';
457+
458+
return [
459+
[
460+
static fn (self $testCase) => $testCase->getRequestMock($expectedValue)['requestMock'],
461+
'ATTR_CODE',
462+
false,
463+
false,
464+
$expectedValue
465+
],
466+
[
467+
static fn (self $testCase) => $testCase->getRequestMock($expectedValue)['requestMock'],
468+
'ATTR_CODE',
469+
'REQUEST_SCOPE',
470+
false,
471+
$expectedValue
472+
],
473+
[
474+
static fn (self $testCase) => $testCase->getRequestMock($expectedValue)['requestMockHttp'],
475+
'ATTR_CODE',
476+
'REQUEST_SCOPE',
477+
false,
478+
false
479+
],
480+
[
481+
static fn (self $testCase) => $testCase->getRequestMock($expectedValue)['requestMockHttp'],
482+
'ATTR_CODE',
483+
'REQUEST/SCOPE',
484+
false,
485+
$expectedValue
486+
]
445487
];
446488
}
447489
}

app/code/Magento/GroupedCatalogInventory/Test/Unit/Plugin/OutOfStockFilterTest.php

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Magento\GroupedCatalogInventory\Test\Unit\Plugin;
1010

11+
use Closure;
1112
use Magento\Catalog\Model\Product;
1213
use Magento\CatalogInventory\Api\Data\StockStatusInterface;
1314
use Magento\CatalogInventory\Api\StockRegistryInterface;
@@ -115,7 +116,7 @@ public function testFilterIgnoresResultIfSuperGroupIsPresent(): void
115116
/**
116117
* Tests that out of stock products will be removed from resulting array.
117118
*
118-
* @param array $originalResult
119+
* @param array|Closure $originalResult
119120
* @param array $productStockStatusMap
120121
* @param array $expectedResult
121122
* @dataProvider outOfStockProductDataProvider
@@ -125,32 +126,44 @@ public function testFilterRemovesOutOfStockProducts(
125126
array $productStockStatusMap,
126127
array $expectedResult
127128
): void {
129+
$finalOriginalResult = [];
130+
131+
if (is_array($originalResult)) {
132+
foreach ($originalResult as $result) {
133+
if (is_callable($result)) {
134+
$finalOriginalResult[] = $result($this);
135+
}
136+
}
137+
} else {
138+
$finalOriginalResult[] = $originalResult($this);
139+
}
140+
128141
$this->stockRegistryMock->method('getProductStockStatus')
129142
->willReturnMap($productStockStatusMap);
130143

131-
$result = $this->unit->afterPrepareForCartAdvanced(
144+
$finalResult = $this->unit->afterPrepareForCartAdvanced(
132145
$this->subjectMock,
133-
$originalResult,
146+
$finalOriginalResult,
134147
$this->buyRequestMock
135148
);
136149

137-
$this->assertSame($expectedResult, $result);
150+
$finalExpectedResult = [];
151+
foreach ($expectedResult as $key => $result) {
152+
$finalExpectedResult[$key] = $result($this);
153+
}
154+
155+
$this->assertEquals($finalExpectedResult, $finalResult);
138156
}
139157

140158
/**
141159
* Out of stock
142160
*
143161
* @return array
144162
*/
145-
public function outOfStockProductDataProvider(): array
163+
public static function outOfStockProductDataProvider(): array
146164
{
147-
$product1 = $this->createProductMock();
148-
$product1->method('getId')
149-
->willReturn(123);
150-
151-
$product2 = $this->createProductMock();
152-
$product2->method('getId')
153-
->willReturn(321);
165+
$product1 = static fn (self $testCase) => $testCase->createProductMockById(123);
166+
$product2 = static fn (self $testCase) => $testCase->createProductMockById(321);
154167

155168
return [
156169
[
@@ -174,6 +187,17 @@ public function outOfStockProductDataProvider(): array
174187
];
175188
}
176189

190+
/**ß
191+
* @param int $id
192+
* @return MockObject|Product
193+
*/
194+
public function createProductMockById(int $id): MockObject|Product
195+
{
196+
$product = $this->getMockBuilder(Product::class)->disableOriginalConstructor()->getMock();
197+
$product->method('getId')->willReturn($id);
198+
return $product;
199+
}
200+
177201
/**
178202
* Provider of non array type "result" parameters.
179203
*

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ protected function setUp(): void
8080
*/
8181
public function testLoadData(array $indexersData, array $states)
8282
{
83+
$finalStates = [];
84+
85+
foreach ($states as $key => $state) {
86+
if (is_callable($state)) {
87+
$finalStates[$key] = $state($this);
88+
}
89+
}
90+
8391
$statesCollection = $this->getMockBuilder(StateCollection::class)
8492
->disableOriginalConstructor()
8593
->getMock();
@@ -88,12 +96,12 @@ public function testLoadData(array $indexersData, array $states)
8896
->method('create')
8997
->willReturn($statesCollection);
9098
$statesCollection->method('getItems')
91-
->willReturn($states);
99+
->willReturn($finalStates);
92100

93101
$calls = [];
94102
foreach ($indexersData as $indexerId => $indexerData) {
95103
$indexer = $this->getIndexerMock($indexerData);
96-
$state = $states[$indexerId] ?? '';
104+
$state = $finalStates[$indexerId] ?? '';
97105
$indexer
98106
->expects($this->once())
99107
->method('load')
@@ -124,7 +132,7 @@ public function testLoadData(array $indexersData, array $states)
124132
/**
125133
* @return array
126134
*/
127-
public function loadDataDataProvider()
135+
public static function loadDataDataProvider()
128136
{
129137
return [
130138
[
@@ -140,8 +148,8 @@ public function loadDataDataProvider()
140148
],
141149
],
142150
'states' => [
143-
'indexer_2' => $this->getStateMock(['indexer_id' => 'indexer_2']),
144-
'indexer_3' => $this->getStateMock(['indexer_id' => 'indexer_3']),
151+
'indexer_2' => static fn (self $testCase) => $testCase->getStateMock(['indexer_id' => 'indexer_2']),
152+
'indexer_3' => static fn (self $testCase) => $testCase->getStateMock(['indexer_id' => 'indexer_3']),
145153
],
146154
]
147155
];

app/code/Magento/Quote/Test/Unit/Model/CartMutexTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,18 @@ protected function setUp(): void
5555
/**
5656
* Tests cart mutex execution with different callables.
5757
*
58-
* @param callable $callable
58+
* @param callable|string $callable
5959
* @param array $args
6060
* @param mixed $expectedResult
6161
* @return void
6262
* @dataProvider callableDataProvider
6363
*/
64-
public function testSuccessfulExecution(callable $callable, array $args, $expectedResult): void
64+
public function testSuccessfulExecution(callable|string $callable, array $args, $expectedResult): void
6565
{
66+
if ($callable === 'privateMethod') {
67+
$callable = \Closure::fromCallable([$this, 'privateMethod']);
68+
}
69+
6670
$cartId = 1;
6771
$this->lockManager->expects($this->once())
6872
->method('lock')
@@ -80,7 +84,7 @@ public function testSuccessfulExecution(callable $callable, array $args, $expect
8084
/**
8185
* @return array[]
8286
*/
83-
public function callableDataProvider(): array
87+
public static function callableDataProvider(): array
8488
{
8589
$functionWithArgs = function (int $a, int $b) {
8690
return $a + $b;
@@ -94,7 +98,7 @@ public function callableDataProvider(): array
9498
['callable' => $functionWithoutArgs, 'args' => [], 'expectedResult' => 'Function without args'],
9599
['callable' => $functionWithArgs, 'args' => [1,2], 'expectedResult' => 3],
96100
[
97-
'callable' => \Closure::fromCallable([$this, 'privateMethod']),
101+
'callable' => 'privateMethod',
98102
'args' => ['test'],
99103
'expectedResult' => 'test'
100104
],

app/code/Magento/Security/Test/Unit/Model/ConfigTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,15 @@ public function testGetPasswordResetProtectionType($resetMethod, $scope)
143143
$this->scopeMock->expects($this->once())
144144
->method('getCurrentScope')
145145
->willReturn($scope);
146-
$this->assertEquals($resetMethod, $this->model->getPasswordResetProtectionType($scope));
146+
$this->assertEquals($resetMethod, $this->model->getPasswordResetProtectionType());
147147
}
148148

149149
/**
150150
* @return array
151151
*/
152-
public function dataProviderResetMethodValues()
152+
public static function dataProviderResetMethodValues()
153153
{
154-
$objectManager = new ObjectManager($this);
155-
$resetMethodSource = $objectManager->getObject(
156-
ResetMethod::class
157-
);
154+
$resetMethodSource = new ResetMethod();
158155

159156
$optionKeys = array_keys($resetMethodSource->toArray());
160157
$data = [];

0 commit comments

Comments
 (0)