Skip to content

Commit 9d56b86

Browse files
committed
Merge remote-tracking branch 'karyna/php8.1-compatibility/fix-unit-tests-core' into platform-health
2 parents bd4ea02 + fffc16b commit 9d56b86

File tree

22 files changed

+126
-92
lines changed

22 files changed

+126
-92
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1850,7 +1850,7 @@ private function reInitModel(): void
18501850
->getMockForAbstractClass();
18511851

18521852
$dateTime = '2017-10-25 18:57:08';
1853-
$timestamp = '1508983028';
1853+
$timestamp = 1508983028;
18541854
$dateTimeMock = $this->getMockBuilder(\DateTime::class)
18551855
->disableOriginalConstructor()
18561856
->onlyMethods(['format', 'getTimestamp', 'setTimestamp'])
@@ -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')

lib/internal/Magento/Framework/App/Console/Request.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22
/**
3-
* Console request
4-
*
53
* Copyright © Magento, Inc. All rights reserved.
64
* See COPYING.txt for license details.
75
*/
@@ -19,7 +17,7 @@ class Request implements \Magento\Framework\App\RequestInterface
1917
*/
2018
public function __construct(array $parameters = [])
2119
{
22-
$data = getopt(null, $parameters);
20+
$data = getopt('', $parameters);
2321
// It can happen that request comes from http (e.g. pub/cron.php), but it runs the console
2422
if ($data) {
2523
$this->setParams($data);
@@ -35,17 +33,19 @@ public function __construct(array $parameters = [])
3533
*/
3634
public function getModuleName()
3735
{
36+
// phpcs:ignore Squiz.PHP.NonExecutableCode.ReturnNotRequired
3837
return;
3938
}
4039

4140
/**
4241
* Set Module name
4342
*
4443
* @param string $name
44+
*
4545
* @return void
4646
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4747
*/
48-
public function setModuleName($name)
48+
public function setModuleName($name) // phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
4949
{
5050
}
5151

@@ -56,6 +56,7 @@ public function setModuleName($name)
5656
*/
5757
public function getActionName()
5858
{
59+
// phpcs:ignore Squiz.PHP.NonExecutableCode.ReturnNotRequired
5960
return;
6061
}
6162

@@ -66,7 +67,7 @@ public function getActionName()
6667
* @return void
6768
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
6869
*/
69-
public function setActionName($name)
70+
public function setActionName($name) // phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
7071
{
7172
}
7273

@@ -112,10 +113,11 @@ public function setParams(array $data)
112113
*
113114
* @param null|string $name
114115
* @param null|string $default
116+
*
115117
* @return null|string|void
116118
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
117119
*/
118-
public function getCookie($name, $default)
120+
public function getCookie($name, $default) // phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
119121
{
120122
}
121123

lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/WriterTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ public function testSaveConfig()
175175
->method('getFiles')
176176
->willReturn('test.php');
177177
$this->dirRead->expects($this->any())
178-
->method('getAbsolutePath');
178+
->method('getAbsolutePath')
179+
->willReturn('');
179180
$this->filesystem->expects($this->any())
180181
->method('getDirectoryWrite')
181182
->with(DirectoryList::CONFIG)
@@ -231,7 +232,8 @@ public function testSaveConfigOverride()
231232
->method('getFiles')
232233
->willReturn('test.php');
233234
$this->dirRead->expects($this->any())
234-
->method('getAbsolutePath');
235+
->method('getAbsolutePath')
236+
->willReturn('');
235237
$this->filesystem->expects($this->any())
236238
->method('getDirectoryWrite')
237239
->with(DirectoryList::CONFIG)

lib/internal/Magento/Framework/App/Test/Unit/Request/HttpTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function testGetBasePathWithPath()
142142
public function testGetBasePathWithoutPath()
143143
{
144144
$this->model = $this->getModel();
145-
$this->model->setBasePath(null);
145+
$this->model->setBasePath('');
146146
$this->assertEquals('/', $this->model->getBasePath());
147147
}
148148

lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ protected function _splitMultiQuery($sql)
814814
$parts = preg_split(
815815
'#(;|\'|"|\\\\|//|--|\n|/\*|\*/)#',
816816
$sql,
817-
null,
817+
-1,
818818
PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
819819
);
820820

@@ -3625,6 +3625,7 @@ public function selectsByRange($rangeField, \Magento\Framework\DB\Select $select
36253625
private function getQueryGenerator()
36263626
{
36273627
if ($this->queryGenerator === null) {
3628+
// phpcs:ignore Magento2.PHP.AutogeneratedClassNotInConstructor
36283629
$this->queryGenerator = \Magento\Framework\App\ObjectManager::getInstance()->create(QueryGenerator::class);
36293630
}
36303631
return $this->queryGenerator;
@@ -4097,6 +4098,7 @@ public function getAutoIncrementField($tableName, $schemaName = null)
40974098
public function getSchemaListener()
40984099
{
40994100
if ($this->schemaListener === null) {
4101+
// phpcs:ignore Magento2.PHP.AutogeneratedClassNotInConstructor
41004102
$this->schemaListener = \Magento\Framework\App\ObjectManager::getInstance()->create(SchemaListener::class);
41014103
}
41024104
return $this->schemaListener;

lib/internal/Magento/Framework/DB/Test/Unit/DB/Statement/MysqlTest.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function testExecuteWhenThrowPDOException()
8787
$this->pdoStatementMock->expects($this->once())
8888
->method('execute')
8989
->willThrowException(new \PDOException('test message'));
90-
90+
$this->setQueryStringForPdoStmtMock($query);
9191
$this->assertEquals($errorReporting, error_reporting(), 'Error report level was\'t restored');
9292

9393
(new Mysql($this->adapterMock, $query))->_execute();
@@ -152,4 +152,23 @@ public function testExecuteWhenParamsAsParameterObject()
152152

153153
(new Mysql($this->adapterMock, $query))->_execute($params);
154154
}
155+
156+
/**
157+
* Initialize queryString property.
158+
*
159+
* @param string $query
160+
*
161+
* @return void
162+
*/
163+
private function setQueryStringForPdoStmtMock(string $query): void
164+
{
165+
/*
166+
* In PHP 8.1 $queryString is a Typed property, thus it should be initialized before the 1st call.
167+
* But it's not automatically initialized in case of Mocking, so we do it here.
168+
* Note: In PHP < 8.1 such assignment prohibited.
169+
*/
170+
if (PHP_VERSION_ID >= 80100) {
171+
$this->pdoStatementMock->queryString = $query;
172+
}
173+
}
155174
}

lib/internal/Magento/Framework/Event/Observer/Cron.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Cron extends \Magento\Framework\Event\Observer
2727
*/
2828
public function isValidFor(\Magento\Framework\Event $event)
2929
{
30-
$e = preg_split('#\s+#', $this->getCronExpr(), null, PREG_SPLIT_NO_EMPTY);
30+
$e = preg_split('#\s+#', $this->getCronExpr(), -1, PREG_SPLIT_NO_EMPTY);
3131
if (count($e) !== 5) {
3232
return false;
3333
}

lib/internal/Magento/Framework/Filter/SplitWords.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
*/
66
namespace Magento\Framework\Filter;
77

8-
/**
9-
* Split words
10-
*/
118
class SplitWords implements \Zend_Filter_Interface
129
{
1310
/**
@@ -46,7 +43,7 @@ public function __construct($uniqueOnly = true, $wordsQty = 0, $wordSeparatorReg
4643
public function filter($str)
4744
{
4845
$result = [];
49-
$split = preg_split('#' . $this->wordSeparatorRegexp . '#siu', $str, null, PREG_SPLIT_NO_EMPTY);
46+
$split = preg_split('#' . $this->wordSeparatorRegexp . '#siu', $str, -1, PREG_SPLIT_NO_EMPTY);
5047
foreach ($split as $word) {
5148
if ($this->uniqueOnly) {
5249
$result[$word] = $word;

lib/internal/Magento/Framework/Model/ResourceModel/Db/Collection/AbstractCollection.php

+13-18
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
namespace Magento\Framework\Model\ResourceModel\Db\Collection;
88

9-
use \Magento\Framework\App\ResourceConnection\SourceProviderInterface;
10-
use \Magento\Framework\Data\Collection\AbstractDb;
9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\App\ResourceConnection\SourceProviderInterface;
11+
use Magento\Framework\Data\Collection\AbstractDb;
12+
use Magento\Framework\Event\ManagerInterface;
1113

1214
/**
1315
* Abstract Resource Collection
@@ -106,23 +108,23 @@ abstract class AbstractCollection extends AbstractDb implements SourceProviderIn
106108
/**
107109
* Event manager proxy
108110
*
109-
* @var \Magento\Framework\Event\ManagerInterface
111+
* @var ManagerInterface
110112
*/
111113
protected $_eventManager = null;
112114

113115
/**
114116
* @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
115117
* @param \Psr\Log\LoggerInterface $logger
116118
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
117-
* @param \Magento\Framework\Event\ManagerInterface $eventManager
119+
* @param ManagerInterface $eventManager
118120
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
119121
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
120122
*/
121123
public function __construct(
122124
\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
123125
\Psr\Log\LoggerInterface $logger,
124126
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
125-
\Magento\Framework\Event\ManagerInterface $eventManager,
127+
ManagerInterface $eventManager,
126128
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
127129
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
128130
) {
@@ -241,12 +243,7 @@ protected function _initSelectFields()
241243
$column = $field;
242244
}
243245

244-
if ($alias !== null &&
245-
in_array($alias, $columnsToSelect) ||
246-
// If field already joined from another table
247-
$alias === null &&
248-
isset($alias, $columnsToSelect)
249-
) {
246+
if ($alias !== null && in_array($alias, $columnsToSelect)) {
250247
continue;
251248
}
252249

@@ -468,9 +465,8 @@ public function getResourceModelName()
468465
public function getResource()
469466
{
470467
if (empty($this->_resource)) {
471-
$this->_resource = \Magento\Framework\App\ObjectManager::getInstance()->create(
472-
$this->getResourceModelName()
473-
);
468+
// phpcs:ignore Magento2.PHP.AutogeneratedClassNotInConstructor
469+
$this->_resource = ObjectManager::getInstance()->create($this->getResourceModelName());
474470
}
475471
return $this->_resource;
476472
}
@@ -513,14 +509,13 @@ public function getAllIds()
513509
*/
514510
public function join($table, $cond, $cols = '*')
515511
{
512+
$alias = $table;
516513
if (is_array($table)) {
517514
foreach ($table as $k => $v) {
518515
$alias = $k;
519516
$table = $v;
520517
break;
521518
}
522-
} else {
523-
$alias = $table;
524519
}
525520

526521
if (!isset($this->_joinedTables[$alias])) {
@@ -624,7 +619,7 @@ public function __sleep()
624619
public function __wakeup()
625620
{
626621
parent::__wakeup();
627-
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
628-
$this->_eventManager = $objectManager->get(\Magento\Framework\Event\ManagerInterface::class);
622+
// phpcs:ignore Magento2.PHP.AutogeneratedClassNotInConstructor
623+
$this->_eventManager = ObjectManager::getInstance()->get(ManagerInterface::class);
629624
}
630625
}

lib/internal/Magento/Framework/ObjectManager/TMap.php

+7-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Magento\Framework\ObjectManagerInterface;
99

1010
/**
11-
* Class TMap
1211
* @internal
1312
*/
1413
class TMap implements \IteratorAggregate, \Countable, \ArrayAccess
@@ -62,7 +61,7 @@ public function __construct(
6261
array $array = [],
6362
\Closure $objectCreationStrategy = null
6463
) {
65-
if (!class_exists($this->type) && !interface_exists($type)) {
64+
if ((empty($this->type) || !class_exists($this->type)) && !interface_exists($type)) {
6665
throw new \InvalidArgumentException(sprintf('Unknown type %s', $type));
6766
}
6867

@@ -142,7 +141,7 @@ private function initObject($index)
142141
}
143142

144143
/**
145-
* {inheritdoc}
144+
* @inheritdoc
146145
*/
147146
public function getIterator()
148147
{
@@ -156,23 +155,23 @@ public function getIterator()
156155
}
157156

158157
/**
159-
* {inheritdoc}
158+
* @inheritdoc
160159
*/
161160
public function offsetExists($offset)
162161
{
163162
return array_key_exists($offset, $this->array);
164163
}
165164

166165
/**
167-
* {inheritdoc}
166+
* @inheritdoc
168167
*/
169168
public function offsetGet($offset)
170169
{
171170
return isset($this->array[$offset]) ? $this->initObject($offset) : null;
172171
}
173172

174173
/**
175-
* {inheritdoc}
174+
* @inheritdoc
176175
*/
177176
public function offsetSet($offset, $value)
178177
{
@@ -187,7 +186,7 @@ public function offsetSet($offset, $value)
187186
}
188187

189188
/**
190-
* {inheritdoc}
189+
* @inheritdoc
191190
*/
192191
public function offsetUnset($offset)
193192
{
@@ -202,7 +201,7 @@ public function offsetUnset($offset)
202201
}
203202

204203
/**
205-
* {inheritdoc}
204+
* @inheritdoc
206205
*/
207206
public function count()
208207
{

0 commit comments

Comments
 (0)