4 * This file is part of the Behat.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Behat\Output\Node\Printer\JUnit;
13 use Behat\Behat\Output\Node\EventListener\JUnit\JUnitOutlineStoreListener;
14 use Behat\Behat\Output\Node\Printer\Helper\ResultToStringConverter;
15 use Behat\Gherkin\Node\ExampleNode;
16 use Behat\Gherkin\Node\FeatureNode;
17 use Behat\Gherkin\Node\OutlineNode;
18 use Behat\Gherkin\Node\ScenarioLikeInterface;
19 use Behat\Testwork\Output\Formatter;
20 use Behat\Testwork\Output\Printer\JUnitOutputPrinter;
21 use Behat\Testwork\Tester\Result\TestResult;
24 * Prints the <testcase> element.
26 * @author Wouter J <wouter@wouterj.nl>
28 final class JUnitScenarioPrinter
31 * @var ResultToStringConverter
33 private $resultConverter;
36 * @var JUnitOutlineStoreListener
38 private $outlineStoreListener;
48 private $outlineStepCount;
50 public function __construct(ResultToStringConverter $resultConverter, JUnitOutlineStoreListener $outlineListener)
52 $this->resultConverter = $resultConverter;
53 $this->outlineStoreListener = $outlineListener;
59 public function printOpenTag(Formatter $formatter, FeatureNode $feature, ScenarioLikeInterface $scenario, TestResult $result)
61 $name = implode(' ', array_map(function ($l) {
63 }, explode("\n", $scenario->getTitle())));
65 if ($scenario instanceof ExampleNode) {
66 $name = $this->buildExampleName($scenario);
69 /** @var JUnitOutputPrinter $outputPrinter */
70 $outputPrinter = $formatter->getOutputPrinter();
72 $outputPrinter->addTestcase(array(
74 'status' => $this->resultConverter->convertResultToString($result)
79 * @param ExampleNode $scenario
82 private function buildExampleName(ExampleNode $scenario)
84 $currentOutline = $this->outlineStoreListener->getCurrentOutline($scenario);
85 if ($currentOutline === $this->lastOutline) {
86 $this->outlineStepCount++;
88 $this->lastOutline = $currentOutline;
89 $this->outlineStepCount = 1;
92 $name = $currentOutline->getTitle() . ' #' . $this->outlineStepCount;