JunitReporterTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.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. namespace PhpCsFixer\Tests\Report;
  12. use PhpCsFixer\Report\JunitReporter;
  13. use PhpCsFixer\Report\ReportSummary;
  14. /**
  15. * @author Boris Gorbylev <ekho@ekho.name>
  16. *
  17. * @internal
  18. */
  19. final class JunitReporterTest extends \PHPUnit_Framework_TestCase
  20. {
  21. /** @var JunitReporter */
  22. private $reporter;
  23. protected function setUp()
  24. {
  25. $this->reporter = new JunitReporter();
  26. }
  27. /**
  28. * @covers \PhpCsFixer\Report\JunitReporter::getFormat
  29. */
  30. public function testGetFormat()
  31. {
  32. $this->assertSame('junit', $this->reporter->getFormat());
  33. }
  34. public function testGenerateNoErrors()
  35. {
  36. $expectedXml = <<<'XML'
  37. <?xml version="1.0" encoding="UTF-8"?>
  38. <testsuites>
  39. <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="0" errors="0">
  40. <testcase name="All OK" assertions="1"/>
  41. </testsuite>
  42. </testsuites>
  43. XML;
  44. $actualXml = $this->reporter->generate(
  45. new ReportSummary(
  46. array(),
  47. 0,
  48. 0,
  49. false,
  50. false,
  51. false
  52. )
  53. );
  54. $this->assertJunitXmlSchema($actualXml);
  55. $this->assertXmlStringEqualsXmlString($expectedXml, $actualXml);
  56. }
  57. public function testGenerateSimple()
  58. {
  59. $expectedXml = <<<'XML'
  60. <?xml version="1.0" encoding="UTF-8"?>
  61. <testsuites>
  62. <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="1" errors="0">
  63. <testcase name="someFile" file="someFile.php" assertions="1">
  64. <failure type="code_style">Wrong code style</failure>
  65. </testcase>
  66. </testsuite>
  67. </testsuites>
  68. XML;
  69. $actualXml = $this->reporter->generate(
  70. new ReportSummary(
  71. array(
  72. 'someFile.php' => array(
  73. 'appliedFixers' => array('some_fixer_name_here'),
  74. ),
  75. ),
  76. 0,
  77. 0,
  78. false,
  79. false,
  80. false
  81. )
  82. );
  83. $this->assertJunitXmlSchema($actualXml);
  84. $this->assertXmlStringEqualsXmlString($expectedXml, $actualXml);
  85. }
  86. public function testGenerateWithDiff()
  87. {
  88. $expectedXml = <<<'XML'
  89. <?xml version="1.0" encoding="UTF-8"?>
  90. <testsuites>
  91. <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="1" errors="0">
  92. <testcase name="someFile" file="someFile.php" assertions="1">
  93. <failure type="code_style"><![CDATA[Wrong code style
  94. Diff:
  95. ---------------
  96. this text is a diff ;)]]></failure>
  97. </testcase>
  98. </testsuite>
  99. </testsuites>
  100. XML;
  101. $actualXml = $this->reporter->generate(
  102. new ReportSummary(
  103. array(
  104. 'someFile.php' => array(
  105. 'appliedFixers' => array('some_fixer_name_here'),
  106. 'diff' => 'this text is a diff ;)',
  107. ),
  108. ),
  109. 0,
  110. 0,
  111. false,
  112. false,
  113. false
  114. )
  115. );
  116. $this->assertJunitXmlSchema($actualXml);
  117. $this->assertXmlStringEqualsXmlString($expectedXml, $actualXml);
  118. }
  119. public function testGenerateWithAppliedFixers()
  120. {
  121. $expectedXml = <<<'XML'
  122. <?xml version="1.0" encoding="UTF-8"?>
  123. <testsuites>
  124. <testsuite name="PHP CS Fixer" tests="1" assertions="2" failures="2" errors="0">
  125. <testcase name="someFile" file="someFile.php" assertions="2">
  126. <failure type="code_style">applied fixers:
  127. ---------------
  128. * some_fixer_name_here_1
  129. * some_fixer_name_here_2</failure>
  130. </testcase>
  131. </testsuite>
  132. </testsuites>
  133. XML;
  134. $actualXml = $this->reporter->generate(
  135. new ReportSummary(
  136. array(
  137. 'someFile.php' => array(
  138. 'appliedFixers' => array('some_fixer_name_here_1', 'some_fixer_name_here_2'),
  139. ),
  140. ),
  141. 0,
  142. 0,
  143. true,
  144. false,
  145. false
  146. )
  147. );
  148. $this->assertJunitXmlSchema($actualXml);
  149. $this->assertXmlStringEqualsXmlString($expectedXml, $actualXml);
  150. }
  151. public function testGenerateWithTimeAndMemory()
  152. {
  153. $expectedXml = <<<'XML'
  154. <?xml version="1.0" encoding="UTF-8"?>
  155. <testsuites>
  156. <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="1" errors="0" time="1.234">
  157. <testcase name="someFile" file="someFile.php" assertions="1">
  158. <failure type="code_style">Wrong code style</failure>
  159. </testcase>
  160. </testsuite>
  161. </testsuites>
  162. XML;
  163. $actualXml = $this->reporter->generate(
  164. new ReportSummary(
  165. array(
  166. 'someFile.php' => array(
  167. 'appliedFixers' => array('some_fixer_name_here'),
  168. ),
  169. ),
  170. 1234,
  171. 0,
  172. false,
  173. false,
  174. false
  175. )
  176. );
  177. $this->assertJunitXmlSchema($actualXml);
  178. $this->assertXmlStringEqualsXmlString($expectedXml, $actualXml);
  179. }
  180. public function testGenerateComplex()
  181. {
  182. $expectedXml = <<<'XML'
  183. <?xml version="1.0"?>
  184. <testsuites>
  185. <testsuite assertions="3" errors="0" failures="3" name="PHP CS Fixer" tests="2" time="1.234">
  186. <testcase assertions="2" file="someFile.php" name="someFile">
  187. <failure type="code_style">applied fixers:
  188. ---------------
  189. * some_fixer_name_here_1
  190. * some_fixer_name_here_2
  191. Diff:
  192. ---------------
  193. this text is a diff ;)</failure>
  194. </testcase>
  195. <testcase assertions="1" file="anotherFile.php" name="anotherFile">
  196. <failure type="code_style">applied fixers:
  197. ---------------
  198. * another_fixer_name_here
  199. Diff:
  200. ---------------
  201. another diff here ;)</failure>
  202. </testcase>
  203. </testsuite>
  204. </testsuites>
  205. XML;
  206. $actualXml = $this->reporter->generate(
  207. new ReportSummary(
  208. array(
  209. 'someFile.php' => array(
  210. 'appliedFixers' => array('some_fixer_name_here_1', 'some_fixer_name_here_2'),
  211. 'diff' => 'this text is a diff ;)',
  212. ),
  213. 'anotherFile.php' => array(
  214. 'appliedFixers' => array('another_fixer_name_here'),
  215. 'diff' => 'another diff here ;)',
  216. ),
  217. ),
  218. 1234,
  219. 0,
  220. true,
  221. true,
  222. false
  223. )
  224. );
  225. $this->assertJunitXmlSchema($actualXml);
  226. $this->assertXmlStringEqualsXmlString($expectedXml, $actualXml);
  227. }
  228. /**
  229. * Validates generated xml report with schema.
  230. * Uses JUnit XML schema from Jenkins.
  231. *
  232. * @see https://github.com/jenkinsci/xunit-plugin/blob/master/src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd
  233. *
  234. * @param string $xml
  235. */
  236. private function assertJunitXmlSchema($xml)
  237. {
  238. $xsdPath = __DIR__.'/../../doc/junit-10.xsd';
  239. static $errorLevels = array(
  240. LIBXML_ERR_WARNING => 'Warning',
  241. LIBXML_ERR_ERROR => 'Error',
  242. LIBXML_ERR_FATAL => 'Fatal Error',
  243. );
  244. $internal = libxml_use_internal_errors(true);
  245. $dom = new \DOMDocument();
  246. $loaded = $dom->loadXML($xml);
  247. if (true !== $loaded) {
  248. libxml_use_internal_errors($internal);
  249. $this->fail(sprintf('XML loading failed, expected "true", got "%s".', var_export($loaded, true)));
  250. }
  251. $dom->schemaValidate($xsdPath);
  252. $errors = array();
  253. foreach (libxml_get_errors() as $error) {
  254. $errors[] = sprintf(
  255. '%s #%s: %s (%s:%s)',
  256. isset($errorLevels[$error->level]) ? $errorLevels[$error->level] : null,
  257. $error->code,
  258. trim($error->message),
  259. $error->file,
  260. $error->line
  261. );
  262. }
  263. $errors = implode(PHP_EOL, $errors);
  264. libxml_clear_errors();
  265. libxml_use_internal_errors($internal);
  266. if (strlen($errors) > 0) {
  267. $this->fail('Actual xml does not match schema: '.PHP_EOL.$errors);
  268. }
  269. }
  270. }