@@ -22,43 +22,57 @@ class LogCommand extends Command
2222 */
2323 private $ domElements = [];
2424
25+ private $ keysToCalculate = ['assertions ' , 'time ' , 'tests ' , 'errors ' , 'failures ' , 'skipped ' ];
26+
2527 protected function configure ()
2628 {
2729 $ this ->setName ('log ' )
28- ->setDescription ('Merges multiple PHPUnit JUnit xml files into one ' )
29- ->addArgument (
30- 'directory ' ,
31- InputArgument::REQUIRED ,
32- 'The directory containing PHPUnit JUnit xml files '
33- )
34- ->addArgument (
35- 'file ' ,
36- InputArgument::REQUIRED ,
37- 'The file where to write the merged result '
38- );
30+ ->setDescription ('Merges multiple PHPUnit JUnit xml files into one ' )
31+ ->addArgument (
32+ 'directory ' ,
33+ InputArgument::REQUIRED ,
34+ 'The directory containing PHPUnit JUnit xml files '
35+ )
36+ ->addArgument (
37+ 'file ' ,
38+ InputArgument::REQUIRED ,
39+ 'The file where to write the merged result '
40+ );
3941 }
4042
4143 protected function execute (InputInterface $ input , OutputInterface $ output )
4244 {
4345 $ finder = new Finder ();
4446 $ finder ->files ()
45- ->in (realpath ($ input ->getArgument ('directory ' )));
47+ ->in (realpath ($ input ->getArgument ('directory ' )))-> sortByName ( true );
4648
4749 $ this ->document = new \DOMDocument ('1.0 ' , 'UTF-8 ' );
4850 $ this ->document ->formatOutput = true ;
4951
5052 $ root = $ this ->document ->createElement ('testsuites ' );
53+ $ baseSuite = $ this ->document ->createElement ('testsuite ' );
54+ $ baseSuite ->setAttribute ('name ' , 'All Suites ' );
55+ $ baseSuite ->setAttribute ('tests ' , '0 ' );
56+ $ baseSuite ->setAttribute ('assertions ' , '0 ' );
57+ $ baseSuite ->setAttribute ('errors ' , '0 ' );
58+ $ baseSuite ->setAttribute ('failures ' , '0 ' );
59+ $ baseSuite ->setAttribute ('skipped ' , '0 ' );
60+ $ baseSuite ->setAttribute ('time ' , '0 ' );
61+
62+ $ this ->domElements ['All Suites ' ] = $ baseSuite ;
63+
64+ $ root ->appendChild ($ baseSuite );
5165 $ this ->document ->appendChild ($ root );
5266
5367 foreach ($ finder as $ file ) {
5468 try {
5569 $ xml = new \SimpleXMLElement (file_get_contents ($ file ->getRealPath ()));
5670 $ xmlArray = json_decode (json_encode ($ xml ), true );
5771 if (!empty ($ xmlArray )) {
58- $ this ->addTestSuites ($ root , $ xmlArray );
72+ $ this ->addTestSuites ($ baseSuite , $ xmlArray );
5973 }
6074 } catch (\Exception $ exception ) {
61- // Initial fallthrough
75+ $ output -> writeln ( sprintf ( ' <error>Error in file %s: %s</error> ' , $ file -> getRealPath (), $ exception -> getMessage ()));
6276 }
6377 }
6478
@@ -67,7 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6781 $ domElement ->removeAttribute ('parent ' );
6882 }
6983 }
70-
84+ $ this -> calculateTopLevelStats ();
7185 $ file = $ input ->getArgument ('file ' );
7286 if (!is_dir (dirname ($ file ))) {
7387 @mkdir (dirname ($ file ), 0777 , true );
@@ -82,7 +96,7 @@ private function addTestSuites(\DOMElement $parent, array $testSuites)
8296 foreach ($ testSuites as $ testSuite ) {
8397 if (empty ($ testSuite ['@attributes ' ]['name ' ])) {
8498 if (!empty ($ testSuite ['testsuite ' ])) {
85- $ this ->addTestSuites ($ parent , $ testSuite[ ' testsuite ' ] );
99+ $ this ->addTestSuites ($ parent , $ testSuite );
86100 }
87101 continue ;
88102 }
@@ -95,7 +109,6 @@ private function addTestSuites(\DOMElement $parent, array $testSuites)
95109 $ element ->setAttribute ('parent ' , $ parent ->getAttribute ('name ' ));
96110 $ attributes = $ testSuite ['@attributes ' ] ?? [];
97111 foreach ($ attributes as $ key => $ value ) {
98- $ value = $ key === 'name ' ? $ value : 0 ;
99112 $ element ->setAttribute ($ key , (string )$ value );
100113 }
101114 $ parent ->appendChild ($ element );
@@ -126,30 +139,51 @@ private function addTestCases(\DOMElement $parent, array $testCases)
126139 if (isset ($ this ->domElements [$ name ])) {
127140 continue ;
128141 }
129-
130142 $ element = $ this ->document ->createElement ('testcase ' );
131143 foreach ($ attributes as $ key => $ value ) {
132144 $ element ->setAttribute ($ key , (string )$ value );
133- if (!is_numeric ($ value )) {
134- continue ;
135- }
136- $ this ->addAttributeValueToTestSuite ($ parent , $ key , $ value );
145+ }
146+ if (isset ($ testCase ['failure ' ]) || isset ($ testCase ['warning ' ]) || isset ($ testCase ['error ' ])) {
147+ $ this ->addChildElements ($ testCase , $ element );
137148 }
138149 $ parent ->appendChild ($ element );
139150 $ this ->domElements [$ name ] = $ element ;
140151 }
141152 }
142153
143- private function addAttributeValueToTestSuite ( \ DOMElement $ element , $ key , $ value )
154+ private function addChildElements ( array $ tree , \ DOMElement $ element )
144155 {
145- $ currentValue = $ element ->hasAttribute ($ key ) ? $ element ->getAttribute ($ key ) : 0 ;
146- $ element ->setAttribute ($ key , (string )($ currentValue + $ value ));
156+ foreach ($ tree as $ key => $ value ) {
157+ if ($ key == '@attributes ' ) {
158+ continue ;
159+ }
160+ $ child = $ this ->document ->createElement ($ key );
161+ $ child ->nodeValue = $ value ;
162+ $ element ->appendChild ($ child );
163+ }
164+ }
147165
148- if ($ element ->hasAttribute ('parent ' )) {
149- $ parent = $ element ->getAttribute ('parent ' );
150- if (isset ($ this ->domElements [$ parent ])) {
151- $ this ->addAttributeValueToTestSuite ($ this ->domElements [$ parent ], $ key , $ value );
166+ private function calculateTopLevelStats ()
167+ {
168+ /** @var \DOMElement $topNode */
169+ $ suites = $ this ->document ->getElementsByTagName ('testsuites ' )->item (0 );
170+ $ topNode = $ suites ->firstChild ;
171+ if ($ topNode ->hasChildNodes ()) {
172+ $ stats = array_flip ($ this ->keysToCalculate );
173+ $ stats = array_map (function ($ _value ) {
174+ return 0 ;
175+ }, $ stats );
176+ foreach ($ topNode ->childNodes as $ child ) {
177+ $ attributes = $ child ->attributes ;
178+ foreach ($ attributes as $ key => $ value ) {
179+ if (in_array ($ key , $ this ->keysToCalculate )) {
180+ $ stats [$ key ] += $ value ->nodeValue ;
181+ }
182+ }
183+ }
184+ foreach ($ stats as $ key => $ value ) {
185+ $ topNode ->setAttribute ($ key , (string )$ value );
152186 }
153187 }
154188 }
155- }
189+ }
0 commit comments