Completed
Push — master ( 158f00...4ee9ec )
by San
06:17 queued 02:55
created

DebugContext   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A iPutABreakpoint() 0 9 2
A iSaveAScreenshotIn() 0 5 1
B failScreenshots() 0 20 5
A getScenario() 0 17 3
A getBackground() 0 18 3
1
<?php
2
3
namespace Behatch\Context;
4
5
use Behat\Gherkin\Node\StepNode;
6
use Behat\Behat\Hook\Scope\AfterStepScope;
7
8
class DebugContext extends BaseContext
9
{
10
    private $screenshotDir;
11
12
    public function __construct($screenshotDir = '.')
13
    {
14
        $this->screenshotDir = $screenshotDir;
15
    }
16
17
    /**
18
     * Pauses the scenario until the user presses a key. Useful when debugging a scenario.
19
     *
20
     * @Then (I )put a breakpoint
21
     */
22
    public function iPutABreakpoint()
23
    {
24
        fwrite(STDOUT, "\033[s    \033[93m[Breakpoint] Press \033[1;93m[RETURN]\033[0;93m to continue...\033[0m");
25
        while (fgets(STDIN, 1024) == '') {
0 ignored issues
show
Unused Code introduced by
This while loop is empty and can be removed.

This check looks for while loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
26
        }
27
        fwrite(STDOUT, "\033[u");
28
29
        return;
30
    }
31
32
    /**
33
     * Saving a screenshot
34
     *
35
     * @When I save a screenshot in :filename
36
     */
37
    public function iSaveAScreenshotIn($filename)
38
    {
39
        sleep(1);
40
        $this->saveScreenshot($filename, $this->screenshotDir);
41
    }
42
43
    /**
44
     * @AfterStep
45
     */
46
    public function failScreenshots(AfterStepScope $scope)
47
    {
48
        if (! $scope->getTestResult()->isPassed()) {
49
            $makeScreenshot = false;
0 ignored issues
show
Unused Code introduced by
$makeScreenshot is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
            $suiteName      = urlencode(str_replace(' ', '_', $scope->getSuite()->getName()));
51
            $featureName    = urlencode(str_replace(' ', '_', $scope->getFeature()->getTitle()));
52
            if ($this->getBackground($scope)) {
53
                $makeScreenshot = $scope->getFeature()->hasTag('javascript');
54
                $scenarioName   = 'background';
55
            } else {
56
                $scenario       = $this->getScenario($scope);
57
                $makeScreenshot = $scope->getFeature()->hasTag('javascript') || $scenario->hasTag('javascript');
58
                $scenarioName   = urlencode(str_replace(' ', '_', $scenario->getTitle()));
59
            }
60
            if ($makeScreenshot) {
61
                $filename = sprintf('fail_%s_%s_%s_%s.png', time(), $suiteName, $featureName, $scenarioName);
62
                $this->saveScreenshot($filename, $this->screenshotDir);
63
            }
64
        }
65
    }
66
67
    /**
68
     * @param AfterStepScope $scope
69
     * @return \Behat\Gherkin\Node\ScenarioInterface
70
     */
71
    private function getScenario(AfterStepScope $scope)
72
    {
73
        $scenarios = $scope->getFeature()->getScenarios();
74
        foreach ($scenarios as $scenario) {
75
            $stepLinesInScenario = array_map(
76
                function (StepNode $step) {
77
                    return $step->getLine();
78
                },
79
                $scenario->getSteps()
80
            );
81
            if (in_array($scope->getStep()->getLine(), $stepLinesInScenario)) {
82
                return $scenario;
83
            }
84
        }
85
86
        throw new \LogicException('Unable to find the scenario');
87
    }
88
89
    /**
90
     * @param AfterStepScope $scope
91
     * @return \Behat\Gherkin\Node\BackgroundNode
92
     */
93
    private function getBackground(AfterStepScope $scope)
94
    {
95
        $background = $scope->getFeature()->getBackground();
96
        if(!$background){
97
            return false;
98
        }
99
        $stepLinesInBackground = array_map(
100
            function (StepNode $step) {
101
                return $step->getLine();
102
            },
103
            $background->getSteps()
104
        );
105
        if (in_array($scope->getStep()->getLine(), $stepLinesInBackground)) {
106
            return $background;
107
        }
108
109
        return false;
110
    }
111
}
112