Passed
Push — master ( ea51e9...96dec0 )
by San
02:15
created

SystemContext::ouputShouldNotContain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace Behatch\Context;
4
5
use Behat\Behat\Context\Step;
6
use Behat\Behat\Context\Context;
7
use Behat\Gherkin\Node\PyStringNode;
8
9
class SystemContext implements Context
10
{
11
    private $root;
12
    private $output;
13
    private $lastExecutionTime;
14
    private $lastReturnCode;
15
    private $createdFiles = [];
16
17
    public function __construct($root = '.')
18
    {
19
        $this->root = $root;
20
    }
21
22
    public static function getTranslationResources()
23
    {
24
        return glob(__DIR__ . '/../../i18n/*.xliff');
25
    }
26
27
    /**
28
     * Uploads a file using the specified input field
29
     *
30
     * @When (I )put the file :file into :field
31
     */
32
    public function putFileIntoField($file, $field)
33
    {
34
        $path = $this->root . DIRECTORY_SEPARATOR . $file;
35
36
        return [
37
            new Step\When("I attach the file '$path' to '$field'")
38
        ];
39
    }
40
41
    /**
42
     * Execute a command
43
     *
44
     * @Given (I )execute :command
45
     */
46
    public function iExecute($cmd)
47
    {
48
        $start = microtime(true);
49
50
        exec($cmd, $this->output, $this->lastReturnCode);
51
52
        $this->lastExecutionTime = microtime(true) - $start;
53
    }
54
55
    /**
56
     * Execute a command from project root
57
     *
58
     * @Given (I )execute :command from project root
59
     */
60
    public function iExecuteFromProjectRoot($cmd)
61
    {
62
        $cmd = $this->root . DIRECTORY_SEPARATOR . $cmd;
63
        $this->iExecute($cmd);
64
    }
65
66
    /**
67
     * Command should succeed
68
     *
69
     * @Then command should succeed
70
     */
71
    public function commandShouldSucceed() {
72
        if ($this->lastReturnCode !== 0) {
73
            throw new \Exception(sprintf("Command should succeed %b", $this->lastReturnCode));
74
        };
75
    }
76
77
    /**
78
     * Command should fail
79
     *
80
     * @Then command should fail
81
     */
82
    public function commandShouldFail() {
83
        if ($this->lastReturnCode === 0) {
84
            throw new \Exception(sprintf("Command should fail %b", $this->lastReturnCode));
85
        };
86
    }
87
88
    /**
89
     * Command should last less than
90
     *
91
     * @Then command should last less than :seconds seconds
92
     */
93
    public function commandShouldLastLessThan($seconds)
94
    {
95
        if ($this->lastExecutionTime > $seconds) {
96
            throw new \Exception(sprintf("Last command last %s which is more than %s seconds", $this->lastExecutionTime, $seconds));
97
        }
98
    }
99
100
    /**
101
     * Command should last more than
102
     *
103
     * @Then command should last more than :seconds seconds
104
     */
105
    public function commandShouldMoreLessThan($seconds)
106
    {
107
        if ($this->lastExecutionTime < $seconds) {
108
            throw new \Exception(sprintf("Last command last %s which is less than %s seconds", $this->lastExecutionTime, $seconds));
109
        }
110
    }
111
112
    /**
113
     * Checks, that output contains specified text.
114
     *
115
     * @Then output should contain :text
116
     */
117
    public function outputShouldContain($text)
118
    {
119
        $regex = '~'.$text.'~ui';
120
121
        $check = false;
122
        foreach ($this->output as $line) {
123
            if (preg_match($regex, $line) === 1) {
124
                $check = true;
125
                break;
126
            }
127
        }
128
129
        if ($check === false) {
130
            throw new \Exception(sprintf("The text '%s' was not found anywhere on output of command.\n%s", $text, implode("\n", $this->output)));
131
        }
132
    }
133
134
135
    public function ouputShouldNotContain($text)
136
    {
137
        trigger_error(
138
            sprintf('The %s function is deprecated since version 2.8 and will be removed in 3.0. Use the %s::outputShouldNotContain function instead.', __METHOD__, __CLASS__),
139
            E_USER_DEPRECATED
140
        );
141
        $this->outputShouldNotContain($text);
142
    }
143
144
    /**
145
     * Checks, that output not contains specified text.
146
     *
147
     * @Then output should not contain :text
148
     */
149
    public function outputShouldNotContain($text)
150
    {
151
        $regex = '~'.$text.'~ui';
152
153 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...
154
            if (preg_match($regex, $line) === 1) {
155
                throw new \Exception(sprintf("The text '%s' was found somewhere on output of command.\n%s", $text, implode("\n", $this->output)));
156
            }
157
        }
158
    }
159
160
    /**
161
     * @Given output should be:
162
     */
163
    public function outputShouldBe(PyStringNode $string)
164
    {
165
        $expected = $string->getStrings();
166 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...
167
            if ($line !== $expected[$index]) {
168
                throw new \Exception(sprintf("instead of\n%s", implode("\n", $this->output)));
169
            }
170
        }
171
    }
172
173
    /**
174
     * @Given output should not be:
175
     */
176
    public function outputShouldNotBe(PyStringNode $string)
177
    {
178
        $expected = $string->getStrings();
179
180
        $check = false;
181
        foreach ($this->output as $index => $line) {
182
            if ($line !== $expected[$index]) {
183
                $check = true;
184
                break;
185
            }
186
        }
187
188
        if ($check === false) {
189
            throw new \Exception("Output should not be");
190
        }
191
    }
192
193
    /**
194
     * @Given (I )create the file :filename containing:
195
     * @Given (I )create the file :filename contening:
196
     */
197
    public function iCreateTheFileContaining($filename, PyStringNode $string)
198
    {
199
        if (!is_file($filename)) {
200
            file_put_contents($filename, $string);
201
            $this->createdFiles[] = $filename;
202
        }
203
        else {
204
            throw new \RuntimeException("'$filename' already exists.");
205
        }
206
    }
207
208
    /**
209
     * @Then print the content of :filename file
210
     */
211
    public function printTheContentOfFile($filename)
212
    {
213
        if (is_file($filename)) {
214
            echo file_get_contents($filename);
215
        }
216
        else {
217
            throw new \RuntimeException("'$filename' doesn't exists.");
218
        }
219
    }
220
221
    /**
222
     * @AfterScenario
223
     */
224
    public function after()
225
    {
226
        foreach ($this->createdFiles as $filename) {
227
            unlink($filename);
228
        }
229
    }
230
}
231