Completed
Push — master ( 2127a7...2a0193 )
by Greg
03:25
created

src/Task/Testing/Behat.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Robo\Task\Testing;
3
4
use Robo\Contract\CommandInterface;
5
use Robo\Contract\PrintedInterface;
6
use Robo\Task\BaseTask;
7
8
/**
9
 * Executes Behat tests
10
 *
11
 * ``` php
12
 * <?php
13
 * $this->taskBehat()
14
 *      ->format('pretty')
15
 *      ->noInteraction()
16
 *      ->run();
17
 * ?>
18
 * ```
19
 *
20
 */
21
class Behat extends BaseTask implements CommandInterface, PrintedInterface
22
{
23
    use \Robo\Common\ExecOneCommand;
24
25
    /**
26
     * @var string
27
     */
28
    protected $command;
29
30
    /**
31
     * @var string[] $formaters available formaters for format option
32
     */
33
    protected $formaters = ['progress', 'pretty', 'junit'];
34
35
    /**
36
     * @var string[] $verbose_levels available verbose levels
37
     */
38
    protected $verbose_levels = ['v', 'vv'];
39
40
    /**
41
     * Behat constructor.
42
     *
43
     * @param null|string $pathToBehat
44
     *
45
     * @throws \Robo\Exception\TaskException
46
     */
47
    public function __construct($pathToBehat = null)
48
    {
49
        $this->command = $pathToBehat;
50
        if (!$this->command) {
51
            $this->command = $this->findExecutable('behat');
52
        }
53
        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...
54
            throw new \Robo\Exception\TaskException(__CLASS__, "Neither composer nor phar installation of Behat found");
55
        }
56
    }
57
58
    /**
59
     * @return $this
60
     */
61
    public function stopOnFail()
62
    {
63
        $this->option('stop-on-failure');
64
        return $this;
65
    }
66
67
    /**
68
     * @return $this
69
     */
70
    public function noInteraction()
71
    {
72
        $this->option('no-interaction');
73
        return $this;
74
    }
75
76
    /**
77
     * @param $config_file
78
     *
79
     * @return $this
80
     */
81
    public function config($config_file)
82
    {
83
        $this->option('config', $config_file);
84
        return $this;
85
    }
86
87
    /**
88
     * @return $this
89
     */
90
    public function colors()
91
    {
92
        $this->option('colors');
93
        return $this;
94
    }
95
96
    /**
97
     * @return $this
98
     */
99
    public function noColors()
100
    {
101
        $this->option('no-colors');
102
        return $this;
103
    }
104
105
    /**
106
     * @param string $suite
107
     *
108
     * @return $this
109
     */
110
    public function suite($suite)
111
    {
112
        $this->option('suite', $suite);
113
        return $this;
114
    }
115
116
    /**
117
     * @param string $level
118
     *
119
     * @return $this
120
     */
121 View Code Duplication
    public function verbose($level = 'v')
122
    {
123
        if (!in_array($level, $this->verbose_levels)) {
124
            throw new \InvalidArgumentException('expected ' . implode(',', $this->verbose_levels));
125
        }
126
        $this->option('-' . $level);
127
        return $this;
128
    }
129
130
    /**
131
     * @param string $formater
132
     *
133
     * @return $this
134
     */
135 View Code Duplication
    public function format($formater)
136
    {
137
        if (!in_array($formater, $this->formaters)) {
138
            throw new \InvalidArgumentException('expected ' . implode(',', $this->formaters));
139
        }
140
        $this->option('format', $formater);
141
        return $this;
142
    }
143
144
    /**
145
     * Returns command that can be executed.
146
     * This method is used to pass generated command from one task to another.
147
     *
148
     * @return string
149
     */
150
    public function getCommand()
151
    {
152
        return $this->command . $this->arguments;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function run()
159
    {
160
        $this->printTaskInfo('Running behat {arguments}', ['arguments' => $this->arguments]);
161
        return $this->executeCommand($this->getCommand());
162
    }
163
}
164