Skip to content

Commit ca90622

Browse files
Raj MohanRaj Mohan
authored andcommitted
AC-12092 Magento Framework PHPUnit10 Fixes
1 parent cf01c5f commit ca90622

File tree

7 files changed

+220
-47
lines changed

7 files changed

+220
-47
lines changed

lib/internal/Magento/Framework/Model/Test/Unit/ActionValidator/RemoveActionTest.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ class RemoveActionTest extends TestCase
2828
*/
2929
public function testIsAllowed($modelToCheck, $protectedModel, $secureArea, $expectedResult)
3030
{
31+
if (is_callable($modelToCheck)) {
32+
$modelToCheck = $modelToCheck($this);
33+
}
34+
if (is_callable($protectedModel)) {
35+
$protectedModel = $protectedModel($this);
36+
}
3137
$registryMock = $this->createMock(Registry::class);
3238
$registryMock->expects($this->once())
3339
->method('registry')->with('isSecureArea')->willReturn($secureArea);
@@ -42,10 +48,11 @@ public function testIsAllowed($modelToCheck, $protectedModel, $secureArea, $expe
4248
/**
4349
* return array
4450
*/
45-
public function isAllowedDataProvider()
51+
public static function isAllowedDataProvider()
4652
{
47-
$productMock = $this->createMock(Product::class);
48-
$bannerMock = $this->createMock(Wishlist::class);
53+
$productMock = static fn(self $testCase) => $testCase->createProductMock();
54+
$bannerMock = static fn(self $testCase) => $testCase->createWishlistMock()['mock'];
55+
$bannerMockClass = static fn(self $testCase) => $testCase->createWishlistMock()['class'];
4956

5057
return [
5158
[
@@ -56,16 +63,31 @@ public function isAllowedDataProvider()
5663
],
5764
[
5865
'modelToCheck' => $bannerMock,
59-
'protectedModel' => get_class($bannerMock),
66+
'protectedModel' => $bannerMockClass,
6067
'secureArea' => false,
6168
'expectedResult' => false
6269
],
6370
[
6471
'modelToCheck' => $bannerMock,
65-
'protectedModel' => get_class($bannerMock),
72+
'protectedModel' => $bannerMockClass,
6673
'secureArea' => true,
6774
'expectedResult' => true
6875
],
6976
];
7077
}
78+
79+
public function createProductMock()
80+
{
81+
return $this->createMock(Product::class);
82+
}
83+
84+
public function createWishlistMock()
85+
{
86+
$wishlistMock = $this->createMock(Wishlist::class);
87+
$wishlistMockClass = get_class($wishlistMock);
88+
return [
89+
'class' => $wishlistMockClass,
90+
'mock' => $wishlistMock
91+
];
92+
}
7193
}

lib/internal/Magento/Framework/Model/Test/Unit/ResourceModel/Db/Collection/AbstractCollectionTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,19 @@ public function testGetSelectCached()
215215
*/
216216
public function testGetSelect($idFieldNameRet, $getPartRet, $expected)
217217
{
218+
if (is_callable($idFieldNameRet['column_alias'])) {
219+
$idFieldNameRet['column_alias'] = $idFieldNameRet['column_alias']($this);
220+
}
221+
if (is_callable($getPartRet[0][1])) {
222+
$getPartRet[0][1] = $getPartRet[0][1]($this);
223+
}
224+
if (is_callable($expected[0][1]['column_alias'])) {
225+
$expected[0][1]['column_alias'] = $expected[0][1]['column_alias']($this);
226+
}
227+
if (is_callable($expected['alias'][1])) {
228+
$expected['alias'][1] = $expected['alias'][1]($this);
229+
}
230+
218231
$this->resourceMock
219232
->expects($this->any())
220233
->method('getIdFieldName')
@@ -238,9 +251,9 @@ public function testGetSelect($idFieldNameRet, $getPartRet, $expected)
238251
/**
239252
* @return array
240253
*/
241-
public function getSelectDataProvider()
254+
public static function getSelectDataProvider(): array
242255
{
243-
$columnMock = $this->createPartialMock(\Zend_Db_Expr::class, ['__toString']);
256+
$columnMock = static fn (self $testCase) => $testCase->getZendDbExprPartialMock();
244257

245258
return [
246259
[
@@ -254,6 +267,11 @@ public function getSelectDataProvider()
254267
];
255268
}
256269

270+
public function getZendDbExprPartialMock()
271+
{
272+
return $this->createPartialMock(\Zend_Db_Expr::class, ['__toString']);
273+
}
274+
257275
/**
258276
* @dataProvider addFieldToSelectDataProvider
259277
*/

lib/internal/Magento/Framework/ObjectManager/Test/Unit/Factory/FactoryTest.php

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Framework\ObjectManager\Test\Unit\Factory\Fixture\SemiVariadic;
1818
use Magento\Framework\ObjectManager\Test\Unit\Factory\Fixture\Two;
1919
use Magento\Framework\ObjectManager\Test\Unit\Factory\Fixture\Variadic;
20+
use PHPUnit\Framework\MockObject\Exception;
2021
use PHPUnit\Framework\TestCase;
2122

2223
class FactoryTest extends TestCase
@@ -215,6 +216,28 @@ public function testCreateUsingVariadic(
215216
$expectedArg0,
216217
$expectedArg1
217218
) {
219+
if (isset($createArgs['oneScalars'])) {
220+
if (is_array($createArgs['oneScalars'])) {
221+
foreach ($createArgs['oneScalars'] as &$args) {
222+
if (is_callable($args)) {
223+
$args = $args($this);
224+
}
225+
}
226+
} else {
227+
if (is_callable($createArgs['oneScalars'])) {
228+
$createArgs['oneScalars'] = $createArgs['oneScalars']($this);
229+
}
230+
}
231+
}
232+
233+
if (is_callable($expectedArg0)) {
234+
$expectedArg0 = $expectedArg0($this);
235+
}
236+
237+
if (is_callable($expectedArg1)) {
238+
$expectedArg1 = $expectedArg1($this);
239+
}
240+
218241
$type = Variadic::class;
219242
$definitions = $this->getMockForAbstractClass(DefinitionInterface::class);
220243

@@ -238,17 +261,17 @@ public function testCreateUsingVariadic(
238261
? $factory->create($type)
239262
: $factory->create($type, $createArgs);
240263

241-
$this->assertSame($expectedArg0, $variadic->getOneScalarByKey(0));
242-
$this->assertSame($expectedArg1, $variadic->getOneScalarByKey(1));
264+
$this->assertEquals($expectedArg0, $variadic->getOneScalarByKey(0));
265+
$this->assertEquals($expectedArg1, $variadic->getOneScalarByKey(1));
243266
}
244267

245268
/**
246269
* @return array
247270
*/
248-
public function testCreateUsingVariadicDataProvider()
271+
public static function testCreateUsingVariadicDataProvider()
249272
{
250-
$oneScalar1 = $this->createMock(OneScalar::class);
251-
$oneScalar2 = $this->createMock(OneScalar::class);
273+
$oneScalar1 = static fn (self $testCase) => $testCase->createScalarMock();
274+
$oneScalar2 = static fn (self $testCase) => $testCase->createScalarMock();
252275

253276
return [
254277
'without_args' => [
@@ -333,6 +356,28 @@ public function testCreateUsingSemiVariadic(
333356
$expectedArg0,
334357
$expectedArg1
335358
) {
359+
if (isset($createArgs['oneScalars'])) {
360+
if (is_array($createArgs['oneScalars'])) {
361+
foreach ($createArgs['oneScalars'] as &$args) {
362+
if (is_callable($args)) {
363+
$args = $args($this);
364+
}
365+
}
366+
} else {
367+
if (is_callable($createArgs['oneScalars'])) {
368+
$createArgs['oneScalars'] = $createArgs['oneScalars']($this);
369+
}
370+
}
371+
}
372+
373+
if (is_callable($expectedArg0)) {
374+
$expectedArg0 = $expectedArg0($this);
375+
}
376+
377+
if (is_callable($expectedArg1)) {
378+
$expectedArg1 = $expectedArg1($this);
379+
}
380+
336381
$type = SemiVariadic::class;
337382
$definitions = $this->getMockForAbstractClass(DefinitionInterface::class);
338383

@@ -363,18 +408,18 @@ public function testCreateUsingSemiVariadic(
363408
? $factory->create($type)
364409
: $factory->create($type, $createArgs);
365410

366-
$this->assertSame($expectedFooValue, $semiVariadic->getFoo());
367-
$this->assertSame($expectedArg0, $semiVariadic->getOneScalarByKey(0));
368-
$this->assertSame($expectedArg1, $semiVariadic->getOneScalarByKey(1));
411+
$this->assertEquals($expectedFooValue, $semiVariadic->getFoo());
412+
$this->assertEquals($expectedArg0, $semiVariadic->getOneScalarByKey(0));
413+
$this->assertEquals($expectedArg1, $semiVariadic->getOneScalarByKey(1));
369414
}
370415

371416
/**
372417
* @return array
373418
*/
374-
public function testCreateUsingSemiVariadicDataProvider()
419+
public static function testCreateUsingSemiVariadicDataProvider()
375420
{
376-
$oneScalar1 = $this->createMock(OneScalar::class);
377-
$oneScalar2 = $this->createMock(OneScalar::class);
421+
$oneScalar1 = static fn (self $testCase) => $testCase->createScalarMock();
422+
$oneScalar2 = static fn (self $testCase) => $testCase->createScalarMock();
378423

379424
return [
380425
'without_args' => [
@@ -450,4 +495,12 @@ public function testCreateUsingSemiVariadicDataProvider()
450495
],
451496
];
452497
}
498+
499+
/**
500+
* @throws Exception
501+
*/
502+
public function createScalarMock()
503+
{
504+
return $this->createMock(OneScalar::class);
505+
}
453506
}

lib/internal/Magento/Framework/Profiler/Test/Unit/Driver/Standard/Output/FactoryTest.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php declare(strict_types=1);
22
/**
3-
* Test class for \Magento\Framework\Profiler\Driver\Standard\Output\Factory
4-
*
53
* Copyright © Magento, Inc. All rights reserved.
64
* See COPYING.txt for license details.
5+
*
6+
* Test class for \Magento\Framework\Profiler\Driver\Standard\Output\Factory
77
*/
88
namespace Magento\Framework\Profiler\Test\Unit\Driver\Standard\Output;
99

@@ -60,6 +60,14 @@ public function testDefaultConstructor()
6060
*/
6161
public function testCreate($configData, $expectedClass)
6262
{
63+
if (isset($configData['type']) && is_callable($configData['type'])) {
64+
$configData['type'] = $configData['type']($this);
65+
}
66+
67+
if (is_callable($expectedClass)) {
68+
$expectedClass = $expectedClass($this);
69+
}
70+
6371
$driver = $this->_factory->create($configData);
6472
$this->assertInstanceOf($expectedClass, $driver);
6573
$this->assertInstanceOf(OutputInterface::class, $driver);
@@ -68,7 +76,29 @@ public function testCreate($configData, $expectedClass)
6876
/**
6977
* @return array
7078
*/
71-
public function createDataProvider()
79+
public static function createDataProvider(): array
80+
{
81+
return [
82+
'Prefix and concrete type' => [
83+
['type' => 'test'],
84+
static fn(self $testCase) => $testCase->getOutputClassMock()['testOutputClass']
85+
],
86+
'Prefix and default type' => [
87+
[],
88+
static fn(self $testCase) => $testCase->getOutputClassMock()['defaultOutputClass']
89+
],
90+
'Concrete class' => [
91+
['type' => static fn(self $testCase) => $testCase->getOutputClassMock()['testOutputClass']],
92+
static fn(self $testCase) => $testCase->getOutputClassMock()['testOutputClass']
93+
],
94+
];
95+
}
96+
97+
/**
98+
* @return array
99+
* @throws \PHPUnit\Framework\MockObject\Exception
100+
*/
101+
public function getOutputClassMock(): array
72102
{
73103
$defaultOutputClassMock = $this->getMockForAbstractClass(
74104
OutputInterface::class,
@@ -95,9 +125,8 @@ public function createDataProvider()
95125
$testOutputClass = get_class($testOutputClassMock);
96126

97127
return [
98-
'Prefix and concrete type' => [['type' => 'test'], $testOutputClass],
99-
'Prefix and default type' => [[], $defaultOutputClass],
100-
'Concrete class' => [['type' => $testOutputClass], $testOutputClass]
128+
'defaultOutputClass' => $defaultOutputClass,
129+
'testOutputClass' => $testOutputClass
101130
];
102131
}
103132

lib/internal/Magento/Framework/View/Test/Unit/Asset/RepositoryTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ public function testUpdateDesignParamsWrongTheme()
190190
*/
191191
public function testUpdateDesignParams($params, $result)
192192
{
193+
if (is_callable($result['themeModel'])) {
194+
$result['themeModel'] = $result['themeModel']($this);
195+
}
193196
$this->themeProvider
194197
->expects($this->any())
195198
->method('getThemeByFullPath')
@@ -246,12 +249,18 @@ public function testUpdateDesignParamsWithThemeId()
246249
/**
247250
* @return array
248251
*/
249-
public function updateDesignParamsDataProvider()
252+
public static function updateDesignParamsDataProvider()
250253
{
251254
return [
252255
[
253256
['area' => 'AREA'],
254-
['area' => 'AREA', 'themeModel' => $this->getThemeMock(), 'module' => false, 'locale' => 'locale']],
257+
[
258+
'area' => 'AREA',
259+
'themeModel' => static fn(self $testCase) => $testCase->getThemeMock(),
260+
'module' => false,
261+
'locale' => 'locale'
262+
]
263+
],
255264
[
256265
['themeId' => 'ThemeID'],
257266
[

0 commit comments

Comments
 (0)