Skip to content

Commit

Permalink
fix unit tests to be compatible with php8
Browse files Browse the repository at this point in the history
  • Loading branch information
karyna-t committed Sep 10, 2021
1 parent c03b8a4 commit 5d32f99
Show file tree
Hide file tree
Showing 25 changed files with 77 additions and 76 deletions.
6 changes: 3 additions & 3 deletions app/code/Magento/Captcha/Test/Unit/Model/DefaultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class DefaultTest extends TestCase
'case_sensitive' => '0',
'shown_to_logged_in_user' => ['contact_us' => 1],
'always_for' => [
'user_create',
'user_forgotpassword',
'contact_us',
'user_create' => '1',
'user_forgotpassword' => '1',
'contact_us' => '1'
],
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function (array $itemA, array $itemB): int {
$posA = (int)$itemA['position'];
$posB = (int)$itemB['position'];

return $posA <=> $posB;
return (($posA - $posB) > 0) ? 1 : -1; // for PHP 7 and 8 consistency
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@

use Magento\CatalogInventory\Model\Config\Backend\Managestock;
use Magento\CatalogInventory\Model\Indexer\Stock\Processor;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class ManagestockTest extends TestCase
{
/**
* @var ScopeConfigInterface|MockObject
*/
private $configMock;

/** @var Processor|MockObject */
protected $stockIndexerProcessor;

Expand All @@ -27,10 +33,15 @@ protected function setUp(): void
Processor::class
)->disableOriginalConstructor()
->getMock();
$this->configMock = $this->getMockBuilder(ScopeConfigInterface::class)
->disableOriginalConstructor()
->getMock();

$this->model = (new ObjectManager($this))->getObject(
Managestock::class,
[
'stockIndexerProcessor' => $this->stockIndexerProcessor,
'config' => $this->configMock,
'stockIndexerProcessor' => $this->stockIndexerProcessor
]
);
}
Expand All @@ -57,6 +68,8 @@ public function testSaveAndRebuildIndex($newStockValue, $callCount)
{
$this->model->setValue($newStockValue);
$this->stockIndexerProcessor->expects($this->exactly($callCount))->method('markIndexerAsInvalid');
$this->configMock->method('getValue')->willReturn(0); // old value for stock status

$this->model->afterSave();
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public function validateDataDataProvider(): array
[
[
'simple_action' => 'by_percent',
'discount_amount' => '9,99'
'discount_amount' => '9.99'
],
true
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ private function generateUrls()
*/
private function isGlobalScope($storeId)
{
return null === $storeId || $storeId == Store::DEFAULT_STORE_ID;
return null === $storeId || ((int) $storeId == Store::DEFAULT_STORE_ID);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ public function testCompactValueNoDelete(): void
[
'value' => 'value',
'isAjax' => false,
'entityTypeCode' => self::ENTITY_TYPE
'entityTypeCode' => 'customer'
]
);

Expand Down Expand Up @@ -641,7 +641,7 @@ public function testCompactValueRemoveUiComponentValue(): void
[
'value' => $value,
'isAjax' => false,
'entityTypeCode' => self::ENTITY_TYPE
'entityTypeCode' => 'customer'
]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected function _getModelDependencies()
$resource = $this->createMock(ResourceConnection::class);

$data = [
'coreString' => $string,
'string' => $string,
'scopeConfig' => $scopeConfig,
'importFactory' => $importFactory,
'resourceHelper' => $resourceHelper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function testUpdateNotQtyDecimal($qty, $expectedQty)
->method('getProduct')
->willReturn($this->productMock);

$result = $this->object->update($this->itemMock, ['qty' => $qty]);
$result = $this->object->update($this->itemMock, ['qty' => (double) $qty]);
$this->assertEquals($result, $this->object);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ public function testPlaceOrderIfCustomerIsGuest(): void
->setConstructorArgs(
[
'eventManager' => $this->eventManager,
'quoteValidator' => $this->submitQuoteValidator,
'submitQuoteValidator' => $this->submitQuoteValidator,
'orderFactory' => $this->orderFactory,
'orderManagement' => $this->orderManagement,
'customerManagement' => $this->customerManagement,
Expand Down Expand Up @@ -930,7 +930,7 @@ public function testPlaceOrder(): void
->setConstructorArgs(
[
'eventManager' => $this->eventManager,
'quoteValidator' => $this->submitQuoteValidator,
'submitQuoteValidator' => $this->submitQuoteValidator,
'orderFactory' => $this->orderFactory,
'orderManagement' => $this->orderManagement,
'customerManagement' => $this->customerManagement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,6 @@ function ($argument) {
]
);
$discountData = $this->getMockBuilder(Data::class)
->setConstructorArgs(
[
'amount' => 0,
'baseAmount' => 0,
'originalAmount' => 0,
'baseOriginalAmount' => 0
]
)
->getMock();
$this->discountFactory->expects($this->any())
->method('create')
Expand Down
16 changes: 0 additions & 16 deletions app/code/Magento/SalesRule/Test/Unit/Model/RulesApplierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,6 @@ public function testApplyRulesWhenRuleWithStopRulesProcessingIsUsed(
$ruleId = 1;
$appliedRuleIds = [$ruleId => $ruleId];
$discountData = $this->getMockBuilder(Data::class)
->setConstructorArgs(
[
'amount' => 0,
'baseAmount' => 0,
'originalAmount' => 0,
'baseOriginalAmount' => 0
]
)
->getMock();
$this->discountFactory->expects($this->any())
->method('create')
Expand Down Expand Up @@ -294,14 +286,6 @@ protected function applyRule(MockObject $item, MockObject $rule): void
['fixQuantity', 'calculate']
);
$discountData = $this->getMockBuilder(Data::class)
->setConstructorArgs(
[
'amount' => 30,
'baseAmount' => 30,
'originalAmount' => 30,
'baseOriginalAmount' => 30
]
)
->getMock();
$this->validatorUtility->expects($this->any())
->method('getItemQty')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function execute()
\Magento\Framework\View\Design\ThemeInterface::class
)->load($themeId);
if (!$theme->getId()) {
throw new \InvalidArgumentException(sprintf('We cannot find a theme with id "%1".', $themeId));
throw new \InvalidArgumentException(__('We cannot find a theme with id "%1".', $themeId));
}
if (!$theme->isVirtual()) {
throw new \InvalidArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function execute()
$themeFactory = $this->_objectManager->create(\Magento\Framework\View\Design\Theme\FlyweightFactory::class);
$theme = $themeFactory->create($themeId);
if (!$theme) {
throw new \InvalidArgumentException(sprintf('We cannot find a theme with id "%1".', $themeId));
throw new \InvalidArgumentException(__('We cannot find a theme with id "%1".', $themeId));
}

$customCssFiles = $theme->getCustomization()->getFilesByType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ protected function _prepareMocksForValidateData(
}

return [
'source_class' => $this->sourceClass,
'result_class' => $this->resultClass,
'io_object' => $ioObject,
'code_generator' => null,
'sourceClassName' => $this->sourceClass,
'resultClassName' => $this->resultClass,
'ioObject' => $ioObject,
'classGenerator' => null,
'definedClasses' => $definedClassesMock,
];
}
Expand Down Expand Up @@ -298,17 +298,17 @@ protected function _prepareMocksForGenerateCode($willWriteCode)

// Add configuration for the generation step
/** @var \PHPUnit\Framework\MockObject\MockObject $ioObject */
$ioObject = $mocks['io_object'];
$ioObject = $mocks['ioObject'];
if ($willWriteCode) {
$ioObject->expects($this->once())->method('writeResultFile')->with(self::RESULT_FILE, self::RESULT_CODE);
}
$ioObject->expects($this->any())->method('generateResultFileName')->willReturn(self::RESULT_FILE);

return [
'source_class' => $mocks['source_class'],
'result_class' => $mocks['result_class'],
'io_object' => $ioObject,
'code_generator' => $codeGenerator,
'sourceClassName' => $mocks['sourceClassName'],
'resultClassName' => $mocks['resultClassName'],
'ioObject' => $ioObject,
'classGenerator' => $codeGenerator,
'definedClasses' => $mocks['definedClasses'],
];
}
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/Magento/Framework/DB/Statement/Pdo/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function _executeWithBinding(array $params)
// Separate array with values, as they are bound by reference
foreach ($params as $name => $param) {
$dataType = \PDO::PARAM_STR;
$length = null;
$length = 0;
$driverOptions = null;

if ($param instanceof Parameter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ public function testExecuteWhenParamsAsParameterObject()
{
$param1 = $this->createMock(Parameter::class);
$param1Value = 'SomeValue';
$param1DataType = 'dataType';
$param1Length = '9';
$param1DataType = \PDO::PARAM_STR;
$param1Length = 9;
$param1DriverOptions = 'some driver options';
$param1->expects($this->once())
->method('getIsBlob')
Expand Down Expand Up @@ -145,7 +145,7 @@ public function testExecuteWhenParamsAsParameterObject()
->method('bindParam')
->withConsecutive(
[':param1', $param1Value, $param1DataType, $param1Length, $param1DriverOptions],
[':param2', 'value2', \PDO::PARAM_STR, null, null]
[':param2', 'value2', \PDO::PARAM_STR, 0, null]
);
$this->pdoStatementMock->expects($this->once())
->method('execute');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public function testGetDataObjectFieldFromExtensibleEntity()
$targetMock
->expects($this->any())
->method('getExtensionAttributes')
->willReturn(null);
->willReturnSelf();

$this->eventManagerMock->expects($this->once())->method('dispatch');
$result = $this->copy->copyFieldsetToTarget('fieldset', 'aspect', $sourceMock, $targetMock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ public function testWriteMoreThanSessionMaxSize()

$this->saveHandlerAdapterMock->expects($this->once())
->method('read')
->with('test_session_id');
->with('test_session_id')
->willReturn('test_session_data');

$this->assertTrue($this->saveHandler->write("test_session_id", "testdata"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class CurrencyTest extends TestCase
* @var array
*/
protected $expectedCurrencies = [
'USD',
'EUR',
'UAH',
'GBP',
'USD' => 'US Dollar (USD)',
'EUR' => 'Euro (EUR)',
'UAH' => 'Ukrainian Hryvnia (UAH)',
'GBP' => 'British Pound (GBP)'
];

public function testIsValid()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class LocaleTest extends TestCase
* @var array
*/
protected $expectedLocales = [
'en_US',
'en_GB',
'uk_UA',
'de_DE',
'en_US' => 'English (United States)',
'en_GB' => 'English (United Kingdom)',
'uk_UA' => 'Ukrainian (Ukraine)',
'de_DE' => 'German (Germany)'
];

public function testIsValid()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class TimezoneTest extends TestCase
* @var array
*/
protected $expectedTimezones = [
'Australia/Darwin',
'America/Los_Angeles',
'Europe/Kiev',
'Asia/Jerusalem',
'Australia/Darwin' => 'Darwind description',
'America/Los_Angeles' => 'Los_Angeles description',
'Europe/Kiev' => 'Kiev description',
'Asia/Jerusalem' => 'Jerusalem description'
];

public function testIsValid()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function ($match) use (&$heredocs) {

return '__MINIFIED_HEREDOC__' .(count($heredocs) - 1);
},
$content
($content ?? '')
);
$content = preg_replace(
'#(?<!]]>)\s+</#',
Expand All @@ -149,7 +149,7 @@ function ($match) use (&$heredocs) {
preg_replace(
'#(?<!:)//[^\n\r]*(\<\?php)[^\n\r]*(\s\?\>)[^\n\r]*#',
'',
$content
($content ?? '')
)
)
)
Expand All @@ -164,7 +164,7 @@ function ($match) use (&$heredocs) {
function ($match) use ($heredocs) {
return $heredocs[(int)$match[1]];
},
$content
($content ?? '')
);

if (!$this->htmlDirectory->isExist()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ public function testProcess(
'actions' => [
[
$methodName,
[
'test_argument' => $argumentData
],
[$argumentData],
'config_path',
'scope',
],
Expand Down
Loading

0 comments on commit 5d32f99

Please sign in to comment.