Passed
Push — master ( eabf33...342b63 )
by Michiel
05:53
created

PHPUnitResultFormatter   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 303
Duplicated Lines 0 %

Test Coverage

Coverage 82.11%

Importance

Changes 0
Metric Value
eloc 65
dl 0
loc 303
ccs 78
cts 95
cp 0.8211
rs 10
c 0
b 0
f 0
wmc 27

26 Methods

Rating   Name   Duplication   Size   Complexity  
A processResult() 0 2 1
A addFailure() 0 6 1
A startTestRun() 0 10 1
A endTestRun() 0 2 1
A getElapsedTime() 0 7 2
A setOutput() 0 3 1
A getRunCount() 0 3 1
A addError() 0 3 1
A getErrorCount() 0 3 1
A endTest() 0 2 1
A addIncompleteTest() 0 3 1
A __construct() 0 3 1
A getMicrotime() 0 5 1
A getExtension() 0 3 1
A startTestSuite() 0 10 1
A getWarningCount() 0 3 1
A addRiskyTest() 0 3 1
A getRiskyCount() 0 3 1
A startTest() 0 3 1
A getPreferredOutfile() 0 3 1
A addSkippedTest() 0 3 1
A getIncompleteCount() 0 3 1
A addWarning() 0 3 1
A getFailureCount() 0 3 1
A getSkippedCount() 0 3 1
A endTestSuite() 0 24 1
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
use Phing\Io\Writer;
21
use Phing\Project;
22
23
/**
24
 * This abstract class describes classes that format the results of a PHPUnit testrun.
25
 *
26
 * @author  Siad Ardroumli <[email protected]>
27
 * @package phing.tasks.ext.phpunit.formatter
28
 */
29
abstract class PHPUnitResultFormatter implements PHPUnit\Framework\TestListener
0 ignored issues
show
Deprecated Code introduced by
The interface PHPUnit\Framework\TestListener has been deprecated: Use the `TestHook` interfaces instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

29
abstract class PHPUnitResultFormatter implements /** @scrutinizer ignore-deprecated */ PHPUnit\Framework\TestListener

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

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

