-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the SimpleXmlElementTreeDumper to be able to dump simple xml tree…
…s including content
- Loading branch information
Showing
6 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/Component/VarDumper/Resources/functions/dumpsimplexmlelementtree.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
use Jspeedz\DebugPooper\Pooper\SimpleXmlElementTreeDumper; | ||
|
||
if(!function_exists('dumpSimpleXmlElementTree')) { | ||
/** | ||
* @param SimpleXMLElement $simpleXMLElement | ||
* @param bool $includeStringContent = true If true, will summarise textual content, as well as child elements and attribute names | ||
* @param bool $return = false | ||
* | ||
* @return void | ||
*/ | ||
function dumpSimpleXmlElementTree(SimpleXMLElement $simpleXMLElement, bool $includeStringContent = true, bool $return = false): void { | ||
SimpleXmlElementTreeDumper::dump($simpleXMLElement, $includeStringContent, $return); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
namespace Jspeedz\DebugPooper\Pooper; | ||
|
||
use SimpleXMLElement; | ||
|
||
require_once __DIR__ . '/../../vendor-nopackage/simplexml_debug-1.0.0/src/simplexml_tree.php'; | ||
|
||
// Load the global 💩() function (and in extension, dump()) | ||
require_once __DIR__ . '/../Component/VarDumper/Resources/functions/dump.php'; | ||
|
||
class SimpleXmlElementTreeDumper { | ||
/** | ||
* @param SimpleXMLElement $element | ||
* @param bool $includeStringContent = true If true, will summarise textual content, as well as child elements and attribute names | ||
* @param bool $return = false | ||
* | ||
* @return string|null | ||
*/ | ||
public static function dump(SimpleXMLElement $element, bool $includeStringContent = true, bool $return = false) { | ||
$result = simplexml_tree($element, $includeStringContent, true); | ||
|
||
if(!$return) { | ||
dump($result); | ||
|
||
return null; | ||
} | ||
else { | ||
return $result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
namespace Jspeedz\DebugPooper\Tests\Pooper; | ||
|
||
use Jspeedz\DebugPooper\Pooper\SimpleXmlElementTreeDumper; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class SimpleXmlElementTreeDumperTest extends TestCase { | ||
public function testDumpReturnOutputWithoutStringContent() { | ||
$element = simplexml_load_string('<?xml version="1.0" encoding="utf-8" ?> | ||
<root> | ||
<this> | ||
<is>Some</is> | ||
<test> | ||
<that>Will validate</that> | ||
</test> | ||
<z our="dumper">xx</z> | ||
</this> | ||
</root>'); | ||
|
||
$result = SimpleXmlElementTreeDumper::dump($element, false, true); | ||
$result = str_replace("\n", "", $result); | ||
$result = str_replace("\r", "", $result); | ||
|
||
$expected = "SimpleXML object (1 item)[0] // <root> ->this[0] ->is[0] ->test[0] ->that[0] ->z[0] ['our']"; | ||
$expected = str_replace("\n", "", $expected); | ||
$expected = str_replace("\r", "", $expected); | ||
|
||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
public function testDumpReturnOutputWithStringContent() { | ||
$element = simplexml_load_string('<?xml version="1.0" encoding="utf-8" ?> | ||
<root> | ||
<this> | ||
<is>Some</is> | ||
<test> | ||
<that>Will validate</that> | ||
</test> | ||
<z our="dumper">xx</z> | ||
</this> | ||
</root>'); | ||
|
||
$result = SimpleXmlElementTreeDumper::dump($element, true, true); | ||
|
||
$result = str_replace("\n", "", $result); | ||
$result = str_replace("\r", "", $result); | ||
|
||
$expected = "SimpleXML object (1 item)[0] // <root> (string) '' (6 chars) ->this[0] (string) '' (32 chars) ->is[0] (string) 'Some' (4 chars) ->test[0] (string) '' (22 chars) ->that[0] (string) 'Will validate' (13 chars) ->z[0] (string) 'xx' (2 chars) ['our'] (string) 'dumper' (6 chars)"; | ||
$expected = str_replace("\n", "", $expected); | ||
$expected = str_replace("\r", "", $expected); | ||
|
||
$this->assertEquals($expected, $result); | ||
} | ||
} |