Completed
Push — master ( 158f00...4ee9ec )
by San
06:17 queued 02:55
created

SystemContext   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 212
Duplicated Lines 4.72 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 33
lcom 2
cbo 1
dl 10
loc 212
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 putFileIntoField() 0 8 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() 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\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", $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", $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
     * Checks, that output not contains specified text.
136
     *
137
     * @Then output should not contain :text
138
     */
139
    public function ouputShouldNotContain($text)
140
    {
141
        $regex = '~'.$text.'~ui';
142
143 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...
144
            if (preg_match($regex, $line) === 1) {
145
                throw new \Exception(sprintf("The text '%s' was found somewhere on output of command.\n%s", $text, implode("\n", $this->output)));
146
            }
147
        }
148
    }
149
150
    /**
151
     * @Given output should be:
152
     */
153
    public function outputShouldBe(PyStringNode $string)
154
    {
155
        $expected = $string->getStrings();
156 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...
157
            if ($line !== $expected[$index]) {
158
                throw new \Exception(sprintf("instead of\n%s", implode("\n", $this->output)));
159
            }
160
        }
161
    }
162
163
    /**
164
     * @Given output should not be:
165
     */
166
    public function outputShouldNotBe(PyStringNode $string)
167
    {
168
        $expected = $string->getStrings();
169
170
        $check = false;
171
        foreach ($this->output as $index => $line) {
172
            if ($line !== $expected[$index]) {
173
                $check = true;
174
                break;
175
            }
176
        }
177
178
        if ($check === false) {
179
            throw new \Exception("Output should not be");
180
        }
181
    }
182
183
    /**
184
     * @Given (I )create the file :filename containing:
185
     * @Given (I )create the file :filename contening:
186
     */
187
    public function iCreateTheFileContaining($filename, PyStringNode $string)
188
    {
189
        if (!is_file($filename)) {
190
            file_put_contents($filename, $string);
191
            $this->createdFiles[] = $filename;
192
        }
193
        else {
194
            throw new \RuntimeException("'$filename' already exists.");
195
        }
196
    }
197
198
    /**
199
     * @Then print the content of :filename file
200
     */
201
    public function printTheContentOfFile($filename)
202
    {
203
        if (is_file($filename)) {
204
            echo file_get_contents($filename);
205
        }
206
        else {
207
            throw new \RuntimeException("'$filename' doesn't exists.");
208
        }
209
    }
210
211
    /**
212
     * @AfterScenario
213
     */
214
    public function after()
215
    {
216
        foreach ($this->createdFiles as $filename) {
217
            unlink($filename);
218
        }
219
    }
220
}
221