Loading history...
30
{
31
    protected $out;
32
33
    /** @var Project */
34
    protected $project;
35
36
    /**
37
     * @var array
38
     */
39
    private $timers = [];
40
41
    /**
42
     * @var array
43
     */
44
    private $runCounts = [];
45
46
    /**
47
     * @var array
48
     */
49
    private $failureCounts = [];
50
51
    /**
52
     * @var array
53
     */
54
    private $errorCounts = [];
55
56
    /**
57
     * @var array
58
     */
59
    private $incompleteCounts = [];
60
61
    /**
62
     * @var array
63
     */
64
    private $skipCounts = [];
65
66
    /**
67
     * @var array
68
     */
69
    private $warningCounts = [];
70
    private $riskyCounts = [];
71
72
    /**
73
     * Constructor
74
     *
75
     * @param PHPUnitTask $parentTask Calling Task
76
     */
77 2
    public function __construct(PHPUnitTask $parentTask)
78
    {
79 2
        $this->project = $parentTask->getProject();
80 2
    }
81
82
    /**
83
     * Sets the writer the formatter is supposed to write its results to.
84
     *
85
     * @param Writer $out
86
     */
87 2
    public function setOutput(Writer $out)
88
    {
89 2
        $this->out = $out;
90 2
    }
91
92
    /**
93
     * Returns the extension used for this formatter
94
     *
95
     * @return string the extension
96
     */
97
    public function getExtension()
98
    {
99
        return '';
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    public function getPreferredOutfile()
106
    {
107
        return '';
108
    }
109
110
    /**
111
     * @param PHPUnit\Framework\TestResult $result
112
     */
113 2
    public function processResult(PHPUnit\Framework\TestResult $result)
0 ignored issues
show
Unused Code introduced by
The parameter $result is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

113
    public function processResult(/** @scrutinizer ignore-unused */ PHPUnit\Framework\TestResult $result)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
114
    {
115 2
    }
116
117 2
    public function startTestRun()
118
    {
119 2
        $this->timers = [$this->getMicrotime()];
120 2
        $this->runCounts = [0];
121 2
        $this->failureCounts = [0];
122 2
        $this->errorCounts = [0];
123 2
        $this->warningCounts = [0];
124 2
        $this->incompleteCounts = [0];
125 2
        $this->skipCounts = [0];
126 2
        $this->riskyCounts = [0];
127 2
    }
128
129 2
    public function endTestRun()
130
    {
131 2
    }
132
133
    /**
134
     * @param PHPUnit\Framework\TestSuite $suite
135
     */
136 2
    public function startTestSuite(PHPUnit\Framework\TestSuite $suite): void
137
    {
138 2
        $this->timers[] = $this->getMicrotime();
139 2
        $this->runCounts[] = 0;
140 2
        $this->failureCounts[] = 0;
141 2
        $this->errorCounts[] = 0;
142 2
        $this->incompleteCounts[] = 0;
143 2
        $this->skipCounts[] = 0;
144 2
        $this->warningCounts[] = 0;
145 2
        $this->riskyCounts[] = 0;
146 2
    }
147
148
    /**
149
     * @param PHPUnit\Framework\TestSuite $suite
150
     */
151 2
    public function endTestSuite(PHPUnit\Framework\TestSuite $suite): void
152
    {
153 2
        $lastRunCount = array_pop($this->runCounts);
154 2
        $this->runCounts[count($this->runCounts) - 1] += $lastRunCount;
155
156 2
        $lastFailureCount = array_pop($this->failureCounts);
157 2
        $this->failureCounts[count($this->failureCounts) - 1] += $lastFailureCount;
158
159 2
        $lastErrorCount = array_pop($this->errorCounts);
160 2
        $this->errorCounts[count($this->errorCounts) - 1] += $lastErrorCount;
161
162 2
        $lastIncompleteCount = array_pop($this->incompleteCounts);
163 2
        $this->incompleteCounts[count($this->incompleteCounts) - 1] += $lastIncompleteCount;
164
165 2
        $lastSkipCount = array_pop($this->skipCounts);
166 2
        $this->skipCounts[count($this->skipCounts) - 1] += $lastSkipCount;
167
168 2
        $lastWarningCount = array_pop($this->warningCounts);
169 2
        $this->warningCounts[count($this->warningCounts) - 1] += $lastWarningCount;
170
171 2
        $lastRiskyCount = array_pop($this->riskyCounts);
172 2
        $this->riskyCounts[count($this->riskyCounts) - 1] += $lastRiskyCount;
173
174 2
        array_pop($this->timers);
175 2
    }
176
177
    /**
178
     * @param PHPUnit\Framework\Test $test
179
     */
180 2
    public function startTest(PHPUnit\Framework\Test $test): void
181
    {
182 2
        $this->runCounts[count($this->runCounts) - 1]++;
183 2
    }
184
185
    /**
186
     * @param PHPUnit\Framework\Test $test
187
     * @param float $time
188
     */
189 22
    public function endTest(PHPUnit\Framework\Test $test, float $time): void
190
    {
191 22
    }
192
193
    /**
194
     * @param PHPUnit\Framework\Test $test
195
     * @param Exception $e
196
     * @param float $time
197
     */
198
    public function addError(PHPUnit\Framework\Test $test, Throwable $e, float $time): void
199
    {
200
        $this->errorCounts[count($this->errorCounts) - 1]++;
201
    }
202
203
    /**
204
     * @param PHPUnit\Framework\Test $test
205
     * @param PHPUnit\Framework\AssertionFailedError $e
206
     * @param float $time
207
     */
208 1
    public function addFailure(
209
        PHPUnit\Framework\Test $test,
210
        PHPUnit\Framework\AssertionFailedError $e,
211
        float $time
212
    ): void {
213 1
        $this->failureCounts[count($this->failureCounts) - 1]++;
214 1
    }
215
216
    /**
217
     * @param PHPUnit\Framework\Test $test
218
     * @param PHPUnit\Framework\Warning $e
219
     * @param float $time
220
     */
221 20
    public function addWarning(PHPUnit\Framework\Test $test, \PHPUnit\Framework\Warning $e, float $time): void
222
    {
223 20
        $this->warningCounts[count($this->warningCounts) - 1]++;
224 20
    }
225
226
    /**
227
     * @param PHPUnit\Framework\Test $test
228
     * @param Exception $e
229
     * @param float $time
230
     */
231
    public function addIncompleteTest(PHPUnit\Framework\Test $test, Throwable $e, float $time): void
232
    {
233
        $this->incompleteCounts[count($this->incompleteCounts) - 1]++;
234
    }
235
236
    /**
237
     * @param PHPUnit\Framework\Test $test
238
     * @param Exception $e
239
     * @param float $time
240
     */
241
    public function addSkippedTest(PHPUnit\Framework\Test $test, Throwable $e, float $time): void
242
    {
243
        $this->skipCounts[count($this->skipCounts) - 1]++;
244
    }
245
246
    /**
247
     * @param PHPUnit\Framework\Test $test
248
     * @param Exception $e
249
     * @param float $time
250
     */
251
    public function addRiskyTest(PHPUnit\Framework\Test $test, Throwable $e, float $time): void
252
    {
253
        $this->riskyCounts[count($this->riskyCounts) - 1]++;
254
    }
255
256
    /**
257
     * @return mixed
258
     */
259 2
    public function getRunCount()
260
    {
261 2
        return end($this->runCounts);
262
    }
263
264
    /**
265
     * @return mixed
266
     */
267 2
    public function getFailureCount()
268
    {
269 2
        return end($this->failureCounts);
270
    }
271
272
    /**
273
     * @return mixed
274
     */
275 2
    public function getWarningCount()
276
    {
277 2
        return end($this->warningCounts);
278
    }
279
280
    /**
281
     * @return mixed
282
     */
283 2
    public function getRiskyCount()
284
    {
285 2
        return end($this->riskyCounts);
286
    }
287
288
    /**
289
     * @return mixed
290
     */
291 2
    public function getErrorCount()
292
    {
293 2
        return end($this->errorCounts);
294
    }
295
296
    /**
297
     * @return mixed
298
     */
299 2
    public function getIncompleteCount()
300
    {
301 2
        return end($this->incompleteCounts);
302
    }
303
304
    /**
305
     * @return mixed
306
     */
307 2
    public function getSkippedCount()
308
    {
309 2
        return end($this->skipCounts);
310
    }
311
312
    /**
313
     * @return float|int
314
     */
315 2
    public function getElapsedTime()
316
    {
317 2
        if (end($this->timers)) {
318 2
            return $this->getMicrotime() - end($this->timers);
319
        }
320
321
        return 0;
322
    }
323
324
    /**
325
     * @return float
326
     */
327 2
    private function getMicrotime()
328
    {
329 2
        [$usec, $sec] = explode(' ', microtime());
330
331 2
        return (float) $usec + (float) $sec;
332
    }
333
}
334