Completed
Push — master ( 4066f1...b92adf )
by Greg
02:58
created

Codecept::failGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace Robo\Task\Testing;
3
4
use Robo\Contract\PrintedInterface;
5
use Robo\Exception\TaskException;
6
use Robo\Task\BaseTask;
7
use Robo\Contract\CommandInterface;
8
use Symfony\Component\Process\Process;
9
10
/**
11
 * Executes Codeception tests
12
 *
13
 * ``` php
14
 * <?php
15
 * // config
16
 * $this->taskCodecept()
17
 *      ->suite('acceptance')
18
 *      ->env('chrome')
19
 *      ->group('admin')
20
 *      ->xml()
21
 *      ->html()
22
 *      ->run();
23
 *
24
 * ?>
25
 * ```
26
 *
27
 */
28
class Codecept extends BaseTask implements CommandInterface, PrintedInterface
29
{
30
    use \Robo\Common\ExecOneCommand;
31
    
32
    /**
33
     * @var string
34
     */
35
    protected $command;
36
37
    /**
38
     * @param string $pathToCodeception
39
     *
40
     * @throws \Robo\Exception\TaskException
41
     */
42
    public function __construct($pathToCodeception = '')
43
    {
44
        $this->command = $pathToCodeception;
45
        if (!$this->command) {
46
            $this->command = $this->findExecutable('codecept');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->findExecutable('codecept') can also be of type false. However, the property $command is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
47
        }
48
        if (!$this->command) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->command of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
49
            throw new TaskException(__CLASS__, "Neither composer nor phar installation of Codeception found.");
50
        }
51
        $this->command .= ' run';
52
    }
53
54
    /**
55
     * @param string $suite
56
     *
57
     * @return $this
58
     */
59
    public function suite($suite)
60
    {
61
        $this->option(null, $suite);
62
        return $this;
63
    }
64
65
    /**
66
     * @param string $testName
67
     *
68
     * @return $this
69
     */
70
    public function test($testName)
71
    {
72
        $this->option(null, $testName);
73
        return $this;
74
    }
75
76
    /**
77
     * set group option. Can be called multiple times
78
     *
79
     * @param string $group
80
     *
81
     * @return $this
82
     */
83
    public function group($group)
84
    {
85
        $this->option("group", $group);
86
        return $this;
87
    }
88
89
    /**
90
     * @param string $group
91
     *
92
     * @return $this
93
     */
94
    public function excludeGroup($group)
95
    {
96
        $this->option("skip-group", $group);
97
        return $this;
98
    }
99
100
    /**
101
     * generate json report
102
     *
103
     * @param string $file
104
     *
105
     * @return $this
106
     */
107
    public function json($file = null)
108
    {
109
        $this->option("json", $file);
110
        return $this;
111
    }
112
113
    /**
114
     * generate xml JUnit report
115
     *
116
     * @param string $file
117
     *
118
     * @return $this
119
     */
120
    public function xml($file = null)
121
    {
122
        $this->option("xml", $file);
123
        return $this;
124
    }
125
126
    /**
127
     * Generate html report
128
     *
129
     * @param string $dir
130
     *
131
     * @return $this
132
     */
133
    public function html($dir = null)
134
    {
135
        $this->option("html", $dir);
136
        return $this;
137
    }
138
139
    /**
140
     * generate tap report
141
     *
142
     * @param string $file
143
     *
144
     * @return $this
145
     */
146
    public function tap($file = null)
147
    {
148
        $this->option("tap", $file);
149
        return $this;
150
    }
151
152
    /**
153
     * provides config file other then default `codeception.yml` with `-c` option
154
     *
155
     * @param string $file
156
     *
157
     * @return $this
158
     */
159
    public function configFile($file)
160
    {
161
        $this->option("-c", $file);
162
        return $this;
163
    }
164
165
    /**
166
     * collect codecoverage in raw format. You may pass name of cov file to save results
167
     *
168
     * @param null|string $cov
169
     *
170
     * @return $this
171
     */
172
    public function coverage($cov = null)
173
    {
174
        $this->option("coverage", $cov);
175
        return $this;
176
    }
177
178
    /**
179
     * execute in silent mode
180
     *
181
     * @return $this
182
     */
183
    public function silent()
184
    {
185
        $this->option("silent");
186
        return $this;
187
    }
188
189
    /**
190
     * collect code coverage in xml format. You may pass name of xml file to save results
191
     *
192
     * @param string $xml
193
     *
194
     * @return $this
195
     */
196
    public function coverageXml($xml = null)
197
    {
198
        $this->option("coverage-xml", $xml);
199
        return $this;
200
    }
201
202
    /**
203
     * collect code coverage and generate html report. You may pass
204
     *
205
     * @param string $html
206
     *
207
     * @return $this
208
     */
209
    public function coverageHtml($html = null)
210
    {
211
        $this->option("coverage-html", $html);
212
        return $this;
213
    }
214
215
    /**
216
     * @param string $env
217
     *
218
     * @return $this
219
     */
220
    public function env($env)
221
    {
222
        $this->option("env", $env);
223
        return $this;
224
    }
225
226
    /**
227
     * @return $this
228
     */
229
    public function debug()
230
    {
231
        $this->option("debug");
232
        return $this;
233
    }
234
235
    /**
236
     * @return $this
237
     */
238
    public function noRebuild()
239
    {
240
        $this->option("no-rebuild");
241
        return $this;
242
    }
243
244
    /**
245
     * @param string $failGroup
246
     * @return $this
247
     */
248
    public function failGroup($failGroup)
249
    {
250
        $this->option('override', "extensions: config: Codeception\\Extension\\RunFailed: fail-group: {$failGroup}");
251
        return $this;
252
    }
253
254
    /**
255
     * {@inheritdoc}
256
     */
257
    public function getCommand()
258
    {
259
        return $this->command . $this->arguments;
260
    }
261
262
    /**
263
     * {@inheritdoc}
264
     */
265
    public function run()
266
    {
267
        $command = $this->getCommand();
268
        $this->printTaskInfo('Executing {command}', ['command' => $command]);
269
        return $this->executeCommand($command);
270
    }
271
}
272