Completed
Push — master ( a33641...7233dc )
by San
02:42
created

SystemContext::putFileIntoField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

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 4
nc 1
nop 2
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
     * Command should succeed
53
     *
54
     * @Then command should succeed
55
     */
56
    public function commandShouldSucceed() {
57
        if ($this->lastReturnCode !== 0) {
58
            throw new \Exception(sprintf("Command should succeed %b", $this->lastReturnCode));
59
        };
60
    }
61
62
    /**
63
     * Command should fail
64
     *
65
     * @Then command should fail
66
     */
67
    public function commandShouldFail() {
68
        if ($this->lastReturnCode === 0) {
69
            throw new \Exception(sprintf("Command should fail %b", $this->lastReturnCode));
70
        };
71
    }
72
73
    /**
74
     * Command should last less than
75
     *
76
     * @Then command should last less than :seconds seconds
77
     */
78
    public function commandShouldLastLessThan($seconds)
79
    {
80
        if ($this->lastExecutionTime > $seconds) {
81
            throw new \Exception(sprintf("Last command last %s which is more than %s seconds", $this->lastExecutionTime, $seconds));
82
        }
83
    }
84
85
    /**
86
     * Command should last more than
87
     *
88
     * @Then command should last more than :seconds seconds
89
     */
90
    public function commandShouldMoreLessThan($seconds)
91
    {
92
        if ($this->lastExecutionTime < $seconds) {
93
            throw new \Exception(sprintf("Last command last %s which is less than %s seconds", $this->lastExecutionTime, $seconds));
94
        }
95
    }
96
97
    /**
98
     * Checks, that output contains specified text.
99
     *
100
     * @Then output should contain :text
101
     */
102
    public function outputShouldContain($text)
103
    {
104
        $regex = '~'.$text.'~ui';
105
106
        $check = false;
107
        foreach ($this->output as $line) {
108
            if (preg_match($regex, $line) === 1) {
109
                $check = true;
110
                break;
111
            }
112
        }
113
114
        if ($check === false) {
115
            throw new \Exception(sprintf("The text '%s' was not found anywhere on output of command.\n%s", $text, implode("\n", $this->output)));
116
        }
117
    }
118
119
    /**
120
     * Checks, that output not contains specified text.
121
     *
122
     * @Then output should not contain :text
123
     */
124
    public function outputShouldNotContain($text)
125
    {
126
        $regex = '~'.$text.'~ui';
127
128 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...
129
            if (preg_match($regex, $line) === 1) {
130
                throw new \Exception(sprintf("The text '%s' was found somewhere on output of command.\n%s", $text, implode("\n", $this->output)));
131
            }
132
        }
133
    }
134
135
    /**
136
     * @Given output should be:
137
     */
138
    public function outputShouldBe(PyStringNode $string)
139
    {
140
        $expected = $string->getStrings();
141 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...
142
            if ($line !== $expected[$index]) {
143
                throw new \Exception(sprintf("instead of\n%s", implode("\n", $this->output)));
144
            }
145
        }
146
    }
147
148
    /**
149
     * @Given output should not be:
150
     */
151
    public function outputShouldNotBe(PyStringNode $string)
152
    {
153
        $expected = $string->getStrings();
154
155
        $check = false;
156
        foreach ($this->output as $index => $line) {
157
            if ($line !== $expected[$index]) {
158
                $check = true;
159
                break;
160
            }
161
        }
162
163
        if ($check === false) {
164
            throw new \Exception("Output should not be");
165
        }
166
    }
167
168
    /**
169
     * @Given (I )create the file :filename containing:
170
     * @Given (I )create the file :filename contening:
171
     */
172
    public function iCreateTheFileContaining($filename, PyStringNode $string)
173
    {
174
        if (!is_file($filename)) {
175
            file_put_contents($filename, $string);
176
            $this->createdFiles[] = $filename;
177
        }
178
        else {
179
            throw new \RuntimeException("'$filename' already exists.");
180
        }
181
    }
182
183
    /**
184
     * @Then print the content of :filename file
185
     */
186
    public function printTheContentOfFile($filename)
187
    {
188
        if (is_file($filename)) {
189
            echo file_get_contents($filename);
190
        }
191
        else {
192
            throw new \RuntimeException("'$filename' doesn't exists.");
193
        }
194
    }
195
196
    /**
197
     * @AfterScenario
198
     */
199
    public function after()
200
    {
201
        foreach ($this->createdFiles as $filename) {
202
            unlink($filename);
203
        }
204
    }
205
}
206