Skip to content

Commit a483401

Browse files
ACPT-1552
Fixing unit test failures
1 parent c52a6d7 commit a483401

File tree

4 files changed

+57
-15
lines changed

4 files changed

+57
-15
lines changed

lib/internal/Magento/Framework/Module/ModuleList/Loader.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class Loader
5151
*/
5252
private $filesystemDriver;
5353

54+
/** @var ParserFactory */
55+
private readonly ParserFactory $parserFactory;
56+
5457
/**
5558
* Constructor
5659
*
@@ -65,13 +68,13 @@ public function __construct(
6568
Parser $parser,
6669
ComponentRegistrarInterface $moduleRegistry,
6770
DriverInterface $filesystemDriver,
68-
private ?ParserFactory $parserFactory = null,
71+
?ParserFactory $parserFactory = null,
6972
) {
7073
$this->converter = $converter;
7174
$this->parser = $parser;
7275
$this->moduleRegistry = $moduleRegistry;
7376
$this->filesystemDriver = $filesystemDriver;
74-
$this->parserFactory ??= ObjectManager::getInstance()->get(ParserFactory::class);
77+
$this->parserFactory = $parserFactory ?? ObjectManager::getInstance()->get(ParserFactory::class);
7578
}
7679

7780
/**

lib/internal/Magento/Framework/Module/Test/Unit/ModuleList/LoaderTest.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\Module\Declaration\Converter\Dom;
1313
use Magento\Framework\Module\ModuleList\Loader;
1414
use Magento\Framework\Xml\Parser;
15+
use Magento\Framework\Xml\ParserFactory;
1516
use PHPUnit\Framework\MockObject\MockObject;
1617
use PHPUnit\Framework\TestCase;
1718

@@ -34,6 +35,12 @@ class LoaderTest extends TestCase
3435
*/
3536
private $parser;
3637

38+
/** @var MockObject */
39+
private $parser2;
40+
41+
/** @var MockObject */
42+
private $parserFactory;
43+
3744
/**
3845
* @var MockObject
3946
*/
@@ -61,10 +68,20 @@ protected function setUp(): void
6168
{
6269
$this->converter = $this->createMock(Dom::class);
6370
$this->parser = $this->createMock(Parser::class);
64-
$this->parser->expects($this->once())->method('initErrorHandler');
71+
$this->parser->expects($this->never())->method('initErrorHandler');
72+
$this->parser2 = $this->createMock(Parser::class);
73+
$this->parser2->expects($this->any())->method('initErrorHandler');
74+
$this->parserFactory = $this->createMock(ParserFactory::class);
75+
$this->parserFactory->expects($this->any())->method('create')->willReturn($this->parser2);
6576
$this->registry = $this->getMockForAbstractClass(ComponentRegistrarInterface::class);
6677
$this->driver = $this->getMockForAbstractClass(DriverInterface::class);
67-
$this->loader = new Loader($this->converter, $this->parser, $this->registry, $this->driver);
78+
$this->loader = new Loader(
79+
$this->converter,
80+
$this->parser,
81+
$this->registry,
82+
$this->driver,
83+
$this->parserFactory,
84+
);
6885
}
6986

7087
/**
@@ -103,9 +120,9 @@ public function testLoad($paths): void
103120
$this->converter
104121
->method('convert')
105122
->willReturnOnConsecutiveCalls(...$willReturnArgs);
106-
$this->parser->expects($this->atLeastOnce())->method('loadXML')
123+
$this->parser2->expects($this->atLeastOnce())->method('loadXML')
107124
->with(self::$sampleXml);
108-
$this->parser->expects($this->atLeastOnce())->method('getDom');
125+
$this->parser2->expects($this->atLeastOnce())->method('getDom');
109126
$result = $this->loader->load();
110127
$this->assertSame(['a', 'e', 'c', 'd', 'b'], array_keys($result));
111128

@@ -172,8 +189,8 @@ public function testLoadExclude(): void
172189
['c' => $fixture['c']],
173190
['d' => $fixture['d']]
174191
);
175-
$this->parser->expects($this->atLeastOnce())->method('loadXML');
176-
$this->parser->expects($this->atLeastOnce())->method('getDom');
192+
$this->parser2->expects($this->atLeastOnce())->method('loadXML');
193+
$this->parser2->expects($this->atLeastOnce())->method('getDom');
177194
$result = $this->loader->load(['d']);
178195
$this->assertSame(['a', 'c', 'b'], array_keys($result));
179196
$this->assertSame($fixture['a'], $result['a']);

lib/internal/Magento/Framework/Webapi/Rest/Request/Deserializer/Xml.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class Xml implements \Magento\Framework\Webapi\Rest\Request\DeserializerInterfac
2727
*/
2828
protected $_appState;
2929

