From 4eaf7be47236596ff4763f1b34e92bc48b13ca3d Mon Sep 17 00:00:00 2001 From: jacek Date: Tue, 19 May 2015 16:01:53 +0200 Subject: [PATCH] extract logging to external function --- services/Parser/XmlParser.php | 11 +++--- tests/XmlParserTest.php | 74 +++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/services/Parser/XmlParser.php b/services/Parser/XmlParser.php index 5e54c2b..ad1e57d 100644 --- a/services/Parser/XmlParser.php +++ b/services/Parser/XmlParser.php @@ -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 ] ); } /** diff --git a/tests/XmlParserTest.php b/tests/XmlParserTest.php index 55780b5..3e8c693 100644 --- a/tests/XmlParserTest.php +++ b/tests/XmlParserTest.php @@ -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 [ + [ + 'd', + [ + ['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"], + ] + ], + [ + ' x ', + [ + ['level' => 3, 'code' => 5, 'msg' => "Extra content at the end of the document"], + ] + ], + [ + ' > ddd < a >', + [ + ['level' => 3, 'code' => 68, 'msg' => "StartTag: invalid element name"], + ] + ], + [ + '', + [ + ['level' => 3, 'code' => 77, 'msg' => "Premature end of data in tag data line 1"], + ] + ], + [ + '', + [ + ['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"], + ] + ], + [ + '', + [ + ['level' => 3, 'code' => 76, 'msg' => "Opening and ending tag mismatch: data line 1 and infobox"], + ] + ], + [ + ' 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"] + ] + ] + ]; + } }