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\Tester\Result\StepResult;
14 use Behat\Behat\Output\Node\Printer\StepPrinter;
15 use Behat\Gherkin\Node\ScenarioLikeInterface as Scenario;
16 use Behat\Gherkin\Node\StepNode;
17 use Behat\Testwork\Exception\ExceptionPresenter;
18 use Behat\Testwork\Output\Formatter;
19 use Behat\Testwork\Output\Printer\JUnitOutputPrinter;
20 use Behat\Testwork\Tester\Result\TestResult;
21 use Behat\Testwork\Tester\Result\ExceptionResult;
24 * Prints step with optional results.
26 * @author Wouter J <wouter@wouterj.nl>
27 * @author James Watson <james@sitepulse.org>
29 class JUnitStepPrinter implements StepPrinter
32 * @var ExceptionPresenter
34 private $exceptionPresenter;
36 public function __construct(ExceptionPresenter $exceptionPresenter)
38 $this->exceptionPresenter = $exceptionPresenter;
42 * Prints step using provided printer.
44 * @param Formatter $formatter
45 * @param Scenario $scenario
46 * @param StepNode $step
47 * @param StepResult $result
49 public function printStep(Formatter $formatter, Scenario $scenario, StepNode $step, StepResult $result)
51 /** @var JUnitOutputPrinter $outputPrinter */
52 $outputPrinter = $formatter->getOutputPrinter();
54 $message = $step->getKeyword() . ' ' . $step->getText();
56 if ($result instanceof ExceptionResult && $result->hasException()) {
57 $message .= ': ' . $this->exceptionPresenter->presentException($result->getException());
60 $attributes = array('message' => $message);
62 switch ($result->getResultCode()) {
63 case TestResult::FAILED:
64 $outputPrinter->addTestcaseChild('failure', $attributes);
67 case TestResult::PENDING:
68 $attributes['type'] = 'pending';
69 $outputPrinter->addTestcaseChild('error', $attributes);
72 case StepResult::UNDEFINED:
73 $attributes['type'] = 'undefined';
74 $outputPrinter->addTestcaseChild('error', $attributes);