30+
/** @var ParserFactory */
31+
private readonly ParserFactory $parserFactory;
32+
3033
/**
3134
* @param Parser $xmlParser
3235
* @param State $appState
@@ -36,11 +39,11 @@ class Xml implements \Magento\Framework\Webapi\Rest\Request\DeserializerInterfac
3639
public function __construct(
3740
\Magento\Framework\Xml\Parser $xmlParser,
3841
State $appState,
39-
private ?ParserFactory $parserFactory = null,
42+
?ParserFactory $parserFactory = null,
4043
) {
4144
$this->_xmlParser = $xmlParser;
4245
$this->_appState = $appState;
43-
$this->parserFactory ??= ObjectManager::getInstance()->get(ParserFactory::class);
46+
$this->parserFactory = $parserFactory ?? ObjectManager::getInstance()->get(ParserFactory::class);
4447
}
4548

4649
/**

lib/internal/Magento/Framework/Webapi/Test/Unit/Rest/Request/Deserializer/XmlTest.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\Webapi\Exception;
1212
use Magento\Framework\Webapi\Rest\Request\Deserializer\Xml;
1313
use Magento\Framework\Xml\Parser;
14+
use Magento\Framework\Xml\ParserFactory;
1415
use PHPUnit\Framework\MockObject\MockObject;
1516
use PHPUnit\Framework\TestCase;
1617

@@ -19,6 +20,12 @@ class XmlTest extends TestCase
1920
/** @var MockObject */
2021
protected $_xmlParserMock;
2122

23+
/** @var MockObject */
24+
protected $xmlParserMock2;
25+
26+
/** @var MockObject */
27+
protected $xmlParserFactoryMock;
28+
2229
/** @var Xml */
2330
protected $_xmlDeserializer;
2431

@@ -32,11 +39,23 @@ protected function setUp(): void
3239
Parser::class,
3340
['xmlToArray', 'loadXML']
3441
);
42+
$this->_xmlParserMock->expects($this->never())->method('xmlToArray');
43+
$this->_xmlParserMock->expects($this->never())->method('loadXML');
44+
$this->xmlParserMock2 = $this->createPartialMock(
45+
Parser::class,
46+
['xmlToArray', 'loadXML'],
47+
);
48+
$this->xmlParserFactoryMock = $this->createPartialMock(
49+
ParserFactory::class,
50+
['create'],
51+
);
52+
$this->xmlParserFactoryMock->expects($this->any())->method('create')->willReturn($this->xmlParserMock2);
3553
$this->_appStateMock = $this->createMock(State::class);
3654
/** Initialize SUT. */
3755
$this->_xmlDeserializer = new Xml(
3856
$this->_xmlParserMock,
39-
$this->_appStateMock
57+
$this->_appStateMock,
58+
$this->xmlParserFactoryMock,
4059
);
4160
parent::setUp();
4261
}
@@ -59,10 +78,10 @@ public function testDeserializeInvalidArgumentException()
5978
public function testDeserialize()
6079
{
6180
/** Prepare mocks for SUT constructor. */
62-
$this->_xmlParserMock->expects($this->once())->method('loadXML');
81+
$this->xmlParserMock2->expects($this->once())->method('loadXML');
6382
$validInputXml = '<?xml version="1.0"?><xml><key1>test1</key1><key2>test2</key2></xml>';
6483
$returnArray = ['xml' => ['key1' => 'test1', 'key2' => 'test2']];
65-
$this->_xmlParserMock->expects($this->once())->method('xmlToArray')->willReturn($returnArray);
84+
$this->xmlParserMock2->expects($this->once())->method('xmlToArray')->willReturn($returnArray);
6685
$expectedArray = ['key1' => 'test1', 'key2' => 'test2'];
6786
/** Initialize SUT. */
6887
$this->assertEquals(
@@ -107,7 +126,7 @@ public function testDeserializeMagentoWebapiExceptionDeveloperModeOn()
107126
->willReturn('developer');
108127
$errorMessage = 'End tag for "key1" was omitted.';
109128
$this->_xmlDeserializer->handleErrors(null, $errorMessage, null, null);
110-
$this->_xmlParserMock->expects($this->once())->method('loadXML');
129+
$this->xmlParserMock2->expects($this->once())->method('loadXML');
111130
$invalidXml = '<?xml version="1.0"?><xml><key1>test1</xml>';
112131
/** Initialize SUT. */
113132
try {
@@ -133,7 +152,7 @@ public function testDeserializeMagentoWebapiExceptionDeveloperModeOff()
133152
->willReturn('production');
134153
$errorMessage = 'End tag for "key1" was omitted.';
135154
$this->_xmlDeserializer->handleErrors(null, $errorMessage, null, null);
136-
$this->_xmlParserMock->expects($this->once())->method('loadXML');
155+
$this->xmlParserMock2->expects($this->once())->method('loadXML');
137156
$invalidXml = '<?xml version="1.0"?><xml><key1>test1</xml>';
138157
/** Initialize SUT. */
139158
try {

0 commit comments

Comments
 (0)