PageRenderTime 25ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/phpspec/phpspec/spec/PhpSpec/Formatter/JUnitFormatterSpec.php

https://gitlab.com/judielsm/Handora
PHP | 226 lines | 192 code | 33 blank | 1 comment | 0 complexity | 02df2032db57e2d4516d514596171035 MD5 | raw file
  1. <?php
  2. namespace spec\PhpSpec\Formatter;
  3. use PhpSpec\ObjectBehavior;
  4. use Prophecy\Argument;
  5. use PhpSpec\Formatter\Presenter\PresenterInterface;
  6. use PhpSpec\IO\IOInterface;
  7. use PhpSpec\Listener\StatisticsCollector;
  8. use PhpSpec\Event\SpecificationEvent;
  9. use PhpSpec\Event\ExampleEvent;
  10. use PhpSpec\Loader\Node\SpecificationNode;
  11. use PhpSpec\Event\SuiteEvent;
  12. use PhpSpec\Exception\Example\SkippingException;
  13. class JUnitFormatterSpec extends ObjectBehavior
  14. {
  15. function let(
  16. PresenterInterface $presenter,
  17. IOInterface $io,
  18. StatisticsCollector $stats
  19. ) {
  20. $this->beConstructedWith($presenter, $io, $stats);
  21. }
  22. function it_is_an_event_subscriber()
  23. {
  24. $this->shouldHaveType('Symfony\Component\EventDispatcher\EventSubscriberInterface');
  25. }
  26. function it_stores_a_testcase_node_after_passed_example_run(
  27. ExampleEvent $event,
  28. SpecificationNode $specification,
  29. \ReflectionClass $refClass
  30. ) {
  31. $event->getResult()->willReturn(ExampleEvent::PASSED);
  32. $event->getTitle()->willReturn('example title');
  33. $event->getTime()->willReturn(1337);
  34. $event->getSpecification()->willReturn($specification);
  35. $specification->getClassReflection()->willReturn($refClass);
  36. $refClass->getName()->willReturn('Acme\Foo\Bar');
  37. $this->afterExample($event);
  38. $this->getTestCaseNodes()->shouldReturn(array(
  39. '<testcase name="example title" time="1337" classname="Acme\Foo\Bar" status="passed" />'
  40. ));
  41. }
  42. function it_stores_a_testcase_node_after_broken_example_run(
  43. ExampleEvent $event,
  44. SpecificationNode $specification,
  45. \ReflectionClass $refClass
  46. ) {
  47. $event->getResult()->willReturn(ExampleEvent::BROKEN);
  48. $event->getTitle()->willReturn('example title');
  49. $event->getTime()->willReturn(1337);
  50. $event->getException()->willReturn(new ExceptionStub('Something went wrong', 'Exception trace'));
  51. $event->getSpecification()->willReturn($specification);
  52. $specification->getClassReflection()->willReturn($refClass);
  53. $refClass->getName()->willReturn('Acme\Foo\Bar');
  54. $this->afterExample($event);
  55. $this->getTestCaseNodes()->shouldReturn(array(
  56. '<testcase name="example title" time="1337" classname="Acme\Foo\Bar" status="broken">'."\n".
  57. '<error type="spec\PhpSpec\Formatter\ExceptionStub" message="Something went wrong" />'."\n".
  58. '<system-err>'."\n".
  59. '<![CDATA['."\n".
  60. 'Exception trace'."\n".
  61. ']]>'."\n".
  62. '</system-err>'."\n".
  63. '</testcase>'
  64. ));
  65. }
  66. function it_stores_a_testcase_node_after_failed_example_run(
  67. ExampleEvent $event,
  68. SpecificationNode $specification,
  69. \ReflectionClass $refClass
  70. ) {
  71. $event->getResult()->willReturn(ExampleEvent::FAILED);
  72. $event->getTitle()->willReturn('example title');
  73. $event->getTime()->willReturn(1337);
  74. $event->getException()->willReturn(new ExceptionStub('Something went wrong', 'Exception trace'));
  75. $event->getSpecification()->willReturn($specification);
  76. $specification->getClassReflection()->willReturn($refClass);
  77. $refClass->getName()->willReturn('Acme\Foo\Bar');
  78. $this->afterExample($event);
  79. $this->getTestCaseNodes()->shouldReturn(array(
  80. '<testcase name="example title" time="1337" classname="Acme\Foo\Bar" status="failed">'."\n".
  81. '<failure type="spec\PhpSpec\Formatter\ExceptionStub" message="Something went wrong" />'."\n".
  82. '<system-err>'."\n".
  83. '<![CDATA['."\n".
  84. 'Exception trace'."\n".
  85. ']]>'."\n".
  86. '</system-err>'."\n".
  87. '</testcase>'
  88. ));
  89. }
  90. function it_stores_a_testcase_node_after_skipped_example_run(
  91. ExampleEvent $event,
  92. SpecificationNode $specification,
  93. \ReflectionClass $refClass
  94. ) {
  95. $event->getResult()->willReturn(ExampleEvent::SKIPPED);
  96. $event->getTitle()->willReturn('example title');
  97. $event->getTime()->willReturn(1337);
  98. $event->getException()->willReturn(new SkippingException('zog zog'));
  99. $event->getSpecification()->willReturn($specification);
  100. $specification->getClassReflection()->willReturn($refClass);
  101. $refClass->getName()->willReturn('Acme\Foo\Bar');
  102. $this->afterExample($event);
  103. // skipped tag is escaped because a skipped tag is also registered in the console formatter
  104. $this->getTestCaseNodes()->shouldReturn(array(
  105. '<testcase name="example title" time="1337" classname="Acme\Foo\Bar" status="skipped">'."\n".
  106. '\<skipped><![CDATA[ skipped: zog zog ]]>\</skipped>'."\n".
  107. '</testcase>'
  108. ));
  109. }
  110. function it_aggregates_testcase_nodes_and_store_them_after_specification_run(SpecificationEvent $event)
  111. {
  112. $event->getTitle()->willReturn('specification title');
  113. $event->getTime()->willReturn(42);
  114. $this->setTestCaseNodes(array(
  115. '<testcase name="example1" />',
  116. '<testcase name="example2" />',
  117. '<testcase name="example3" />',
  118. ));
  119. $this->setExampleStatusCounts(array(
  120. ExampleEvent::FAILED => 1,
  121. ExampleEvent::BROKEN => 2,
  122. ExampleEvent::PENDING => 5,
  123. ExampleEvent::SKIPPED => 3,
  124. ));
  125. $this->afterSpecification($event);
  126. $this->getTestSuiteNodes()->shouldReturn(array(
  127. '<testsuite name="specification title" time="42" tests="3" failures="1" errors="2" skipped="8">'."\n".
  128. '<testcase name="example1" />'."\n".
  129. '<testcase name="example2" />'."\n".
  130. '<testcase name="example3" />'."\n".
  131. '</testsuite>'
  132. ));
  133. $this->getTestCaseNodes()->shouldHaveCount(0);
  134. $this->getExampleStatusCounts()->shouldReturn(array(
  135. ExampleEvent::PASSED => 0,
  136. ExampleEvent::PENDING => 0,
  137. ExampleEvent::SKIPPED => 0,
  138. ExampleEvent::FAILED => 0,
  139. ExampleEvent::BROKEN => 0,
  140. ));
  141. }
  142. function it_aggregates_testsuite_nodes_and_display_them_after_suite_run(SuiteEvent $event, $io, $stats)
  143. {
  144. $event->getTime()->willReturn(48151.62342);
  145. $stats->getFailedEvents()->willReturn(range(1, 12));
  146. $stats->getBrokenEvents()->willReturn(range(1, 3));
  147. $stats->getEventsCount()->willReturn(100);
  148. $this->setTestSuiteNodes(array(
  149. '<testsuite name="specification1" tests="3">'."\n".
  150. '<testcase name="example1" />'."\n".
  151. '<testcase name="example2" />'."\n".
  152. '<testcase name="example3" />'."\n".
  153. '</testsuite>',
  154. '<testsuite name="specification2" tests="2">'."\n".
  155. '<testcase name="example1" />'."\n".
  156. '<testcase name="example2" />'."\n".
  157. '</testsuite>'
  158. ));
  159. $this->afterSuite($event);
  160. $io->write(
  161. '<?xml version="1.0" encoding="UTF-8" ?>'."\n".
  162. '<testsuites time="48151.62342" tests="100" failures="12" errors="3">'."\n".
  163. '<testsuite name="specification1" tests="3">'."\n".
  164. '<testcase name="example1" />'."\n".
  165. '<testcase name="example2" />'."\n".
  166. '<testcase name="example3" />'."\n".
  167. '</testsuite>'."\n".
  168. '<testsuite name="specification2" tests="2">'."\n".
  169. '<testcase name="example1" />'."\n".
  170. '<testcase name="example2" />'."\n".
  171. '</testsuite>'."\n".
  172. '</testsuites>'
  173. )->shouldBeCalled();
  174. }
  175. }
  176. class ExceptionStub
  177. {
  178. protected $trace;
  179. protected $message;
  180. public function __construct($message, $trace)
  181. {
  182. $this->message = $message;
  183. $this->trace = $trace;
  184. }
  185. public function getMessage()
  186. {
  187. return $this->message;
  188. }
  189. public function getTraceAsString()
  190. {
  191. return $this->trace;
  192. }
  193. }