Passed
Push — master ( 87dcc6...8fedad )
by Michiel
13:32
created

PlainPHPUnitResultFormatter7::formatError()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.392

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 10
c 2
b 0
f 0
nc 14
nop 3
dl 0
loc 17
ccs 8
cts 10
cp 0.8
crap 7.392
rs 8.8333
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 PHPUnit\Framework\ExpectationFailedException;
21
22
/**
23
 * Prints plain text output of the test to a specified Writer.
24
 *
25
 * @author  Siad Ardroumli <[email protected]>
26
 * @package phing.tasks.ext.phpunit.formatter
27
 */
28
class PlainPHPUnitResultFormatter7 extends PHPUnitResultFormatter7
29
{
30
    private $inner = "";
31
32
    /**
33
     * @return string
34
     */
35
    public function getExtension()
36
    {
37
        return ".txt";
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function getPreferredOutfile()
44
    {
45
        return "testresults";
46
    }
47
48
    /**
49
     * @param PHPUnit\Framework\TestSuite $suite
50
     */
51 2
    public function startTestSuite(PHPUnit\Framework\TestSuite $suite): void
52
    {
53 2
        parent::startTestSuite($suite);
54
55 2
        $this->inner = "";
56 2
    }
57
58
    /**
59
     * @param PHPUnit\Framework\TestSuite $suite
60
     */
61 2
    public function endTestSuite(PHPUnit\Framework\TestSuite $suite): void
62
    {
63 2
        if ($suite->getName() === 'AllTests') {
64 2
            return;
65
        }
66
67 2
        $sb = "Testsuite: " . $suite->getName() . "\n";
68 2
        $sb .= "Tests run: " . $this->getRunCount();
69 2
        $sb .= ", Warnings: " . $this->getWarningCount();
70 2
        $sb .= ", Failures: " . $this->getFailureCount();
71 2
        $sb .= ", Errors: " . $this->getErrorCount();
72 2
        $sb .= ", Incomplete: " . $this->getIncompleteCount();
73 2
        $sb .= ", Skipped: " . $this->getSkippedCount();
74 2
        $sb .= ", Time elapsed: " . sprintf('%0.5f', $this->getElapsedTime()) . " s\n";
75
76 2
        if ($this->out !== null) {
77 2
            $this->out->write($sb);
78 2
            $this->out->write($this->inner);
79
        }
80
81 2
        parent::endTestSuite($suite);
82 2
    }
83
84
    /**
85
     * @param PHPUnit\Framework\Test $test
86
     * @param Exception $e
87
     * @param float $time
88
     */
89
    public function addError(PHPUnit\Framework\Test $test, Throwable $e, float $time): void
90
    {
91
        parent::addError($test, $e, $time);
92
93
        $this->formatError("ERROR", $test, $e);
94
    }
95
96
    /**
97
     * @param PHPUnit\Framework\Test $test
98
     * @param PHPUnit\Framework\AssertionFailedError $e
99
     * @param float $time
100
     */
101 1
    public function addFailure(
102
        PHPUnit\Framework\Test $test,
103
        PHPUnit\Framework\AssertionFailedError $e,
104
        float $time
105
    ): void {
106 1
        parent::addFailure($test, $e, $time);
107 1
        $this->formatError("FAILED", $test, $e);
108 1
    }
109
110
    /**
111
     * @param PHPUnit\Framework\Test $test
112
     * @param PHPUnit\Framework\AssertionFailedError $e
113
     * @param float $time
114
     */
115
    public function addWarning(PHPUnit\Framework\Test $test, PHPUnit\Framework\Warning $e, float $time): void
116
    {
117
        parent::addWarning($test, $e, $time);
118
        $this->formatError("WARNING", $test, $e);
119
    }
120
121
    /**
122
     * @param PHPUnit\Framework\Test $test
123
     * @param Exception $e
124
     * @param float $time
125
     */
126
    public function addIncompleteTest(PHPUnit\Framework\Test $test, Throwable $e, float $time): void
127
    {
128
        parent::addIncompleteTest($test, $e, $time);
129
130
        $this->formatError("INCOMPLETE", $test);
131
    }
132
133
    /**
134
     * @param PHPUnit\Framework\Test $test
135
     * @param Exception $e
136
     * @param float $time
137
     */
138
    public function addSkippedTest(PHPUnit\Framework\Test $test, Throwable $e, float $time): void
139
    {
140
        parent::addSkippedTest($test, $e, $time);
141
        $this->formatError("SKIPPED", $test);
142
    }
143
144
    /**
145
     * @param $type
146
     * @param PHPUnit\Framework\Test $test
147
     * @param Exception $e
148
     */
149 1
    private function formatError($type, PHPUnit\Framework\Test $test, Exception $e = null)
150
    {
151 1
        if ($test != null) {
152 1
            $this->endTest($test, time());
153
        }
154
155 1
        $this->inner .= $test->getName() . " " . $type . "\n";
0 ignored issues
show
Bug introduced by
The method getName() does not exist on PHPUnit\Framework\Test. It seems like you code against a sub-type of said class. However, the method does not exist in DoubleTestCase or NotSelfDescribingTest. Are you sure you never get one of those? ( Ignorable by Annotation )

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

155
        $this->inner .= $test->/** @scrutinizer ignore-call */ getName() . " " . $type . "\n";
Loading history...
156
157 1
        if ($e !== null) {
158 1
            if ($e instanceof PHPUnit\Framework\ExceptionWrapper) {
159
                $this->inner .= $e->getPreviousWrapped() ? $e->getPreviousWrapped()->getMessage() : $e->getMessage() . "\n";
160
            } else {
161 1
                $this->inner .= $e->getMessage() . "\n";
162
            }
163
164 1
            if ($e instanceof ExpectationFailedException && $e->getComparisonFailure()) {
165
                $this->inner .= $e->getComparisonFailure()->getDiff();
166
            }
167
        }
168 1
    }
169
170 2
    public function endTestRun()
171
    {
172 2
        parent::endTestRun();
173
174 2
        if ($this->out != null) {
175 2
            $this->out->close();
176
        }
177 2
    }
178
}
179