Skip to content

Commit 4ac86e2

Browse files
AC-15635: PHPUnit 12 Upgrade | Optimize code
1 parent 8677883 commit 4ac86e2

File tree

5 files changed

+65
-125
lines changed

5 files changed

+65
-125
lines changed

app/code/Magento/AdvancedPricingImportExport/Test/Unit/Helper/AdvancedPricingExportTestHelper.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
namespace Magento\AdvancedPricingImportExport\Test\Unit\Helper;
99

1010
use Magento\AdvancedPricingImportExport\Model\Export\AdvancedPricing;
11+
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
12+
use Magento\Store\Model\Store;
1113

1214
/**
1315
* Test helper for AdvancedPricing Export
@@ -164,14 +166,14 @@ public function _customHeadersMapping($rowData)
164166
/**
165167
* Prepare entity collection
166168
*
167-
* @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection
169+
* @param AbstractCollection $collection
168170
* @return $this
169171
*/
170-
public function _prepareEntityCollection(\Magento\Eav\Model\Entity\Collection\AbstractCollection $collection)
172+
public function _prepareEntityCollection(AbstractCollection $collection)
171173
{
172174
$this->entityCollection = $collection;
173175
// Call setStoreId on the collection to satisfy the test expectation
174-
$collection->setStoreId(\Magento\Store\Model\Store::DEFAULT_STORE_ID);
176+
$collection->setStoreId(Store::DEFAULT_STORE_ID);
175177
return $this;
176178
}
177179

app/code/Magento/AdvancedPricingImportExport/Test/Unit/Model/Export/AdvancedPricingTest.php

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
3838
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
3939
use Magento\ImportExport\Model\Export\Config as ExportConfig;
40-
use ReflectionClass;
40+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
4141
use stdClass;
4242

4343
/**
@@ -224,12 +224,15 @@ protected function setUp(): void
224224
'correctExportData'
225225
]);
226226

227-
$this->advancedPricing = new AdvancedPricingExportTestHelper();
228-
229-
// Manually set the required properties that would normally be set by the parent constructor
230-
$this->setPropertyValue($this->advancedPricing, '_storeResolver', $this->storeResolver);
231-
$this->setPropertyValue($this->advancedPricing, '_groupRepository', $this->groupRepository);
232-
$this->setPropertyValue($this->advancedPricing, '_resource', $this->resource);
227+
$objectManager = new ObjectManager($this);
228+
$this->advancedPricing = $objectManager->getObject(
229+
AdvancedPricingExportTestHelper::class,
230+
[
231+
'_storeResolver' => $this->storeResolver,
232+
'_groupRepository' => $this->groupRepository,
233+
'_resource' => $this->resource
234+
]
235+
);
233236
}
234237

235238
/**
@@ -308,38 +311,4 @@ protected function tearDown(): void
308311
{
309312
unset($this->object);
310313
}
311-
312-
/**
313-
* Get any object property value.
314-
*
315-
* @param $object
316-
* @param $property
317-
* @return mixed
318-
* @throws \ReflectionException
319-
*/
320-
protected function getPropertyValue($object, $property)
321-
{
322-
$reflection = new ReflectionClass(get_class($object));
323-
$reflectionProperty = $reflection->getProperty($property);
324-
$reflectionProperty->setAccessible(true);
325-
return $reflectionProperty->getValue($object);
326-
}
327-
328-
/**
329-
* Set object property value.
330-
*
331-
* @param $object
332-
* @param $property
333-
* @param $value
334-
* @return mixed
335-
* @throws \ReflectionException
336-
*/
337-
protected function setPropertyValue(&$object, $property, $value)
338-
{
339-
$reflection = new ReflectionClass(get_class($object));
340-
$reflectionProperty = $reflection->getProperty($property);
341-
$reflectionProperty->setAccessible(true);
342-
$reflectionProperty->setValue($object, $value);
343-
return $object;
344-
}
345314
}

app/code/Magento/AdvancedPricingImportExport/Test/Unit/Model/Import/AdvancedPricing/Validator/TierPriceTest.php

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
use Magento\Framework\App\ResourceConnection;
2323
use Magento\Framework\Json\Helper\Data;
2424
use Magento\Framework\Stdlib\StringUtils;
25+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
2526
use Magento\ImportExport\Helper\Data as ImportExportHelperData;
2627
use Magento\ImportExport\Model\ResourceModel\Helper;
2728
use Magento\ImportExport\Model\ResourceModel\Import\Data as ResourceImportData;
2829
use PHPUnit\Framework\MockObject\MockObject;
2930
use PHPUnit\Framework\TestCase;
30-
use ReflectionClass;
3131

