2015-04-27 14:05:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class XmlParserTest extends WikiaBaseTest {
|
|
|
|
|
|
|
|
protected function setUp() {
|
2015-05-04 14:43:53 +00:00
|
|
|
$this->setupFile = dirname( __FILE__ ) . '/../PortableInfobox.setup.php';
|
2015-04-27 14:05:31 +00:00
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsEmpty() {
|
2015-05-07 12:37:13 +00:00
|
|
|
$parser = new \Wikia\PortableInfobox\Parser\XmlParser( [
|
2015-04-27 14:05:31 +00:00
|
|
|
'elem2' => 'ELEM2',
|
2015-05-07 09:46:41 +00:00
|
|
|
'lado2' => 'LALALA',
|
|
|
|
'nonempty' => '111'
|
2015-04-27 14:05:31 +00:00
|
|
|
]);
|
|
|
|
$markup = '
|
|
|
|
<infobox>
|
|
|
|
<comparison>
|
|
|
|
<set>
|
2015-05-04 14:16:35 +00:00
|
|
|
<header>Combatientes</header>
|
2015-05-04 10:48:57 +00:00
|
|
|
<data source="lado1" />
|
|
|
|
<data source="lado2" />
|
2015-04-27 14:05:31 +00:00
|
|
|
</set>
|
|
|
|
</comparison>
|
2015-05-07 09:46:41 +00:00
|
|
|
<data source="empty" />
|
|
|
|
<data source="nonempty"><label>nonemepty</label></data>
|
2015-04-27 14:05:31 +00:00
|
|
|
</infobox>
|
|
|
|
';
|
|
|
|
$data = $parser->getDataFromXmlString( $markup );
|
2015-05-07 09:46:41 +00:00
|
|
|
$this->assertTrue( $data[0]['data']['value'][0]['data']['value'][0]['data']['value'] == 'Combatientes' );
|
2015-05-07 09:48:43 +00:00
|
|
|
// '111' should be at [1] position, becasue <data source="empty"> should be ommited
|
2015-05-07 09:46:41 +00:00
|
|
|
$this->assertTrue( $data[1]['data']['value'] == '111' );
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testExternalParser() {
|
2015-05-07 12:37:13 +00:00
|
|
|
$parser = new \Wikia\PortableInfobox\Parser\XmlParser( [
|
2015-04-27 14:05:31 +00:00
|
|
|
'elem2' => 'ELEM2',
|
|
|
|
'lado2' => 'LALALA'
|
2015-05-07 12:37:13 +00:00
|
|
|
] );
|
2015-05-04 14:43:53 +00:00
|
|
|
$externalParser = new \Wikia\PortableInfobox\Parser\DummyParser();
|
2015-04-27 14:05:31 +00:00
|
|
|
$parser->setExternalParser( $externalParser );
|
|
|
|
$markup = '
|
|
|
|
<infobox>
|
|
|
|
<title><default>ABB</default></title>
|
|
|
|
<comparison>
|
|
|
|
<set>
|
2015-05-04 14:16:35 +00:00
|
|
|
<header>Combatientes</header>
|
2015-05-04 10:48:57 +00:00
|
|
|
<data source="lado1" />
|
|
|
|
<data source="lado2" />
|
2015-04-27 14:05:31 +00:00
|
|
|
</set>
|
|
|
|
</comparison>
|
2015-05-04 14:16:35 +00:00
|
|
|
<footer>[[aaa]]</footer>
|
2015-04-27 14:05:31 +00:00
|
|
|
</infobox>
|
|
|
|
';
|
|
|
|
$data = $parser->getDataFromXmlString( $markup );
|
2015-05-07 13:52:29 +00:00
|
|
|
|
2015-04-27 14:05:31 +00:00
|
|
|
$this->assertTrue( $data[0]['data']['value'] == 'parseRecursive(ABB)' );
|
2015-05-07 09:48:43 +00:00
|
|
|
// ledo1 ommited, ledo2 at [1] position
|
2015-05-07 13:52:29 +00:00
|
|
|
$this->assertTrue( $data[1]['data']['value'][0]['data']['value'][2]['data']['value'] == 'parseRecursive(LALALA)');
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
2015-05-19 14:01:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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"]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|