Skip to content

Commit f2afeda

Browse files
author
Ivan Gavryshko
committed
MAGETWO-31422: XML file syntax error does not report file name error is in
- fixed code style - added test for Parser
1 parent ff43247 commit f2afeda

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

dev/tests/unit/testsuite/Magento/Framework/Xml/ParserTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ class ParserTest extends \PHPUnit_Framework_TestCase
1111
/** @var \Magento\Framework\Xml\Parser */
1212
protected $parser;
1313

14+
/**
15+
* A sample invalid XML
16+
*
17+
* @var string
18+
*/
19+
private static $sampleXml = '<?xml version="1.0"?><config></onfig>';
20+
1421
protected function setUp()
1522
{
1623
$this->parser = new \Magento\Framework\Xml\Parser();
@@ -31,4 +38,14 @@ public function testGetXml()
3138
$this->parser->load(__DIR__ . '/_files/data.xml')->xmlToArray()
3239
);
3340
}
41+
42+
/**
43+
* @expectedException \Magento\Framework\Exception
44+
* @expectedExceptionMessage DOMDocument::loadXML(): Opening and ending tag mismatch
45+
*/
46+
public function testInvalidXml()
47+
{
48+
$this->parser->initErrorHandler();
49+
$this->parser->loadXML(self::$sampleXml);
50+
}
3451
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Loader
3232

3333
/**
3434
* Parser
35+
*
3536
* @var \Magento\Framework\Xml\Parser
3637
*/
3738
private $parser;
@@ -67,8 +68,9 @@ public function load()
6768
$this->parser->loadXML($contents);
6869
} catch (\Magento\Framework\Exception $e) {
6970
throw new \Magento\Framework\Exception(
70-
'Invalid Document: ' . $file . PHP_EOL
71-
. ' Error: ' . $e->getMessage()
71+
'Invalid Document: ' . $file . PHP_EOL . ' Error: ' . $e->getMessage(),
72+
$e->getCode(),
73+
$e
7274
);
7375
}
7476

lib/internal/Magento/Framework/Xml/Parser.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,12 @@ public function loadXML($string)
156156
set_error_handler([$this, 'errorHandler']);
157157
}
158158

159-
$this->getDom()->loadXML($string);
159+
try {
160+
$this->getDom()->loadXML($string);
161+
} catch(\Magento\Framework\Exception $e) {
162+
restore_error_handler();
163+
throw new \Magento\Framework\Exception($e->getMessage(), $e->getCode(), $e);
164+
}
160165

161166
if ($this->errorHandlerIsActive) {
162167
restore_error_handler();
@@ -168,10 +173,10 @@ public function loadXML($string)
168173
/**
169174
* Custom XML lib error handler
170175
*
171-
* @param $errorNo
172-
* @param $errorStr
173-
* @param $errorFile
174-
* @param $errorLine
176+
* @param int $errorNo
177+
* @param string $errorStr
178+
* @param string $errorFile
179+
* @param int $errorLine
175180
* @throws \Magento\Framework\Exception
176181
* @return void
177182
*/

0 commit comments

Comments
 (0)