extract logging to external function

This commit is contained in:
jacek 2015-05-19 16:01:53 +02:00
parent 6ab700c853
commit 4eaf7be472
2 changed files with 79 additions and 6 deletions

View file

@ -49,7 +49,6 @@ class XmlParser {
* @param $xmlString
* @return array
* @throws XmlMarkupParseErrorException
* @internal param String $xml
*/
public function getDataFromXmlString( $xmlString ) {
wfProfileIn( __METHOD__ );
@ -62,7 +61,7 @@ class XmlParser {
if ( $xml === false ) {
foreach ( $errors as $xmlerror ) {
$this->logXmlParseError( $xmlerror );
$this->logXmlParseError( $xmlerror->level, $xmlerror->code, trim( $xmlerror->message ) );
}
libxml_clear_errors();
throw new XmlMarkupParseErrorException();
@ -73,11 +72,11 @@ class XmlParser {
return $data;
}
protected function logXmlParseError( \libXMLError $xmlError ) {
public function logXmlParseError( $level, $code, $message ) {
\Wikia\Logger\WikiaLogger::instance()->info( "PortableInfobox XML Parser problem", [
"level" => $xmlError->level,
"code" => $xmlError->code,
"message" => $xmlError->message ] );
"level" => $level,
"code" => $code,
"message" => $message ] );
}
/**

View file

@ -58,4 +58,78 @@ class XmlParserTest extends WikiaBaseTest {
// ledo1 ommited, ledo2 at [1] position
$this->assertTrue( $data[1]['data']['value'][0]['data']['value'][2]['data']['value'] == 'parseRecursive(LALALA)');
}
/**
* @dataProvider errorHandlingDataProvider
*/
public function testErrorHandling( $markup, $expectedErrors ) {
$this->setExpectedException( '\Wikia\PortableInfobox\Parser\XmlMarkupParseErrorException' );
$parser = $this->getMockBuilder( 'Wikia\PortableInfobox\Parser\XmlParser' )
->setConstructorArgs( [ [] ] )
->setMethods( [ 'logXmlParseError' ] )
->getMock();
foreach ( $expectedErrors as $i => $ee ) {
$parser->expects( $this->at( $i ) )->method( 'logXmlParseError' )->with($ee['level'], $ee['code'], $ee['msg'] );
}
$data = $parser->getDataFromXmlString( $markup );
}
public function errorHandlingDataProvider() {
return [
[
'<data>d</dat/a>',
[
['level' => 3, 'code' => 73, 'msg' => "expected '>'"],
['level' => 3, 'code' => 76, 'msg' => "Opening and ending tag mismatch: data line 1 and dat"],
['level' => 3, 'code' => 5, 'msg' => "Extra content at the end of the document"],
]
],
[
'<data> x </data></data>',
[
['level' => 3, 'code' => 5, 'msg' => "Extra content at the end of the document"],
]
],
[
'<data> > ddd < a ></data>',
[
['level' => 3, 'code' => 68, 'msg' => "StartTag: invalid element name"],
]
],
[
'<data>',
[
['level' => 3, 'code' => 77, 'msg' => "Premature end of data in tag data line 1"],
]
],
[
'<infobox><data source=caption></infobox>',
[
['level' => 3, 'code' => 39, 'msg' => "AttValue: \" or ' expected"],
['level' => 3, 'code' => 65, 'msg' => "attributes construct error"],
['level' => 3, 'code' => 73, 'msg' => "Couldn't find end of Start Tag data line 1"],
]
],
[
'<infobox><data source="caption"></infobox>',
[
['level' => 3, 'code' => 76, 'msg' => "Opening and ending tag mismatch: data line 1 and infobox"],
]
],
[
'<infobox><data source="caption></data></infobox>',
[
['level' => 3, 'code' => 38, 'msg' => "Unescaped '<' not allowed in attributes values"],
['level' => 3, 'code' => 65, 'msg' => "attributes construct error"],
['level' => 3, 'code' => 73, 'msg' => "Couldn't find end of Start Tag data line 1"],
['level' => 3, 'code' => 76, 'msg' => "Opening and ending tag mismatch: infobox line 1 and data"],
['level' => 3, 'code' => 5, 'msg' => "Extra content at the end of the document"]
]
]
];
}
}