Completed
Push — master ( 132b95...1b4be3 )
by Richard
9s
created

ErrorTester   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 0
loc 101
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A setUp() 0 8 1
B test() 0 33 5
A tearDown() 0 10 1
1
<?php
2
3
namespace Rmiller\ErrorExtension\Tester;
4
5
use Behat\Behat\Tester\Result\ExecutedStepResult;
6
use Behat\Behat\Tester\Result\StepResult;
7
use Behat\Behat\Tester\StepTester;
8
use Behat\Gherkin\Node\FeatureNode;
9
use Behat\Gherkin\Node\StepNode;
10
use Behat\Testwork\Call\Exception\FatalThrowableError;
11
use Behat\Testwork\Environment\Environment;
12
use Behat\Testwork\Tester\Setup\Setup;
13
use Behat\Testwork\Tester\Setup\Teardown;
14
use RMiller\ErrorExtension\Observer\ErrorObservers;
15
use Symfony\Component\Console\Helper\FormatterHelper;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class ErrorTester implements StepTester
19
{
20
    private $baseTester;
21
    private $observers;
22
    private $output;
23
24
    public function __construct(
25
        StepTester $baseTester,
26
        OutputInterface $output,
27
        array $observers = null
28
    ) {
29
        $this->baseTester = $baseTester;
30
        $this->output = $output;
31
        $this->observers = new ErrorObservers($observers);
0 ignored issues
show
Bug introduced by
It seems like $observers defined by parameter $observers on line 27 can also be of type null; however, RMiller\ErrorExtension\O...bservers::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
32
    }
33
34
    /**
35
     * Sets up suite for a test.
36
     *
37
     * @param Environment $env
38
     * @param FeatureNode $feature
39
     * @param StepNode $step
40
     * @param bool $skip
41
     *
42
     * @return Setup
43
     */
44
    public function setUp(
45
        Environment $env,
46
        FeatureNode $feature,
47
        StepNode $step,
48
        $skip
49
    ) {
50
        return $this->baseTester->setUp($env, $feature, $step, $skip);
51
    }
52
53
    /**
54
     * Tests provided suite specifications.
55
     *
56
     * @param Environment $env
57
     * @param FeatureNode $feature
58
     * @param StepNode $step
59
     * @param bool $skip
60
     *
61
     * @return StepResult
62
     */
63
    public function test(
64
        Environment $env,
65
        FeatureNode $feature,
66
        StepNode $step,
67
        $skip = false
68
    ) {
69
        $result = $this->baseTester->test($env, $feature, $step, $skip);
70
71
        if ($result instanceof ExecutedStepResult && $result->hasException()) {
72
            $exception = $result->getException();
73
74
            if ($exception instanceof FatalThrowableError) {
75
                $errorMessages = [
76
                    sprintf('The error "%s"', $exception->getMessage()),
77
                    sprintf('occurred in step %s', $step->getText()),
78
                    sprintf('at line %s', $step->getLine()),
79
                ];
80
81
                $formatter = new FormatterHelper();
82
                $formattedBlock = $formatter->formatBlock($errorMessages,
83
                    'error', true);
84
                $this->output->writeln('');
85
                $this->output->writeln($formattedBlock);
86
                $this->output->writeln('');
87
88
                foreach ($this->observers as $observer) {
89
                    $observer->notify($exception);
90
                }
91
            }
92
        }
93
94
        return $result;
95
    }
96
97
    /**
98
     * Tears down suite after a test.
99
     *
100
     * @param Environment $env
101
     * @param FeatureNode $feature
102
     * @param StepNode $step
103
     * @param bool $skip
104
     * @param StepResult $result
105
     *
106
     * @return Teardown
107
     */
108
    public function tearDown(
109
        Environment $env,
110
        FeatureNode $feature,
111
        StepNode $step,
112
        $skip,
113
        StepResult $result
114
    ) {
115
        return $this->baseTester->tearDown($env, $feature, $step, $skip,
116
            $result);
117
    }
118
}
119