LimeOutputXml.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. * This file is part of the Lime framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. class LimeOutputXml implements LimeOutputInterface
  12. {
  13. protected
  14. $ouptut = null;
  15. public function __construct()
  16. {
  17. $this->output = new LimeOutputArray();
  18. }
  19. public function supportsThreading()
  20. {
  21. return $this->output->supportsThreading();
  22. }
  23. public function focus($file)
  24. {
  25. return $this->output->focus($file);
  26. }
  27. public function close()
  28. {
  29. return $this->output->close();
  30. }
  31. public function plan($amount)
  32. {
  33. return $this->output->plan($amount);
  34. }
  35. public function pass($message, $file, $line)
  36. {
  37. return $this->output->plan($message, $file, $line);
  38. }
  39. public function fail($message, $file, $line, $error = null)
  40. {
  41. return $this->output->fail($message, $file, $line, $error);
  42. }
  43. public function skip($message, $file, $line)
  44. {
  45. return $this->output->skip($message, $file, $line);
  46. }
  47. public function todo($message, $file, $line)
  48. {
  49. return $this->output->todo($message, $file, $line);
  50. }
  51. public function warning($message, $file, $line)
  52. {
  53. return $this->output->warning($message, $file, $line);
  54. }
  55. public function error(LimeError $error)
  56. {
  57. return $this->output->error($error);
  58. }
  59. public function comment($message)
  60. {
  61. return $this->output->comment($message);
  62. }
  63. public function flush()
  64. {
  65. print $this->toXml();
  66. }
  67. public function toXml()
  68. {
  69. $results = $this->output->toArray();
  70. $dom = new DOMDocument('1.0', 'UTF-8');
  71. $dom->formatOutput = true;
  72. $dom->appendChild($testsuites = $dom->createElement('testsuites'));
  73. $errors = 0;
  74. $failures = 0;
  75. $errors = 0;
  76. $skipped = 0;
  77. $assertions = 0;
  78. foreach ($results as $result)
  79. {
  80. $testsuites->appendChild($testSuite = $dom->createElement('testsuite'));
  81. $testSuite->setAttribute('name', basename($result['file'], '.php'));
  82. $testSuite->setAttribute('file', $result['file']);
  83. $testSuite->setAttribute('failures', count($result['stats']['failed']));
  84. $testSuite->setAttribute('errors', 0);
  85. $testSuite->setAttribute('skipped', count($result['stats']['skipped']));
  86. $testSuite->setAttribute('tests', $result['stats']['plan']);
  87. $testSuite->setAttribute('assertions', $result['stats']['plan']);
  88. $failures += count($result['stats']['failed']);
  89. $skipped += count($result['stats']['skipped']);
  90. $assertions += $result['stats']['plan'];
  91. foreach ($result['tests'] as $test)
  92. {
  93. $testSuite->appendChild($testCase = $dom->createElement('testcase'));
  94. $testCase->setAttribute('name', $test['message']);
  95. $testCase->setAttribute('file', $test['file']);
  96. $testCase->setAttribute('line', $test['line']);
  97. $testCase->setAttribute('assertions', 1);
  98. if (!$test['status'])
  99. {
  100. $testCase->appendChild($failure = $dom->createElement('failure'));
  101. $failure->setAttribute('type', 'lime');
  102. if (array_key_exists('error', $test))
  103. {
  104. $failure->appendChild($dom->createTextNode($test['error']));
  105. }
  106. }
  107. }
  108. }
  109. $testsuites->setAttribute('failures', $failures);
  110. $testsuites->setAttribute('errors', $errors);
  111. $testsuites->setAttribute('tests', $assertions);
  112. $testsuites->setAttribute('assertions', $assertions);
  113. $testsuites->setAttribute('skipped', $skipped);
  114. return $dom->saveXml();
  115. }
  116. }