SystemContext::outputShouldBe()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 5
Ratio 55.56 %

Importance

Changes 0
Metric Value
dl 5
loc 9
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Behatch\Context;
4
5
use Behat\Behat\Context\Context;
6
use Behat\Gherkin\Node\PyStringNode;
7
8
class SystemContext implements Context
9
{
10
    private $root;
11
    private $output;
12
    private $lastExecutionTime;
13
    private $lastReturnCode;
14
    private $createdFiles = [];
15
16
    public function __construct($root = '.')
17
    {
18
        $this->root = $root;
19
    }
20
21
    public static function getTranslationResources()
22
    {
23
        return glob(__DIR__ . '/../../i18n/*.xliff');
24
    }
25
26
    /**
27
     * Execute a command
28
     *
29
     * @Given (I )execute :command
30
     */
31
    public function iExecute($cmd)
32
    {
33
        $start = microtime(true);
34
35
        exec($cmd, $this->output, $this->lastReturnCode);
36
37
        $this->lastExecutionTime = microtime(true) - $start;
38
    }
39
40
    /**
41
     * Execute a command from project root
42
     *
43
     * @Given (I )execute :command from project root
44
     */
45
    public function iExecuteFromProjectRoot($cmd)
46
    {
47
        $cmd = $this->root . DIRECTORY_SEPARATOR . $cmd;
48
        $this->iExecute($cmd);
49
    }
50
51
    /**
52
     * Display the last command output
53
     *
54
     * @Then (I )display the last command output
55
     */
56
    public function iDumpCommandOutput()
57
    {
58
        echo implode(PHP_EOL, $this->output);
59
    }
60
61
    /**
62
     * Command should succeed
63
     *
64
     * @Then command should succeed
65
     */
66
    public function commandShouldSucceed() {
67
        if ($this->lastReturnCode !== 0) {
68
            throw new \Exception(sprintf("Command should succeed %b", $this->lastReturnCode));
69
        };
70
    }
71
72
    /**
73
     * Command should fail
74
     *
75
     * @Then command should fail
76
     */
77
    public function commandShouldFail() {
78
        if ($this->lastReturnCode === 0) {
79
            throw new \Exception(sprintf("Command should fail %b", $this->lastReturnCode));
80
        };
81
    }
82
83
    /**
84
     * Command should last less than
85
     *
86
     * @Then command should last less than :seconds seconds
87
     */
88
    public function commandShouldLastLessThan($seconds)
89
    {
90
        if ($this->lastExecutionTime > $seconds) {
91
            throw new \Exception(sprintf("Last command last %s which is more than %s seconds", $this->lastExecutionTime, $seconds));
92
        }
93
    }
94
95
    /**
96
     * Command should last more than
97
     *
98
     * @Then command should last more than :seconds seconds
99
     */
100
    public function commandShouldMoreLessThan($seconds)
101
    {
102
        if ($this->lastExecutionTime < $seconds) {
103
            throw new \Exception(sprintf("Last command last %s which is less than %s seconds", $this->lastExecutionTime, $seconds));
104
        }
105
    }
106
107
    /**
108
     * Checks, that output contains specified text.
109
     *
110
     * @Then output should contain :text
111
     */
112
    public function outputShouldContain($text)
113
    {
114
        $regex = '~'.$text.'~ui';
115
116
        $check = false;
117
        foreach ($this->output as $line) {
118
            if (preg_match($regex, $line) === 1) {
119
                $check = true;
120
                break;
121
            }
122
        }
123
124
        if ($check === false) {
125
            throw new \Exception(sprintf("The text '%s' was not found anywhere on output of command.\n%s", $text, implode("\n", $this->output)));
126
        }
127
    }
128
129
    /**
130
     * Checks, that output not contains specified text.
131
     *
132
     * @Then output should not contain :text
133
     */
134
    public function outputShouldNotContain($text)
135
    {
136
        $regex = '~'.$text.'~ui';
137
138 View Code Duplication
        foreach ($this->output as $line) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
            if (preg_match($regex, $line) === 1) {
140
                throw new \Exception(sprintf("The text '%s' was found somewhere on output of command.\n%s", $text, implode("\n", $this->output)));
141
            }
142
        }
143
    }
144
145
    /**
146
     * @Given output should be:
147
     */
148
    public function outputShouldBe(PyStringNode $string)
149
    {
150
        $expected = $string->getStrings();
151 View Code Duplication
        foreach ($this->output as $index => $line) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
            if ($line !== $expected[$index]) {
153
                throw new \Exception(sprintf("instead of\n%s", implode("\n", $this->output)));
154
            }
155
        }
156
    }
157
158
    /**
159
     * @Given output should not be:
160
     */
161
    public function outputShouldNotBe(PyStringNode $string)
162
    {
163
        $expected = $string->getStrings();
164
165
        $check = false;
166
        foreach ($this->output as $index => $line) {
167
            if ($line !== $expected[$index]) {
168
                $check = true;
169
                break;
170
            }
171
        }
172
173
        if ($check === false) {
174
            throw new \Exception("Output should not be");
175
        }
176
    }
177
178
    /**
179
     * @Given (I )create the file :filename containing:
180
     * @Given (I )create the file :filename contening:
181
     */
182
    public function iCreateTheFileContaining($filename, PyStringNode $string)
183
    {
184
        if (!is_file($filename)) {
185
            file_put_contents($filename, $string);
186
            $this->createdFiles[] = $filename;
187
        }
188
        else {
189
            throw new \RuntimeException("'$filename' already exists.");
190
        }
191
    }
192
193
    /**
194
     * @Then print the content of :filename file
195
     */
196
    public function printTheContentOfFile($filename)
197
    {
198
        if (is_file($filename)) {
199
            echo file_get_contents($filename);
200
        }
201
        else {
202
            throw new \RuntimeException("'$filename' doesn't exists.");
203
        }
204
    }
205
206
    /**
207
     * @AfterScenario
208
     */
209
    public function after()
210
    {
211
        foreach ($this->createdFiles as $filename) {
212
            unlink($filename);
213
        }
214
    }
215
}
216