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

PlainPHPUnitResultFormatter::addWarning()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 PlainPHPUnitResultFormatter extends PHPUnitResultFormatter
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 .= ", Risky: " . $this->getRiskyCount();
70 2
        $sb .= ", Warnings: " . $this->getWarningCount();
71 2
        $sb .= ", Failures: " . $this->getFailureCount();
72 2
        $sb .= ", Errors: " . $this->getErrorCount();
73 2
        $sb .= ", Incomplete: " . $this->getIncompleteCount();
74 2
        $sb .= ", Skipped: " . $this->getSkippedCount();
75 2
        $sb .= ", Time elapsed: " . sprintf('%0.5f', $this->getElapsedTime()) . " s\n";
76
77 2
        if ($this->out !== null) {
78 2
            $this->out->write($sb);
79 2
            $this->out->write($this->inner);
80
        }
81
82 2
        parent::endTestSuite($suite);
83 2
    }
84
85
    /**
86
     * @param PHPUnit\Framework\Test $test
87
     * @param Exception $e
88
     * @param float $time
89
     */
90
    public function addError(PHPUnit\Framework\Test $test, Throwable $e, float $time): void
91
    {
92
        parent::addError($test, $e, $time);
93
94
        $this->formatError("ERROR", $test, $e);
95
    }
96
97
    /**
98
     * @param PHPUnit\Framework\Test $test
99
     * @param PHPUnit\Framework\AssertionFailedError $e
100
     * @param float $time
101
     */
102 1
    public function addFailure(
103
        PHPUnit\Framework\Test $test,
104
        PHPUnit\Framework\AssertionFailedError $e,
105
        float $time
106
    ): void {
107 1
        parent::addFailure($test, $e, $time);
108 1
        $this->formatError("FAILED", $test, $e);
109 1
    }
110
111
    /**
112
     * @param PHPUnit\Framework\Test $test
113
     * @param PHPUnit\Framework\AssertionFailedError $e
114
     * @param float $time
115
     */
116 20
    public function addWarning(PHPUnit\Framework\Test $test, PHPUnit\Framework\Warning $e, float $time): void
117
    {
118 20
        parent::addWarning($test, $e, $time);
119 20
        $this->formatError("WARNING", $test, $e);
120 20
    }
121
122
    /**
123
     * @param PHPUnit\Framework\Test $test
124
     * @param PHPUnit\Framework\AssertionFailedError $e
125
     * @param float $time
126
     */
127
    public function addRiskyTest(PHPUnit\Framework\Test $test, Throwable $e, float $time): void
128
    {
129
        parent::addRiskyTest($test, $e, $time);
130
        $this->formatError("RISKY", $test, $e);
131
    }
132
133
    /**
134
     * @param PHPUnit\Framework\Test $test
135
     * @param Exception $e
136
     * @param float $time
137
     */
138
    public function addIncompleteTest(PHPUnit\Framework\Test $test, Throwable $e, float $time): void
139
    {
140
        parent::addIncompleteTest($test, $e, $time);
141
142
        $this->formatError("INCOMPLETE", $test);
143
    }
144
145
    /**
146
     * @param PHPUnit\Framework\Test $test
147
     * @param Exception $e
148
     * @param float $time
149
     */
150
    public function addSkippedTest(PHPUnit\Framework\Test $test, Throwable $e, float $time): void
151
    {
152
        parent::addSkippedTest($test, $e, $time);
153
        $this->formatError("SKIPPED", $test);
154
    }
155
156
    /**
157
     * @param $type
158
     * @param PHPUnit\Framework\Test $test
159
     * @param Exception $e
160
     */
161 21
    private function formatError($type, PHPUnit\Framework\Test $test, Exception $e = null)
162
    {
163 21
        if ($test != null) {
164 21
            $this->endTest($test, time());
165
        }
166
167 21
        $this->inner .= $test->getName() . " " . $type . "\n";
168
169 21
        if ($e !== null) {
170 21
            if ($e instanceof PHPUnit\Framework\ExceptionWrapper) {
171
                $this->inner .= $e->getPreviousWrapped() ? $e->getPreviousWrapped()->getMessage() : $e->getMessage() . "\n";
172
            } else {
173 21
                $this->inner .= $e->getMessage() . "\n";
174
            }
175
176 21
            if ($e instanceof ExpectationFailedException && $e->getComparisonFailure()) {
177
                $this->inner .= $e->getComparisonFailure()->getDiff();
178
            }
179
        }
180 21
    }
181
182 2
    public function endTestRun()
183
    {
184 2
        parent::endTestRun();
185
186 2
        if ($this->out != null) {
187 2
            $this->out->close();
188
        }
189 2
    }
190
}
191