Passed
Pull Request — master (#251)
by San
02:46
created

DebugContext::displayProfilerLink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
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
        try {
74
            $headers = $this->getMink()->getSession()->getResponseHeaders();
75
            echo "The debug profile URL {$headers['X-Debug-Token-Link'][0]}";
76
        } catch (\Exception $e) {
77
            /* Intentionally leave blank */
78
        }
79
    }
80
81
    /**
82
     * @param AfterStepScope $scope
83
     * @return \Behat\Gherkin\Node\ScenarioInterface
84
     */
85
    private function getScenario(AfterStepScope $scope)
86
    {
87
        $scenarios = $scope->getFeature()->getScenarios();
88
        foreach ($scenarios as $scenario) {
89
            $stepLinesInScenario = array_map(
90
                function (StepNode $step) {
91
                    return $step->getLine();
92
                },
93
                $scenario->getSteps()
94
            );
95
            if (in_array($scope->getStep()->getLine(), $stepLinesInScenario)) {
96
                return $scenario;
97
            }
98
        }
99
100
        throw new \LogicException('Unable to find the scenario');
101
    }
102
103
    /**
104
     * @param AfterStepScope $scope
105
     * @return \Behat\Gherkin\Node\BackgroundNode|bool
106
     */
107
    private function getBackground(AfterStepScope $scope)
108
    {
109
        $background = $scope->getFeature()->getBackground();
110
        if(!$background){
111
            return false;
112
        }
113
        $stepLinesInBackground = array_map(
114
            function (StepNode $step) {
115
                return $step->getLine();
116
            },
117
            $background->getSteps()
118
        );
119
        if (in_array($scope->getStep()->getLine(), $stepLinesInBackground)) {
120
            return $background;
121
        }
122
123
        return false;
124
    }
125
}
126