Passed
Pull Request — master (#251)
by San
03:43
created

DebugContext::displayProfilerLink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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
    /**
11
     * @var string
12
     */
13
    private $screenshotDir;
14
15
    public function __construct($screenshotDir = '.')
16
    {
17
        $this->screenshotDir = $screenshotDir;
18
    }
19
20
    /**
21
     * Pauses the scenario until the user presses a key. Useful when debugging a scenario.
22
     *
23
     * @Then (I )put a breakpoint
24
     */
25
    public function iPutABreakpoint()
26
    {
27
        fwrite(STDOUT, "\033[s    \033[93m[Breakpoint] Press \033[1;93m[RETURN]\033[0;93m to continue...\033[0m");
28
        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...
29
        }
30
        fwrite(STDOUT, "\033[u");
31
32
        return;
33
    }
34
35
    /**
36
     * Saving a screenshot
37
     *
38
     * @When I save a screenshot in :filename
39
     */
40
    public function iSaveAScreenshotIn($filename)
41
    {
42
        sleep(1);
43
        $this->saveScreenshot($filename, $this->screenshotDir);
44
    }
45
46
    /**
47
     * @AfterStep
48
     */
49
    public function failScreenshots(AfterStepScope $scope)
50
    {
51
        if (! $scope->getTestResult()->isPassed()) {
52
            $this->displayProfilerLink();
53
54
            $suiteName      = urlencode(str_replace(' ', '_', $scope->getSuite()->getName()));
55
            $featureName    = urlencode(str_replace(' ', '_', $scope->getFeature()->getTitle()));
56
            if ($this->getBackground($scope)) {
57
                $makeScreenshot = $scope->getFeature()->hasTag('javascript');
58
                $scenarioName   = 'background';
59
            } else {
60
                $scenario       = $this->getScenario($scope);
61
                $makeScreenshot = $scope->getFeature()->hasTag('javascript') || $scenario->hasTag('javascript');
62
                $scenarioName   = urlencode(str_replace(' ', '_', $scenario->getTitle()));
63
            }
64
            if ($makeScreenshot) {
65
                $filename = sprintf('fail_%s_%s_%s_%s.png', time(), $suiteName, $featureName, $scenarioName);
66
                $this->saveScreenshot($filename, $this->screenshotDir);
67
            }
68
        }
69
    }
70
71
    private function displayProfilerLink()
72
    {
73
        $headers = $this->getMink()->getSession()->getResponseHeaders();
74
        if (isset($headers['X-Debug-Token-Link'][0])) {
75
            echo "The debug profile URL {$headers['X-Debug-Token-Link'][0]}";
76
        }
77
    }
78
79
    /**
80
     * @param AfterStepScope $scope
81
     * @return \Behat\Gherkin\Node\ScenarioInterface
82
     */
83
    private function getScenario(AfterStepScope $scope)
84
    {
85
        $scenarios = $scope->getFeature()->getScenarios();
86
        foreach ($scenarios as $scenario) {
87
            $stepLinesInScenario = array_map(
88
                function (StepNode $step) {
89
                    return $step->getLine();
90
                },
91
                $scenario->getSteps()
92
            );
93
            if (in_array($scope->getStep()->getLine(), $stepLinesInScenario)) {
94
                return $scenario;
95
            }
96
        }
97
98
        throw new \LogicException('Unable to find the scenario');
99
    }
100
101
    /**
102
     * @param AfterStepScope $scope
103
     * @return \Behat\Gherkin\Node\BackgroundNode|bool
104
     */
105
    private function getBackground(AfterStepScope $scope)
106
    {
107
        $background = $scope->getFeature()->getBackground();
108
        if(!$background){
109
            return false;
110
        }
111
        $stepLinesInBackground = array_map(
112
            function (StepNode $step) {
113
                return $step->getLine();
114
            },
115
            $background->getSteps()
116
        );
117
        if (in_array($scope->getStep()->getLine(), $stepLinesInBackground)) {
118
            return $background;
119
        }
120
121
        return false;
122
    }
123
}
124