Completed
Push — master ( 540fc7...19c7a1 )
by Greg
02:50
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 View Code Duplication
    public function __construct($pathToPhpUnit = null)
39
    {
40
        $this->command = $pathToPhpUnit;
41
        if (!$this->command) {
42
            $this->command = $this->findExecutablePhar('phpunit');
43
        }
44
        if (!$this->command) {
45
            throw new \Robo\Exception\TaskException(__CLASS__, "Neither local phpunit nor global composer installation not found");
46
        }
47
    }
48
49
    /**
50
     * @param string $filter
51
     *
52
     * @return $this
53
     */
54
    public function filter($filter)
55
    {
56
        $this->option('filter', $filter);
57
        return $this;
58
    }
59
60
    /**
61
     * @param string $group
62
     *
63
     * @return $this
64
     */
65
    public function group($group)
66
    {
67
        $this->option("group", $group);
68
        return $this;
69
    }
70
71
    /**
72
     * @param string $group
73
     *
74
     * @return $this
75
     */
76
    public function excludeGroup($group)
77
    {
78
        $this->option("exclude-group", $group);
79
        return $this;
80
    }
81
82
    /**
83
     * adds `log-json` option to runner
84
     *
85
     * @param string $file
86
     *
87
     * @return $this
88
     */
89
    public function json($file = null)
90
    {
91
        $this->option("log-json", $file);
92
        return $this;
93
    }
94
95
    /**
96
     * adds `log-junit` option
97
     *
98
     * @param string $file
99
     *
100
     * @return $this
101
     */
102
    public function xml($file = null)
103
    {
104
        $this->option("log-junit", $file);
105
        return $this;
106
    }
107
108
    /**
109
     * @param string $file
110
     *
111
     * @return $this
112
     */
113
    public function tap($file = "")
114
    {
115
        $this->option("log-tap", $file);
116
        return $this;
117
    }
118
119
    /**
120
     * @param string $file
121
     *
122
     * @return $this
123
     */
124
    public function bootstrap($file)
125
    {
126
        $this->option("bootstrap", $file);
127
        return $this;
128
    }
129
130
    /**
131
     * @param string $file
132
     *
133
     * @return $this
134
     */
135
    public function configFile($file)
136
    {
137
        $this->option('-c', $file);
138
        return $this;
139
    }
140
141
    /**
142
     * @return $this
143
     */
144
    public function debug()
145
    {
146
        $this->option("debug");
147
        return $this;
148
    }
149
150
    /**
151
     * Directory of test files or single test file to run.
152
     *
153
     * @param string $files A single test file or a directory containing test files.
154
     *
155
     * @return $this
156
     *
157
     * @throws \Robo\Exception\TaskException
158
     *
159
     * @deprecated Use file() or dir() method instead
160
     */
161
    public function files($files)
162
    {
163
        if (!empty($this->files) || is_array($files)) {
164
            throw new \Robo\Exception\TaskException(__CLASS__, "Only one file or directory may be provided.");
165
        }
166
        $this->files = ' ' . $files;
167
168
        return $this;
169
    }
170
171
    /**
172
     * Test the provided file.
173
     *
174
     * @param string $file path to file to test
175
     *
176
     * @return $this
177
     */
178
    public function file($file)
179
    {
180
        return $this->files($file);
0 ignored issues
show
Deprecated Code introduced by
The method Robo\Task\Testing\PHPUnit::files() has been deprecated with message: Use file() or dir() method instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function getCommand()
187
    {
188
        return $this->command . $this->arguments . $this->files;
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function run()
195
    {
196
        $this->printTaskInfo('Running PHPUnit {arguments}', ['arguments' => $this->arguments]);
197
        return $this->executeCommand($this->getCommand());
198
    }
199
}
200