Skip to content

Commit f18ca37

Browse files
author
silinmykola
committed
fix php8.1 compatibility for unit tests
1 parent d44a639 commit f18ca37

File tree

18 files changed

+131
-89
lines changed

18 files changed

+131
-89
lines changed

app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@
99
*/
1010
namespace Magento\Cron\Observer;
1111

12+
use Exception;
13+
use Magento\Cron\Model\DeadlockRetrierInterface;
1214
use Magento\Cron\Model\ResourceModel\Schedule\Collection as ScheduleCollection;
1315
use Magento\Cron\Model\Schedule;
1416
use Magento\Framework\App\State;
1517
use Magento\Framework\Console\Cli;
18+
use Magento\Framework\Event\Observer;
1619
use Magento\Framework\Event\ObserverInterface;
20+
use Magento\Framework\Exception\LocalizedException;
1721
use Magento\Framework\Profiler\Driver\Standard\Stat;
1822
use Magento\Framework\Profiler\Driver\Standard\StatFactory;
19-
use Magento\Cron\Model\DeadlockRetrierInterface;
23+
use Throwable;
2024

2125
/**
2226
* The observer for processing cron jobs.
@@ -225,13 +229,15 @@ public function __construct(
225229
* Generate tasks schedule
226230
* Cleanup tasks schedule
227231
*
228-
* @param \Magento\Framework\Event\Observer $observer
232+
* @param Observer $observer
233+
*
229234
* @return void
235+
* @throws LocalizedException
230236
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
231237
* @SuppressWarnings(PHPMD.NPathComplexity)
232238
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
233239
*/
234-
public function execute(\Magento\Framework\Event\Observer $observer)
240+
public function execute(Observer $observer)
235241
{
236242
$currentTime = $this->dateTime->gmtTimestamp();
237243
$jobGroupsRoot = $this->_config->getJobs();
@@ -310,8 +316,9 @@ private function lockGroup(string $groupId, callable $callback): void
310316
* @param string[] $jobConfig
311317
* @param Schedule $schedule
312318
* @param string $groupId
319+
*
313320
* @return void
314-
* @throws \Exception
321+
* @throws Exception|Throwable
315322
*/
316323
protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule, $groupId)
317324
{
@@ -321,25 +328,29 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
321328
if ($scheduledTime < $currentTime - $scheduleLifetime) {
322329
$schedule->setStatus(Schedule::STATUS_MISSED);
323330
// phpcs:ignore Magento2.Exceptions.DirectThrow
324-
throw new \Exception(sprintf('Cron Job %s is missed at %s', $jobCode, $schedule->getScheduledAt()));
331+
throw new Exception(sprintf('Cron Job %s is missed at %s', $jobCode, $schedule->getScheduledAt()));
325332
}
326333

327334
if (!isset($jobConfig['instance'], $jobConfig['method'])) {
328335
$schedule->setStatus(Schedule::STATUS_ERROR);
329336
// phpcs:ignore Magento2.Exceptions.DirectThrow
330-
throw new \Exception(sprintf('No callbacks found for cron job %s', $jobCode));
337+
throw new Exception(sprintf('No callbacks found for cron job %s', $jobCode));
331338
}
332339
$model = $this->_objectManager->create($jobConfig['instance']);
333340
$callback = [$model, $jobConfig['method']];
334341
if (!is_callable($callback)) {
335342
$schedule->setStatus(Schedule::STATUS_ERROR);
336343
// phpcs:ignore Magento2.Exceptions.DirectThrow
337-
throw new \Exception(
338-
sprintf('Invalid callback: %s::%s can\'t be called', $jobConfig['instance'], $jobConfig['method'])
344+
throw new Exception(
345+
sprintf(
346+
'Invalid callback: %s::%s can\'t be called',
347+
$jobConfig['instance'],
348+
$jobConfig['method']
349+
)
339350
);
340351
}
341352

342-
$schedule->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', $this->dateTime->gmtTimestamp()));
353+
$schedule->setExecutedAt(date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp()));
343354
$this->retrier->execute(
344355
function () use ($schedule) {
345356
$schedule->save();
@@ -354,7 +365,7 @@ function () use ($schedule) {
354365
$this->logger->info(sprintf('Cron Job %s is run', $jobCode));
355366
//phpcs:ignore Magento2.Functions.DiscouragedFunction
356367
call_user_func_array($callback, [$schedule]);
357-
} catch (\Throwable $e) {
368+
} catch (Throwable $e) {
358369
$schedule->setStatus(Schedule::STATUS_ERROR);
359370
$this->logger->error(
360371
sprintf(
@@ -379,8 +390,8 @@ function () use ($schedule) {
379390
$schedule->setStatus(
380391
Schedule::STATUS_SUCCESS
381392
)->setFinishedAt(
382-
strftime(
383-
'%Y-%m-%d %H:%M:%S',
393+
date(
394+
'Y-m-d H:i:s',
384395
$this->dateTime->gmtTimestamp()
385396
)
386397
);
@@ -606,14 +617,16 @@ protected function getConfigSchedule($jobConfig)
606617
* @param string $cronExpression
607618
* @param int $timeInterval
608619
* @param array $exists
620+
*
609621
* @return void
622+
* @throws Exception
610623
*/
611624
protected function saveSchedule($jobCode, $cronExpression, $timeInterval, $exists)
612625
{
613626
$currentTime = $this->dateTime->gmtTimestamp();
614627
$timeAhead = $currentTime + $timeInterval;
615628
for ($time = $currentTime; $time < $timeAhead; $time += self::SECONDS_IN_MINUTE) {
616-
$scheduledAt = strftime('%Y-%m-%d %H:%M:00', $time);
629+
$scheduledAt = date('Y-m-d H:i:00', $time);
617630
$alreadyScheduled = !empty($exists[$jobCode . '/' . $scheduledAt]);
618631
$schedule = $this->createSchedule($jobCode, $cronExpression, $time);
619632
$valid = $schedule->trySchedule();
@@ -647,8 +660,8 @@ protected function createSchedule($jobCode, $cronExpression, $time)
647660
->setCronExpr($cronExpression)
648661
->setJobCode($jobCode)
649662
->setStatus(Schedule::STATUS_PENDING)
650-
->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', $this->dateTime->gmtTimestamp()))
651-
->setScheduledAt(strftime('%Y-%m-%d %H:%M', $time));
663+
->setCreatedAt(date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp()))
664+
->setScheduledAt(date('Y-m-d H:i', $time));
652665

653666
return $schedule;
654667
}

app/code/Magento/Cron/Test/Unit/Observer/ProcessCronQueueObserverTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Magento\Framework\DB\Adapter\AdapterInterface;
2727
use Magento\Framework\Event\ManagerInterface;
2828
use Magento\Framework\Event\Observer;
29+
use Magento\Framework\Exception\LocalizedException;
2930
use Magento\Framework\Lock\LockManagerInterface;
3031
use Magento\Framework\Model\AbstractModel;
3132
use Magento\Framework\Process\PhpExecutableFinderFactory;
@@ -656,6 +657,7 @@ public function dispatchExceptionInCallbackDataProvider(): array
656657
* Test case, successfully run job.
657658
*
658659
* @return void
660+
* @throws Exception
659661
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
660662
*/
661663
public function testDispatchRunJob(): void
@@ -761,6 +763,7 @@ function ($callback) {
761763
* Testing _generate(), iterate over saved cron jobs.
762764
*
763765
* @return void
766+
* @throws Exception
764767
*/
765768
public function testDispatchNotGenerate(): void
766769
{
@@ -814,6 +817,7 @@ public function testDispatchNotGenerate(): void
814817
* Testing _generate(), iterate over saved cron jobs and generate jobs.
815818
*
816819
* @return void
820+
* @throws Exception
817821
*/
818822
public function testDispatchGenerate(): void
819823
{
@@ -890,6 +894,7 @@ public function testDispatchGenerate(): void
890894
* Test case without saved cron jobs in data base.
891895
*
892896
* @return void
897+
* @throws Exception
893898
*/
894899
public function testDispatchCleanup(): void
895900
{
@@ -954,6 +959,7 @@ public function testDispatchCleanup(): void
954959

955960
/**
956961
* @return void
962+
* @throws LocalizedException
957963
*/
958964
public function testMissedJobsCleanedInTime(): void
959965
{

app/code/Magento/Customer/Controller/Adminhtml/Index/InlineEdit.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
use Magento\Customer\Model\EmailNotificationInterface;
1313
use Magento\Customer\Ui\Component\Listing\AttributeRepository;
1414
use Magento\Framework\App\Action\HttpPostActionInterface;
15+
use Magento\Framework\App\ObjectManager;
1516
use Magento\Framework\Exception\NoSuchEntityException;
1617
use Magento\Framework\Message\MessageInterface;
17-
use Magento\Framework\App\ObjectManager;
1818

1919
/**
2020
* Customer inline edit action
@@ -179,7 +179,7 @@ protected function getData(array $data, $isCustomerData = null)
179179
$addressKeys = preg_grep(
180180
'/^(' . AttributeRepository::BILLING_ADDRESS_PREFIX . '\w+)/',
181181
array_keys($data),
182-
$isCustomerData
182+
(int) $isCustomerData
183183
);
184184
$result = array_intersect_key($data, array_flip($addressKeys));
185185
if ($isCustomerData === null) {

app/code/Magento/Customer/Model/FileProcessor.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ public function saveTemporaryFile($fileId)
215215
);
216216

217217
$result = $uploader->save($path);
218-
unset($result['path']);
218+
if ($result && isset($result['path'])) {
219+
unset($result['path']);
220+
}
219221
if (!$result) {
220222
throw new LocalizedException(
221223
__('File can not be saved to the destination folder.')

app/code/Magento/Customer/Model/Metadata/Form/Multiselect.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
class Multiselect extends Select
1414
{
1515
/**
16-
* {@inheritdoc}
16+
* @inheritdoc
1717
*/
1818
public function extractValue(RequestInterface $request)
1919
{
@@ -25,7 +25,7 @@ public function extractValue(RequestInterface $request)
2525
}
2626

2727
/**
28-
* {@inheritdoc}
28+
* @inheritdoc
2929
*/
3030
public function compactValue($value)
3131
{
@@ -40,13 +40,13 @@ public function compactValue($value)
4040
}
4141

4242
/**
43-
* {@inheritdoc}
43+
* @inheritdoc
4444
*/
4545
public function outputValue($format = ElementFactory::OUTPUT_FORMAT_TEXT)
4646
{
4747
$values = $this->_value;
4848
if (!is_array($values)) {
49-
$values = explode(',', $values);
49+
$values = explode(',', (string) $values);
5050
}
5151

5252
if (ElementFactory::OUTPUT_FORMAT_ARRAY === $format || ElementFactory::OUTPUT_FORMAT_JSON === $format) {

app/code/Magento/Customer/Model/Metadata/Form/Text.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private function validateLength($value, AttributeMetadataInterface $attribute, a
126126
// validate length
127127
$label = __($attribute->getStoreLabel());
128128

129-
$length = $this->_string->strlen(trim($value));
129+
$length = $this->_string->strlen(trim((string) $value));
130130

131131
$validateRules = $attribute->getValidationRules();
132132

app/code/Magento/Customer/Test/Unit/Block/Adminhtml/Edit/Tab/View/PersonalInfoTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public function getStoreLastLoginDateDataProvider()
246246
{
247247
return [
248248
['2015-03-04 12:00:00', '2015-03-04 12:00:00'],
249-
['Never', null]
249+
['Never', '']
250250
];
251251
}
252252

app/code/Magento/Customer/Test/Unit/Model/AccountManagementTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2363,7 +2363,7 @@ public function testCreateAccountWithPasswordHashWithCustomerAddresses(): void
23632363
private function prepareDateTimeFactory(): string
23642364
{
23652365
$dateTime = '2017-10-25 18:57:08';
2366-
$timestamp = '1508983028';
2366+
$timestamp = 1508983028;
23672367
$dateTimeMock = $this->createMock(\DateTime::class);
23682368
$dateTimeMock->expects($this->any())
23692369
->method('format')

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function testExtractValueNoRequestScope($expected, $attributeCode = '', $
130130
'entityTypeCode' => self::ENTITY_TYPE,
131131
]
132132
);
133-
133+
$model->setRequestScope('');
134134
$this->assertEquals($expected, $model->extractValue($this->requestMock));
135135
if (!empty($attributeCode)) {
136136
unset($_FILES[$attributeCode]);

app/code/Magento/Customer/Test/Unit/Model/ResourceModel/GroupTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ public function testSaveWithReservedId()
131131
->willReturn([]);
132132
$this->groupModel->expects($this->once())->method('setId')
133133
->with($expectedId);
134+
$this->groupModel->expects($this->once())->method('getCode')
135+
->willReturn('');
134136

135137
$dbAdapter = $this->getMockBuilder(AdapterInterface::class)
136138
->disableOriginalConstructor()

0 commit comments

Comments
 (0)