3232
/**
3333
* @SuppressWarnings(PHPMD)
@@ -54,8 +54,14 @@ class TierPriceTest extends TestCase
5454
*/
5555
protected $tierPrice;
5656

57+
/**
58+
* @var ObjectManager
59+
*/
60+
protected $objectManager;
61+
5762
protected function setUp(): void
5863
{
64+
$this->objectManager = new ObjectManager($this);
5965
$this->groupRepository = $this->createMock(GroupRepositoryInterface::class);
6066

6167
$this->searchCriteriaBuilder = $this->createMock(SearchCriteriaBuilder::class);
@@ -114,13 +120,16 @@ public function testInitAddToCustomerGroups()
114120

115121
$this->tierPrice->init(null);
116122

117-
$this->assertEquals($expectedCustomerGroups, $this->getPropertyValue($this->tierPrice, 'customerGroups'));
123+
$reflection = new \ReflectionClass($this->tierPrice);
124+
$property = $reflection->getProperty('customerGroups');
125+
$property->setAccessible(true);
126+
$this->assertEquals($expectedCustomerGroups, $property->getValue($this->tierPrice));
118127
}
119128

120129
public function testIsValidResultTrue()
121130
{
122131
$this->tierPrice->expects($this->once())->method('isValidValueAndLength')->willReturn(false);
123-
$this->setPropertyValue($this->tierPrice, 'customerGroups', true);
132+
$this->objectManager->setBackwardCompatibleProperty($this->tierPrice, 'customerGroups', true);
124133

125134
$result = $this->tierPrice->isValid([]);
126135
$this->assertTrue($result);
@@ -152,7 +161,7 @@ public function testIsValidAddMessagesCall($value, $hasEmptyColumns, $customerGr
152161

153162
$this->tierPrice->expects($this->once())->method('isValidValueAndLength')->willReturn(true);
154163
$this->tierPrice->method('hasEmptyColumns')->willReturn($hasEmptyColumns);
155-
$this->setPropertyValue($this->tierPrice, 'customerGroups', $customerGroups);
164+
$this->objectManager->setBackwardCompatibleProperty($this->tierPrice, 'customerGroups', $customerGroups);
156165

157166
$searchCriteria = $this->createMock(SearchCriteria::class);
158167
$this->searchCriteriaBuilder->method('create')->willReturn($searchCriteria);
@@ -330,40 +339,4 @@ public static function isValidAddMessagesCallDataProvider()
330339
],
331340
];
332341
}
333-
334-
/**
335-
* Get any object property value.
336-
*
337-
* @param object $object
338-
* @param string $property
339-
* @return mixed
340-
* @throws \ReflectionException
341-
*/
342-
protected function getPropertyValue($object, $property)
343-
{
344-
$reflection = new ReflectionClass(get_class($object));
345-
$reflectionProperty = $reflection->getProperty($property);
346-
$reflectionProperty->setAccessible(true);
347-
348-
return $reflectionProperty->getValue($object);
349-
}
350-
351-
/**
352-
* Set object property value.
353-
*
354-
* @param object $object
355-
* @param string $property
356-
* @param mixed $value
357-
* @return object
358-
* @throws \ReflectionException
359-
*/
360-
protected function setPropertyValue(&$object, $property, $value)
361-
{
362-
$reflection = new ReflectionClass(get_class($object));
363-
$reflectionProperty = $reflection->getProperty($property);
364-
$reflectionProperty->setAccessible(true);
365-
$reflectionProperty->setValue($object, $value);
366-
367-
return $object;
368-
}
369342
}

app/code/Magento/AdvancedPricingImportExport/Test/Unit/Model/Import/AdvancedPricingTest.php

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Magento\Framework\Json\Helper\Data;
2828
use Magento\Framework\Stdlib\DateTime\DateTime;
2929
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
30+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
3031
use Magento\Catalog\Helper\Data as CatalogHelperData;
3132
use Magento\Catalog\Model\Product as CatalogProduct;
3233
use Magento\ImportExport\Helper\Data as ImportExportHelperData;
@@ -151,13 +152,20 @@ class AdvancedPricingTest extends AbstractImportTestCase
151152
*/
152153
private $currencyResolver;
153154

