Completed
Pull Request — master (#893)
by Greg
05:57
created

src/Task/Testing/PHPUnit.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
 * Runs PHPUnit tests
10
 *
11
 * ``` php
12
 * <?php
13
 * $this->taskPHPUnit()
14
 *  ->group('core')
15
 *  ->bootstrap('test/bootstrap.php')
16
 *  ->run()
17
 *
18
 * ?>
19
 * ```
20
 */
21
class PHPUnit extends BaseTask implements CommandInterface, PrintedInterface
22
{
23
    use \Robo\Common\ExecOneCommand;
24
25
    /**
26
     * @var string
27
     */
28
    protected $command;
29
30
    /**
31
     * Directory of test files or single test file to run. Appended to
32
     * the command and arguments.
33
     *
34
     * @var string
35
     */
36
    protected $files = '';
37
38
    /**
39
     * PHPUnit constructor.
40
     *
41
     * @param null|string $pathToPhpUnit
42
     *
43
     * @throws \Robo\Exception\TaskException
44
     */
45 View Code Duplication
    public function __construct($pathToPhpUnit = null)
46
    {
47
        $this->command = $pathToPhpUnit;
48
        if (!$this->command) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->command of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null 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
            $this->command = $this->findExecutablePhar('phpunit');
50
        }
51
        if (!$this->command) {
52
            throw new \Robo\Exception\TaskException(__CLASS__, "Neither local phpunit nor global composer installation not found");
53
        }
54
    }
55
56
    /**
57
     * @param string $filter
58
     *
59
     * @return $this
60
     */
61
    public function filter($filter)
62
    {
63
        $this->option('filter', $filter);
64
        return $this;
65
    }
66
67
    /**
68
     * @param string $group
69
     *
70
     * @return $this
71
     */
72
    public function group($group)
73
    {
74
        $this->option("group", $group);
75
        return $this;
76
    }
77
78
    /**
79
     * @param string $group
80
     *
81
     * @return $this
82
     */
83
    public function excludeGroup($group)
84
    {
85
        $this->option("exclude-group", $group);
86
        return $this;
87
    }
88
89
    /**
90
     * adds `log-json` option to runner
91
     *
92
     * @param string $file
93
     *
94
     * @return $this
95
     */
96
    public function json($file = null)
97
    {
98
        $this->option("log-json", $file);
99
        return $this;
100
    }
101
102
    /**
103
     * adds `log-junit` option
104
     *
105
     * @param string $file
106
     *
107
     * @return $this
108
     */
109
    public function xml($file = null)
110
    {
111
        $this->option("log-junit", $file);
112
        return $this;
113
    }
114
115
    /**
116
     * @param string $file
117
     *
118
     * @return $this
119
     */
120
    public function tap($file = "")
121
    {
122
        $this->option("log-tap", $file);
123
        return $this;
124
    }
125
126
    /**
127
     * @param string $file
128
     *
129
     * @return $this
130
     */
131
    public function bootstrap($file)
132
    {
133
        $this->option("bootstrap", $file);
134
        return $this;
135
    }
136
137
    /**
138
     * @param string $file
139
     *
140
     * @return $this
141
     */
142
    public function configFile($file)
143
    {
144
        $this->option('-c', $file);
145
        return $this;
146
    }
147
148
    /**
149
     * @return $this
150
     */
151
    public function debug()
152
    {
153
        $this->option("debug");
154
        return $this;
155
    }
156
157
    /**
158
     * Directory of test files or single test file to run.
159
     *
160
     * @param string $files
161
     *   A single test file or a directory containing test files.
162
     *
163
     * @return $this
164
     *
165
     * @throws \Robo\Exception\TaskException
166
     *
167
     * @deprecated Use file() or dir() method instead
168
     */
169
    public function files($files)
170
    {
171
        if (!empty($this->files) || is_array($files)) {
172
            throw new \Robo\Exception\TaskException(__CLASS__, "Only one file or directory may be provided.");
173
        }
174
        $this->files = ' ' . $files;
175
176
        return $this;
177
    }
178
179
    /**
180
     * Test the provided file.
181
     *
182
     * @param string $file
183
     *   Path to file to test.
184
     *
185
     * @return $this
186
     */
187
    public function file($file)
188
    {
189
        return $this->files($file);
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195
    public function getCommand()
196
    {
197
        return $this->command . $this->arguments . $this->files;
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203
    public function run()
204
    {
205
        $this->printTaskInfo('Running PHPUnit {arguments}', ['arguments' => $this->arguments]);
206
        return $this->executeCommand($this->getCommand());
207
    }
208
}
209