You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
One can imagine what the $jUnitSimpleXml->testsuite actually is: a SimpleXML object from parsing a jUnit XML-file. Seems easy enough.
Whilst cleaning this up I wanted to 'laravel way' it a bit more by going to:
However: due to the structure of jUnit XML files multiple <testsuite>-elements can be grouped into a single parent. Thus I have to write:
collect(
// due to the preserve_keys:false, we can't call collect($jUnitSimpleXml->testsuite) directlyiterator_to_array($jUnitSimpleXml->testsuite, preserve_keys: false)
)->map(
staticfn ($testsuite): ?JunitTestSuite => JunitTestSuite::fromSimpleXml($testsuite)
);
Which is basically what happens inside Illuminate\Support\Traits\EnumeratesValues::getArrayableItems():
if ($itemsinstanceof Traversable) {
returniterator_to_array($items);
}
But with the preserve_keys as true(default).
Is there any better way to achieve readable, concise and clear code to go from SimpleXml to a Collection with aforementioned JunitTestSuite-objects without manually performing the iterator_to_array call?
for refence a sample jUnit XML file:
<?xml version="1.0" encoding="UTF-8"?>
<testsuitesname="PHP_CodeSniffer 3.7.0"errors="0"tests="4465"failures="5">
<testsuitename="web/core/lib/Drupal/Core/Entity/Query/Null/QueryFactory.php"errors="0"tests="2"failures="2">
<testcasename="PHPCompatibility.Keywords.ForbiddenNamesAsDeclared.nullFound at web/core/lib/Drupal/Core/Entity/Query/Null/QueryFactory.php (3:1)">
<failuretype="error"message="'null' is a reserved keyword as of PHP version 7.0 and should not be used to name a class, interface or trait or as part of a namespace (T_NAMESPACE)"/>
</testcase>
</testsuite>
<testsuitename="web/core/lib/Drupal/Core/Entity/Query/Null/Query.php"errors="0"tests="1"failures="1">
<testcasename="PHPCompatibility.Keywords.ForbiddenNamesAsDeclared.nullFound at web/core/lib/Drupal/Core/Entity/Query/Null/Query.php (3:1)">
<failuretype="error"message="'null' is a reserved keyword as of PHP version 7.0 and should not be used to name a class, interface or trait or as part of a namespace (T_NAMESPACE)"/>
</testcase>
</testsuite>
<testsuitename="web/core/lib/Drupal/Core/Entity/Query/Null/Condition.php"errors="0"tests="1"failures="1">
<testcasename="PHPCompatibility.Keywords.ForbiddenNamesAsDeclared.nullFound at web/core/lib/Drupal/Core/Entity/Query/Null/Condition.php (3:1)">
<failuretype="error"message="'null' is a reserved keyword as of PHP version 7.0 and should not be used to name a class, interface or trait or as part of a namespace (T_NAMESPACE)"/>
</testcase>
</testsuite>
</testsuites>
Which should result in a Collection with 3 JunitTestSuite Items after parsing. But will result in 1 item due to preserve_keys.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently we're parsing an XML document into collections with various objects being parsed. This basically looks like:
One can imagine what the
$jUnitSimpleXml->testsuite
actually is: aSimpleXML
object from parsing a jUnit XML-file. Seems easy enough.Whilst cleaning this up I wanted to 'laravel way' it a bit more by going to:
However: due to the structure of jUnit XML files multiple
<testsuite>
-elements can be grouped into a single parent. Thus I have to write:Which is basically what happens inside
Illuminate\Support\Traits\EnumeratesValues::getArrayableItems()
:But with the
preserve_keys
astrue
(default).Is there any better way to achieve readable, concise and clear code to go from SimpleXml to a Collection with aforementioned JunitTestSuite-objects without manually performing the iterator_to_array call?
for refence a sample jUnit XML file:
Which should result in a Collection with 3 JunitTestSuite Items after parsing. But will result in 1 item due to
preserve_keys
.Beta Was this translation helpful? Give feedback.
All reactions