*/
namespace oat\tao\test\unit\helpers;
use common_exception_Error;
use oat\generis\test\TestCase;
use tao_helpers_Xml;
class XmlHelperTest extends TestCase
{
public function dataProvider()
{
return [
[
'
text
',
['text']
],
[
'',
[
'p' => 'text'
]
],
[
'',
[
'p' => [
'i' => 'text'
]
]
],
[
"",
[
'a' => [
'first',
'forth',
],
'br' => [],
'p' => [
[],
"test\n\nwith\t",
]
]
]
];
}
public function exceptionDataProvider()
{
return [
// xml validation
[
'just a string',
[
'exception' => [
'class' => common_exception_Error::class,
'message' => 'Start tag expected, \'<\' not found [1]'
],
],
],
// without a root element
[
'text
',
[
'exception' => [
'class' => common_exception_Error::class,
'message' => 'Extra content at the end of the document [1]'
]
]
],
[
'',
[
'exception' => [
'class' => common_exception_Error::class,
'message' => "error parsing attribute name [1]\nattributes construct error [1]\nCouldn't find end of Start Tag aasdf line 1 [1]\nExtra content at the end of the document [1]"
]
]
]
];
}
/**
* @dataProvider dataProvider
* @param $xml
* @param $expected
* @throws common_exception_Error
*/
public function testToArray($xml, $expected)
{
$actual = tao_helpers_Xml::to_array($xml);
$this->assertSame($expected, $actual);
}
/**
* @dataProvider exceptionDataProvider
* @param $data
* @param $expected
*/
public function testToArrayExceptions($data, $expected)
{
try {
tao_helpers_Xml::to_array($data);
$this->assertFalse(true, 'Should not be here, exceptions only');
} catch (common_exception_Error $e) {
$this->assertSame($expected['exception']['class'], get_class($e));
$this->assertSame($expected['exception']['message'], $e->getMessage());
}
}
public function extractElementDataProvider()
{
return [
[
'some text link 1 and link 2
', // xml
'a', // tag to be extracted
'', // namespace
[
'link 1',
'link 2',
], // result
],
// with namespace
[
'some text link 1 and link 2
', // xml
'a', // tag to be extracted
'http://www.w3.org/1999/xhtml', // namespace
[
'link 2',
], // result
],
];
}
/**
* @dataProvider extractElementDataProvider
* @param string $xml
* @param string $tag
* @param string $namespace
* @param array $expected
* @throws common_exception_Error
*/
public function testExtractElements($xml, $tag, $namespace, $expected)
{
$elements = tao_helpers_Xml::extractElements($tag, $xml, $namespace);
$this->assertSame($expected, $elements);
}
}