Skip to content

Commit bf2124f

Browse files
committed
refactor Utils class,
update/add unit tests
1 parent 898ee9e commit bf2124f

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

Tests/UtilsTest.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ public function testGetFormatedXmlAsDomDocument()
2121
$this->assertInstanceOf('\DOMDocument', Utils::getFormatedXml(file_get_contents(__DIR__ . '/resources/oneline.xml'), true));
2222
}
2323
/**
24-
*
24+
* @expectedException \InvalidArgumentException
2525
*/
2626
public function testGetFormatedXmlEmptyStringAsString()
2727
{
28-
$this->assertSame('', Utils::getFormatedXml(''));
28+
Utils::getFormatedXml('');
2929
}
3030
/**
31-
*
31+
* @expectedException \InvalidArgumentException
3232
*/
3333
public function testGetFormatedXmlEmptyStringAsDomDocument()
3434
{
35-
$this->assertSame(null, Utils::getFormatedXml('', true));
35+
Utils::getFormatedXml('', true);
3636
}
3737
/**
3838
* @expectedException \InvalidArgumentException
@@ -41,4 +41,18 @@ public function testGetFormatedXmlInvalidXmlAsDomDocument()
4141
{
4242
Utils::getFormatedXml('<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:img="http://ws.estesexpress.com/imageview" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://ws.estesexpress.com/imageview" xml:lang="en"><root>', true);
4343
}
44+
/**
45+
*
46+
*/
47+
public function testGetDOMDocument()
48+
{
49+
$this->assertInstanceOf('\\DOMDocument', Utils::getDOMDocument(file_get_contents(__DIR__ . '/resources/oneline.xml')));
50+
}
51+
/**
52+
* @expectedException \InvalidArgumentException
53+
*/
54+
public function testGetDOMDocumentException()
55+
{
56+
$this->assertInstanceOf('\\DOMDocument', Utils::getDOMDocument(''));
57+
}
4458
}

Utils.php

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,34 @@ class Utils
66
{
77
/**
88
* Returns a XML string content as a DOMDocument or as a formated XML string
9-
* @uses \DOMDocument::loadXML()
10-
* @uses \DOMDocument::saveXML()
9+
* @throws \InvalidArgumentException
1110
* @param string $string
12-
* @param bool $asDomDocument
13-
* @return \DOMDocument|string|null
11+
* @param string $asDomDocument
12+
* @return \DOMDocument|null
1413
*/
1514
public static function getFormatedXml($string, $asDomDocument = false)
1615
{
17-
$dom = null;
18-
$formated = $string;
19-
if (!empty($string) && class_exists('\DOMDocument')) {
20-
$dom = new \DOMDocument('1.0', 'UTF-8');
21-
$dom->formatOutput = true;
22-
$dom->preserveWhiteSpace = false;
23-
$dom->resolveExternals = false;
24-
$dom->substituteEntities = false;
25-
$dom->validateOnParse = false;
26-
try {
27-
if ($dom->loadXML($string)) {
28-
$formated = $dom->saveXML();
29-
}
30-
} catch (\Exception $exception) {
31-
throw new \InvalidArgumentException('XML string is invalid', $exception->getCode(), $exception);
32-
}
16+
$domDocument = self::getDOMDocument($string);
17+
return $asDomDocument ? $domDocument : $domDocument->saveXML();
18+
}
19+
/**
20+
* @param string $string
21+
* @throws \InvalidArgumentException
22+
* @return \DOMDocument
23+
*/
24+
public static function getDOMDocument($string)
25+
{
26+
$dom = new \DOMDocument('1.0', 'UTF-8');
27+
$dom->formatOutput = true;
28+
$dom->preserveWhiteSpace = false;
29+
$dom->resolveExternals = false;
30+
$dom->substituteEntities = false;
31+
$dom->validateOnParse = false;
32+
try {
33+
$dom->loadXML($string);
34+
} catch (\Exception $exception) {
35+
throw new \InvalidArgumentException('XML string is invalid', $exception->getCode(), $exception);
3336
}
34-
return $asDomDocument ? $dom : $formated;
37+
return $dom;
3538
}
3639
}

0 commit comments

Comments
 (0)