155+
/**
156+
* @var ObjectManager
157+
*/
158+
protected $objectManager;
159+
154160
/**
155161
* @inheritDoc
156162
*/
157163
protected function setUp(): void
158164
{
159165
parent::setUp();
160166

167+
$this->objectManager = new ObjectManager($this);
168+
161169
$this->jsonHelper = $this->createMock(Data::class);
162170
$this->importExportData = $this->createMock(ImportExportHelperData::class);
163171
$this->resourceHelper = $this->createMock(Helper::class);
@@ -302,7 +310,7 @@ public function testValidateRowValidatorCall(): void
302310
'getWebSiteId'
303311
]
304312
);
305-
$this->setPropertyValue($advancedPricingMock, '_validatedRows', []);
313+
$this->objectManager->setBackwardCompatibleProperty($advancedPricingMock, '_validatedRows', []);
306314
$this->validator->expects($this->once())->method('isValid')->willReturn(false);
307315
$messages = ['value'];
308316
$this->validator->expects($this->once())->method('getMessages')->willReturn($messages);
@@ -624,14 +632,17 @@ function ($arg1, $arg2) use ($expectedSkuList) {
624632
*/
625633
public function testDeleteAdvancedPricingResetCachedSkuToDelete(): void
626634
{
627-
$this->setPropertyValue($this->advancedPricing, '_cachedSkuToDelete', 'some value');
635+
$this->objectManager->setBackwardCompatibleProperty($this->advancedPricing, '_cachedSkuToDelete', 'some value');
628636
$this->dataSourceModel
629637
->method('getNextBunch')
630638
->willReturnOnConsecutiveCalls([]);
631639

632640
$this->advancedPricing->deleteAdvancedPricing();
633641

634-
$cachedSkuToDelete = $this->getPropertyValue($this->advancedPricing, '_cachedSkuToDelete');
642+
$reflection = new \ReflectionClass($this->advancedPricing);
643+
$property = $reflection->getProperty('_cachedSkuToDelete');
644+
$property->setAccessible(true);
645+
$cachedSkuToDelete = $property->getValue($this->advancedPricing);
635646
$this->assertNull($cachedSkuToDelete);
636647
}
637648

@@ -1049,44 +1060,6 @@ public static function processCountExistingPricesDataProvider(): array
10491060
];
10501061
}
10511062

1052-
/**
1053-
* Get any object property value.
1054-
*
1055-
* @param $object
1056-
* @param $property
1057-
*
1058-
* @return mixed
1059-
* @throws \ReflectionException
1060-
*/
1061-
protected function getPropertyValue($object, $property)
1062-
{
1063-
$reflection = new ReflectionClass(get_class($object));
1064-
$reflectionProperty = $reflection->getProperty($property);
1065-
$reflectionProperty->setAccessible(true);
1066-
1067-
return $reflectionProperty->getValue($object);
1068-
}
1069-
1070-
/**
1071-
* Set object property value.
1072-
*
1073-
* @param $object
1074-
* @param $property
1075-
* @param $value
1076-
*
1077-
* @return mixed
1078-
* @throws \ReflectionException
1079-
*/
1080-
protected function setPropertyValue(&$object, $property, $value)
1081-
{
1082-
$reflection = new ReflectionClass(get_class($object));
1083-
$reflectionProperty = $reflection->getProperty($property);
1084-
$reflectionProperty->setAccessible(true);
1085-
$reflectionProperty->setValue($object, $value);
1086-
1087-
return $object;
1088-
}
1089-
10901063
/**
10911064
* Invoke any method of class AdvancedPricing.
10921065
*

lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,4 +393,27 @@ public function prepareObjectManager(array $map = [])
393393
$reflectionProperty->setAccessible(true);
394394
$reflectionProperty->setValue($objectManagerMock, $objectManagerMock);
395395
}
396+
397+
/**
398+
* Create a partial mock with reflection.
399+
*
400+
* @param string $className
401+
* @param array $methods
402+
* @return MockObject
403+
*/
404+
public function createPartialMockWithReflection(string $className, array $methods): MockObject
405+
{
406+
$reflection = new \ReflectionClass($this->_testObject);
407+
$getMockBuilderMethod = $reflection->getMethod('getMockBuilder');
408+
$getMockBuilderMethod->setAccessible(true);
409+
$mockBuilder = $getMockBuilderMethod->invoke($this->_testObject, $className);
410+
411+
$builderReflection = new \ReflectionClass($mockBuilder);
412+
$methodsProperty = $builderReflection->getProperty('methods');
413+
$methodsProperty->setAccessible(true);
414+
$methodsProperty->setValue($mockBuilder, $methods);
415+
416+
$mockBuilder->disableOriginalConstructor();
417+
return $mockBuilder->getMock();
418+
}
396419
}

0 commit comments

Comments
 (0)