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 <siad.ardroumli@gmail.com>
26
 * @package phing.tasks.ext.phpunit.formatter
27
 */
28
class PlainPHPUnitResultFormatter7 extends PHPUnitResultFormatter7
29
{
30
    private $inner = "";
31

32
    /**
33
     * @return string
34
     */
35 0
    public function getExtension()
36
    {
37 0
        return ".txt";
38
    }
39

40
    /**
41
     * @return string
42
     */
43 0
    public function getPreferredOutfile()
44
    {
45 0
        return "testresults";
46
    }
47

48
    /**
49
     * @param PHPUnit\Framework\TestSuite $suite
50
     */
51 1
    public function startTestSuite(PHPUnit\Framework\TestSuite $suite): void
52
    {
53 1
        parent::startTestSuite($suite);
54

55 1
        $this->inner = "";
56
    }
57

58
    /**
59
     * @param PHPUnit\Framework\TestSuite $suite
60
     */
61 1
    public function endTestSuite(PHPUnit\Framework\TestSuite $suite): void
62
    {
63 1
        if ($suite->getName() === 'AllTests') {
64 1
            return;
65
        }
66

67 1
        $sb = "Testsuite: " . $suite->getName() . "\n";
68 1
        $sb .= "Tests run: " . $this->getRunCount();
69 1
        $sb .= ", Warnings: " . $this->getWarningCount();
70 1
        $sb .= ", Failures: " . $this->getFailureCount();
71 1
        $sb .= ", Errors: " . $this->getErrorCount();
72 1
        $sb .= ", Incomplete: " . $this->getIncompleteCount();
73 1
        $sb .= ", Skipped: " . $this->getSkippedCount();
74 1
        $sb .= ", Time elapsed: " . sprintf('%0.5f', $this->getElapsedTime()) . " s\n";
75

76 1
        if ($this->out !== null) {
77 1
            $this->out->write($sb);
78 1
            $this->out->write($this->inner);
79
        }
80

81 1
        parent::endTestSuite($suite);
82
    }
83

84
    /**
85
     * @param PHPUnit\Framework\Test $test
86
     * @param Exception $e
87
     * @param float $time
88
     */
89 0
    public function addError(PHPUnit\Framework\Test $test, Throwable $e, float $time): void
90
    {
91 0
        parent::addError($test, $e, $time);
92

93 0
        $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
    }
109

110
    /**
111
     * @param PHPUnit\Framework\Test $test
112
     * @param PHPUnit\Framework\AssertionFailedError $e
113
     * @param float $time
114
     */
115 0
    public function addWarning(PHPUnit\Framework\Test $test, PHPUnit\Framework\Warning $e, float $time): void
116
    {
117 0
        parent::addWarning($test, $e, $time);
118 0
        $this->formatError("WARNING", $test, $e);
119
    }
120

121
    /**
122
     * @param PHPUnit\Framework\Test $test
123
     * @param Exception $e
124
     * @param float $time
125
     */
126 0
    public function addIncompleteTest(PHPUnit\Framework\Test $test, Throwable $e, float $time): void
127
    {
128 0
        parent::addIncompleteTest($test, $e, $time);
129

130 0
        $this->formatError("INCOMPLETE", $test);
131
    }
132

133
    /**
134
     * @param PHPUnit\Framework\Test $test
135
     * @param Exception $e
136
     * @param float $time
137
     */
138 0
    public function addSkippedTest(PHPUnit\Framework\Test $test, Throwable $e, float $time): void
139
    {
140 0
        parent::addSkippedTest($test, $e, $time);
141 0
        $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";
156

157 1
        if ($e !== null) {
158 1
            if ($e instanceof PHPUnit\Framework\ExceptionWrapper) {
159 0
                $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 0
                $this->inner .= $e->getComparisonFailure()->getDiff();
166
            }
167
        }
168
    }
169

170 1
    public function endTestRun()
171
    {
172 1
        parent::endTestRun();
173

174 1
        if ($this->out != null) {
175 1
            $this->out->close();
176
        }
177
    }
178
}

Read our documentation on viewing source code .

Loading