Passed
Push — 3.x ( 709340...a6d46d )
by San
04:41
created

SystemContext   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 208
Duplicated Lines 4.81 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 33
lcom 2
cbo 1
dl 10
loc 208
rs 9.3999
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTranslationResources() 0 4 1
A iExecute() 0 8 1
A iExecuteFromProjectRoot() 0 5 1
A commandShouldSucceed() 0 5 2
A commandShouldFail() 0 5 2
A commandShouldLastLessThan() 0 6 2
A commandShouldMoreLessThan() 0 6 2
A outputShouldContain() 0 16 4
A ouputShouldNotContain() 0 8 1
A outputShouldNotContain() 5 10 3
A outputShouldBe() 5 9 3
A outputShouldNotBe() 0 16 4
A iCreateTheFileContaining() 0 10 2
A printTheContentOfFile() 0 9 2
A after() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    public function ouputShouldNotContain($text)
121
    {
122
        trigger_error(
123
            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__),
124
            E_USER_DEPRECATED
125
        );
126
        $this->outputShouldNotContain($text);
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