@@ -104,9 +104,9 @@ function process_junit_xml( $xml_string )
104104 );
105105
106106 $ results ['testsuites ' ] = array ();
107- foreach ( $ project -> testsuite as $ testsuite ) {
108- // Handle nested testsuites like tests with data providers.
109- $ testsuite = isset ( $ testsuite -> testsuite ) ? $ testsuite-> testsuite : $ testsuite ;
107+
108+ $ testsuites = $ xml -> xpath ( ' //testsuites//testsuite[ ( count( testcase ) > 0 ) and ( @errors > 0 or @failures > 0 ) ] ' );
109+ foreach ( $ testsuites as $ testsuite ) {
110110 $ result = array (
111111 'name ' => (string ) $ testsuite ['name ' ],
112112 'tests ' => (string ) $ testsuite ['tests ' ],
@@ -116,19 +116,22 @@ function process_junit_xml( $xml_string )
116116 if ( empty ( $ result ['failures ' ] ) && empty ( $ result ['errors ' ] ) ) {
117117 continue ;
118118 }
119- $ results ['testsuites ' ][ (string ) $ testsuite ['name ' ] ] = $ result ;
120- $ results ['testsuites ' ][ (string ) $ testsuite ['name ' ] ]['testcases ' ] = array ();
119+ $ failures = array ();
121120 foreach ( $ testsuite ->testcase as $ testcase ) {
122121 // Capture both failure and error children.
123122 foreach ( array ( 'failure ' , 'error ' ) as $ key ) {
124123 if ( isset ( $ testcase ->{$ key } ) ) {
125- $ results [ ' testsuites ' ][ ( string ) $ testsuite [ ' name ' ] ][ ' testcases ' ] [ (string ) $ testcase ['name ' ] ] = array (
124+ $ failures [ (string ) $ testcase ['name ' ] ] = array (
126125 'name ' => (string ) $ testcase ['name ' ],
127126 $ key => (string ) $ testcase ->{$ key },
128127 );
129128 }
130129 }
131130 }
131+ if ( $ failures ) {
132+ $ results ['testsuites ' ][ (string ) $ testsuite ['name ' ] ] = $ result ;
133+ $ results ['testsuites ' ][ (string ) $ testsuite ['name ' ] ]['testcases ' ] = $ failures ;
134+ }
132135 }
133136
134137 return json_encode ( $ results );
0 commit comments