2 namespace Behat\Behat\Output\Node\Printer\JUnit;
4 use Behat\Behat\Hook\Scope\StepScope;
5 use Behat\Behat\Output\Node\Printer\SetupPrinter;
6 use Behat\Testwork\Call\CallResult;
7 use Behat\Testwork\Call\CallResults;
8 use Behat\Testwork\Exception\ExceptionPresenter;
9 use Behat\Testwork\Hook\Call\HookCall;
10 use Behat\Testwork\Hook\Tester\Setup\HookedSetup;
11 use Behat\Testwork\Hook\Tester\Setup\HookedTeardown;
12 use Behat\Testwork\Output\Formatter;
13 use Behat\Testwork\Output\Printer\JUnitOutputPrinter;
14 use Behat\Testwork\Tester\Setup\Setup;
15 use Behat\Testwork\Tester\Setup\Teardown;
18 * @author: Jakob Erdmann <jakob.erdmann@rocket-internet.com>
20 class JUnitSetupPrinter implements SetupPrinter
23 /** @var ExceptionPresenter */
24 private $exceptionPresenter;
26 public function __construct(ExceptionPresenter $exceptionPresenter)
28 $this->exceptionPresenter = $exceptionPresenter;
34 public function printSetup(Formatter $formatter, Setup $setup)
36 if (!$setup->isSuccessful()) {
37 if ($setup instanceof HookedSetup) {
38 $this->handleHookCalls($formatter, $setup->getHookCallResults(), 'setup');
46 public function printTeardown(Formatter $formatter, Teardown $teardown)
48 if (!$teardown->isSuccessful()) {
49 if ($teardown instanceof HookedTeardown) {
50 $this->handleHookCalls($formatter, $teardown->getHookCallResults(), 'teardown');
56 * @param Formatter $formatter
57 * @param CallResults $results
58 * @param string $messageType
60 private function handleHookCalls(Formatter $formatter, CallResults $results, $messageType)
62 /** @var CallResult $hookCallResult */
63 foreach ($results as $hookCallResult) {
64 if ($hookCallResult->hasException()) {
65 /** @var HookCall $call */
66 $call = $hookCallResult->getCall();
67 $scope = $call->getScope();
68 /** @var JUnitOutputPrinter $outputPrinter */
69 $outputPrinter = $formatter->getOutputPrinter();
72 if ($scope instanceof StepScope) {
73 $message .= $scope->getStep()->getKeyword() . ' ' . $scope->getStep()->getText() . ': ';
75 $message .= $this->exceptionPresenter->presentException($hookCallResult->getException());
78 'message' => $message,
79 'type' => $messageType,
82 $outputPrinter->addTestcaseChild('failure', $attributes);