Skip to content

Commit 8e55b0b

Browse files
committed
AC-15105: [CE] PHPUnit 12: Upgrade Payment & Shipping related test cases
1 parent d910076 commit 8e55b0b

32 files changed

+250
-256
lines changed

app/code/Magento/Paypal/Test/Unit/Block/Adminhtml/Order/ViewTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99

1010
use Magento\Framework\Exception\LocalizedException;
1111
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12+
use Magento\Framework\View\Helper\SecureHtmlRenderer;
1213
use Magento\Paypal\Block\Adminhtml\Order\View;
1314
use Magento\Paypal\Model\Adminhtml\Express;
1415
use Magento\Sales\Model\Order;
1516
use Magento\Sales\Model\Order\Payment;
17+
use PHPUnit\Framework\Attributes\DataProvider;
1618
use PHPUnit\Framework\MockObject\MockObject;
1719
use PHPUnit\Framework\TestCase;
1820

@@ -45,6 +47,15 @@ protected function setUp(): void
4547
{
4648
$objectManager = new ObjectManager($this);
4749

50+
$secureRendererMock = $this->createMock(SecureHtmlRenderer::class);
51+
$objects = [
52+
[
53+
SecureHtmlRenderer::class,
54+
$secureRendererMock
55+
]
56+
];
57+
$objectManager->prepareObjectManager($objects);
58+
4859
$this->order = $this->createPartialMock(
4960
Order::class,
5061
['canUnhold', 'isPaymentReview', 'getState', 'isCanceled', 'getPayment']
@@ -76,8 +87,8 @@ protected function setUp(): void
7687
* @param string $orderState
7788
* @param bool $canAuthorize
7889
* @throws LocalizedException
79-
* @dataProvider orderDataProvider
8090
*/
91+
#[DataProvider('orderDataProvider')]
8192
public function testIsOrderAuthorizationAllowed(
8293
bool $canUnhold,
8394
bool $isPaymentReview,

app/code/Magento/Paypal/Test/Unit/Block/Adminhtml/Store/SwitcherPluginTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1313
use Magento\Paypal\Block\Adminhtml\Store\SwitcherPlugin as StoreSwitcherBlockPlugin;
1414
use Magento\Paypal\Model\Config\StructurePlugin as ConfigStructurePlugin;
15+
use PHPUnit\Framework\Attributes\DataProvider;
1516
use PHPUnit\Framework\MockObject\MockObject;
1617
use PHPUnit\Framework\TestCase;
1718

@@ -39,11 +40,8 @@ class SwitcherPluginTest extends TestCase
3940

4041
protected function setUp(): void
4142
{
42-
$this->subjectMock = $this->getMockBuilder(StoreSwitcherBlock::class)
43-
->disableOriginalConstructor()
44-
->getMock();
45-
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
46-
->getMockForAbstractClass();
43+
$this->subjectMock = $this->createMock(StoreSwitcherBlock::class);
44+
$this->requestMock = $this->createMock(RequestInterface::class);
4745

4846
$this->objectManagerHelper = new ObjectManagerHelper($this);
4947
$this->plugin = $this->objectManagerHelper->getObject(StoreSwitcherBlockPlugin::class);
@@ -52,9 +50,8 @@ protected function setUp(): void
5250
/**
5351
* @param string|null $countryParam
5452
* @param array $getUrlParams
55-
*
56-
* @dataProvider beforeGetUrlDataProvider
5753
*/
54+
#[DataProvider('beforeGetUrlDataProvider')]
5855
public function testBeforeGetUrl($countryParam, $getUrlParams)
5956
{
6057
$this->requestMock->expects(static::once())

app/code/Magento/Paypal/Test/Unit/Block/Adminhtml/System/Config/Field/CountryTest.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@
88
namespace Magento\Paypal\Test\Unit\Block\Adminhtml\System\Config\Field;
99

1010
use Magento\Backend\Model\Url;
11+
use Magento\Directory\Helper\Data as DirectoryHelper;
1112
use Magento\Framework\App\RequestInterface;
1213
use Magento\Framework\Data\Form\Element\AbstractElement;
14+
use Magento\Framework\Json\Helper\Data as JsonHelper;
1315
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1416
use Magento\Framework\View\Helper\Js;
1517
use Magento\Framework\View\Helper\SecureHtmlRenderer;
1618
use Magento\Paypal\Block\Adminhtml\System\Config\Field\Country;
1719
use Magento\Paypal\Model\Config\StructurePlugin;
20+
use PHPUnit\Framework\Attributes\DataProvider;
1821
use PHPUnit\Framework\Constraint\StringContains;
1922
use PHPUnit\Framework\MockObject\MockObject;
2023
use PHPUnit\Framework\TestCase;
21-
use Magento\Directory\Helper\Data as DirectoryHelper;
2224

2325
class CountryTest extends TestCase
2426
{
@@ -58,13 +60,17 @@ class CountryTest extends TestCase
5860
protected function setUp(): void
5961
{
6062
$helper = new ObjectManager($this);
61-
$this->_element = $this->getMockForAbstractClass(
63+
64+
$jsonHelperMock = $this->createMock(JsonHelper::class);
65+
$directoryHelperMock = $this->createMock(DirectoryHelper::class);
66+
$objects = [
67+
[JsonHelper::class, $jsonHelperMock],
68+
[DirectoryHelper::class, $directoryHelperMock]
69+
];
70+
$helper->prepareObjectManager($objects);
71+
72+
$this->_element = $this->createPartialMock(
6273
AbstractElement::class,
63-
[],
64-
'',
65-
false,
66-
true,
67-
true,
6874
['getHtmlId', 'getElementHtml', 'getName']
6975
);
7076
$this->_element->expects($this->any())
@@ -76,7 +82,7 @@ protected function setUp(): void
7682
$this->_element->expects($this->any())
7783
->method('getName')
7884
->willReturn('name');
79-
$this->_request = $this->getMockForAbstractClass(RequestInterface::class);
85+
$this->_request = $this->createMock(RequestInterface::class);
8086
$this->_jsHelper = $this->createMock(Js::class);
8187
$this->_url = $this->createMock(Url::class);
8288
$this->helper = $this->createMock(DirectoryHelper::class);
@@ -112,9 +118,9 @@ function (string $style, string $selector): string {
112118
* @param bool $inherit
113119
*
114120
* @return void
115-
* @dataProvider renderDataProvider
116121
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
117122
*/
123+
#[DataProvider('renderDataProvider')]
118124
public function testRender(
119125
?string $requestCountry,
120126
?string $requestDefaultCountry,

app/code/Magento/Paypal/Test/Unit/Block/Adminhtml/System/Config/Field/Enable/AbstractEnableTest.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@
88
namespace Magento\Paypal\Test\Unit\Block\Adminhtml\System\Config\Field\Enable;
99

1010
use Magento\Directory\Helper\Data as DirectoryHelper;
11+
use Magento\Framework\Data\Form;
12+
use Magento\Framework\Data\Form\Element\AbstractElement;
1113
use Magento\Framework\Data\Form\Element\CollectionFactory;
1214
use Magento\Framework\Escaper;
1315
use Magento\Framework\Json\Helper\Data as JsonHelper;
1416
use Magento\Framework\Math\Random;
15-
use Magento\Framework\Data\Form;
16-
use Magento\Framework\Data\Form\Element\AbstractElement;
17+
use Magento\Framework\TestFramework\Unit\Helper\MockCreationTrait;
1718
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1819
use Magento\Framework\View\Helper\SecureHtmlRenderer;
19-
use PHPUnit\Framework\MockObject\MockObject;
2020
use Magento\Paypal\Test\Unit\Block\Adminhtml\System\Config\Field\Enable\AbstractEnable\Stub;
21+
use PHPUnit\Framework\MockObject\MockObject;
2122
use PHPUnit\Framework\TestCase;
23+
use ReflectionClass;
2224

2325
/**
2426
* Class AbstractEnableTest
@@ -27,7 +29,9 @@
2729
*/
2830
class AbstractEnableTest extends TestCase
2931
{
30-
const EXPECTED_ATTRIBUTE = 'data-enable="stub"';
32+
use MockCreationTrait;
33+
34+
private const EXPECTED_ATTRIBUTE = 'data-enable="stub"';
3135

3236
/**
3337
* @var Stub
@@ -49,7 +53,7 @@ private function createMocks(array $classes): array
4953
{
5054
$mocks = [];
5155
foreach ($classes as $class) {
52-
$mocks[] = $this->getMockBuilder($class)->disableOriginalConstructor()->getMock();
56+
$mocks[] = $this->createMock($class);
5357
}
5458

5559
return $mocks;
@@ -64,8 +68,9 @@ protected function setUp(): void
6468
{
6569
$objectManager = new ObjectManager($this);
6670

67-
$randomMock = $this->getMockBuilder(Random::class)->disableOriginalConstructor()->getMock();
71+
$randomMock = $this->createMock(Random::class);
6872
$randomMock->method('getRandomString')->willReturn('12345abcdef');
73+
6974
$mockArguments = $this->createMocks([
7075
\Magento\Framework\Data\Form\Element\Factory::class,
7176
CollectionFactory::class,
@@ -74,22 +79,21 @@ protected function setUp(): void
7479
$mockArguments[] = [];
7580
$mockArguments[] = $this->createMock(SecureHtmlRenderer::class);
7681
$mockArguments[] = $randomMock;
77-
$this->elementMock = $this->getMockBuilder(AbstractElement::class)
78-
->addMethods(['getTooltip'])
79-
->onlyMethods(
80-
[
81-
'getHtmlId',
82-
'getForm'
83-
]
84-
)->setConstructorArgs($mockArguments)
85-
->getMockForAbstractClass();
82+
83+
$this->elementMock = $this->createPartialMockWithReflection(
84+
AbstractElement::class,
85+
['getHtmlId', 'getForm', 'getTooltip']
86+
);
8687

87-
$objectManager = new ObjectManager($this);
8888
$escaper = $objectManager->getObject(Escaper::class);
89-
$reflection = new \ReflectionClass($this->elementMock);
90-
$reflection_property = $reflection->getProperty('_escaper');
91-
$reflection_property->setAccessible(true);
92-
$reflection_property->setValue($this->elementMock, $escaper);
89+
$reflection = new ReflectionClass(AbstractElement::class);
90+
$reflectionProperty = $reflection->getProperty('_escaper');
91+
$reflectionProperty->setAccessible(true);
92+
$reflectionProperty->setValue($this->elementMock, $escaper);
93+
94+
$randomProperty = $reflection->getProperty('random');
95+
$randomProperty->setAccessible(true);
96+
$randomProperty->setValue($this->elementMock, $randomMock);
9397

9498
$objects = [
9599
[
@@ -135,11 +139,8 @@ public function testGetUiId()
135139
*/
136140
public function testRender()
137141
{
138-
$formMock = $this->getMockBuilder(Form::class)
139-
->addMethods(['getFieldNameSuffix'])
140-
->disableOriginalConstructor()
141-
->getMock();
142-
142+
$formMock = $this->createPartialMockWithReflection(Form::class, ['getFieldNameSuffix']);
143+
143144
$this->elementMock->expects($this->any())
144145
->method('getHtmlId')
145146
->willReturn('test-html-id');
@@ -149,7 +150,6 @@ public function testRender()
149150
$this->elementMock->expects($this->any())
150151
->method('getForm')
151152
->willReturn($formMock);
152-
153153
$formMock->expects($this->any())
154154
->method('getFieldNameSuffix')
155155
->willReturn('');

app/code/Magento/Paypal/Test/Unit/Block/Adminhtml/System/Config/Fieldset/GroupTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
use Magento\Config\Model\Config\Structure\Element\Group;
1212
use Magento\Framework\Data\Form\Element\AbstractElement;
1313
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
14+
use Magento\Framework\View\Helper\SecureHtmlRenderer;
1415
use Magento\User\Model\User;
16+
use PHPUnit\Framework\Attributes\DataProvider;
1517
use PHPUnit\Framework\MockObject\MockObject;
1618
use PHPUnit\Framework\TestCase;
17-
use Magento\Framework\View\Helper\SecureHtmlRenderer;
1819

1920
class GroupTest extends TestCase
2021
{
@@ -50,13 +51,8 @@ protected function setUp(): void
5051
{
5152
$helper = new ObjectManager($this);
5253
$this->_group = $this->createMock(Group::class);
53-
$this->_element = $this->getMockForAbstractClass(
54+
$this->_element = $this->createPartialMock(
5455
AbstractElement::class,
55-
[],
56-
'',
57-
false,
58-
true,
59-
true,
6056
['getHtmlId', 'getElementHtml', 'getName', 'getElements', 'getId']
6157
);
6258
$this->_element->expects($this->any())
@@ -94,18 +90,22 @@ function (string $style, string $selector): string {
9490
}
9591
);
9692

93+
$helper->prepareObjectManager([
94+
[SecureHtmlRenderer::class, $secureRendererMock]
95+
]);
96+
9797
$this->_model = $helper->getObject(
9898
\Magento\Paypal\Block\Adminhtml\System\Config\Fieldset\Group::class,
99-
['authSession' => $this->_authSession, 'secureRenderer' => $secureRendererMock]
99+
['authSession' => $this->_authSession]
100100
);
101101
$this->_model->setGroup($this->_group);
102102
}
103103

104104
/**
105105
* @param mixed $expanded
106106
* @param int $expected
107-
* @dataProvider isCollapseStateDataProvider
108107
*/
108+
#[DataProvider('isCollapseStateDataProvider')]
109109
public function testIsCollapseState($expanded, $expected)
110110
{
111111
$this->_user->setExtra(['configState' => []]);

app/code/Magento/Paypal/Test/Unit/Block/Adminhtml/System/Config/Fieldset/HintTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
namespace Magento\Paypal\Test\Unit\Block\Adminhtml\System\Config\Fieldset;
99

1010
use Magento\Framework\Data\Form\Element\AbstractElement;
11+
use Magento\Framework\TestFramework\Unit\Helper\MockCreationTrait;
1112
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1213
use Magento\Paypal\Block\Adminhtml\System\Config\Fieldset\Hint;
1314
use PHPUnit\Framework\MockObject\MockObject;
1415
use PHPUnit\Framework\TestCase;
1516

1617
class HintTest extends TestCase
1718
{
19+
use MockCreationTrait;
20+
1821
/**
1922
* @var Hint
2023
*/
@@ -27,15 +30,14 @@ class HintTest extends TestCase
2730

2831
protected function setUp(): void
2932
{
30-
$om = new ObjectManager($this);
33+
$objectManager = new ObjectManager($this);
3134

32-
$this->element = $this->getMockBuilder(AbstractElement::class)
33-
->addMethods(['getComment'])
34-
->onlyMethods(['getHtmlId'])
35-
->disableOriginalConstructor()
36-
->getMockForAbstractClass();
35+
$this->element = $this->createPartialMockWithReflection(
36+
AbstractElement::class,
37+
['getComment', 'getHtmlId']
38+
);
3739

38-
$this->block = $om->getObject(Hint::class);
40+
$this->block = $objectManager->getObject(Hint::class);
3941
}
4042

4143
/**

app/code/Magento/Paypal/Test/Unit/Block/Adminhtml/System/Config/Fieldset/PaymentTest.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\Data\Form\Element\AbstractElement;
1313
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1414
use Magento\Paypal\Block\Adminhtml\System\Config\Fieldset\Payment;
15+
use PHPUnit\Framework\Attributes\DataProvider;
1516
use PHPUnit\Framework\MockObject\MockObject;
1617
use PHPUnit\Framework\TestCase;
1718

@@ -20,8 +21,8 @@ class PaymentTest extends TestCase
2021
/**#@+
2122
* Activity config path
2223
*/
23-
const CONFIG_PATH_ACTIVE = 'payment/path/active';
24-
const CONFIG_PATH_NOT_ACTIVE = 'payment/path/not-active';
24+
private const CONFIG_PATH_ACTIVE = 'payment/path/active';
25+
private const CONFIG_PATH_NOT_ACTIVE = 'payment/path/not-active';
2526
/**#@-*/
2627

2728
/**
@@ -48,13 +49,8 @@ protected function setUp(): void
4849
{
4950
$helper = new ObjectManager($this);
5051
$this->_group = $this->createMock(Group::class);
51-
$this->_element = $this->getMockForAbstractClass(
52+
$this->_element = $this->createPartialMock(
5253
AbstractElement::class,
53-
[],
54-
'',
55-
false,
56-
true,
57-
true,
5854
['getHtmlId', 'getElementHtml', 'getName', 'getElements', 'getId']
5955
);
6056
$this->_element->expects($this->any())
@@ -80,9 +76,7 @@ protected function setUp(): void
8076
$this->_model->setGroup($this->_group);
8177
}
8278

83-
/**
84-
* @dataProvider isPaymentEnabledDataProvider
85-
*/
79+
#[DataProvider('isPaymentEnabledDataProvider')]
8680
public function testIsPaymentEnabled($groupConfig, $expected)
8781
{
8882
$this->_element->setGroup($groupConfig);

0 commit comments

Comments
 (0)