776bad97ec7744ede57e0d703b4ec48d07d70b86
[yaffs-website] / vendor / phpunit / phpunit / src / Util / Log / JSON.php
1 <?php
2 /*
3  * This file is part of PHPUnit.
4  *
5  * (c) Sebastian Bergmann <sebastian@phpunit.de>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * A TestListener that generates JSON messages.
13  *
14  * @since Class available since Release 3.0.0
15  */
16 class PHPUnit_Util_Log_JSON extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
17 {
18     /**
19      * @var string
20      */
21     protected $currentTestSuiteName = '';
22
23     /**
24      * @var string
25      */
26     protected $currentTestName = '';
27
28     /**
29      * @var bool
30      */
31     protected $currentTestPass = true;
32
33     /**
34      * An error occurred.
35      *
36      * @param PHPUnit_Framework_Test $test
37      * @param Exception              $e
38      * @param float                  $time
39      */
40     public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
41     {
42         $this->writeCase(
43             'error',
44             $time,
45             PHPUnit_Util_Filter::getFilteredStacktrace($e, false),
46             $e->getMessage(),
47             $test
48         );
49
50         $this->currentTestPass = false;
51     }
52
53     /**
54      * A failure occurred.
55      *
56      * @param PHPUnit_Framework_Test                 $test
57      * @param PHPUnit_Framework_AssertionFailedError $e
58      * @param float                                  $time
59      */
60     public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
61     {
62         $this->writeCase(
63             'fail',
64             $time,
65             PHPUnit_Util_Filter::getFilteredStacktrace($e, false),
66             $e->getMessage(),
67             $test
68         );
69
70         $this->currentTestPass = false;
71     }
72
73     /**
74      * Incomplete test.
75      *
76      * @param PHPUnit_Framework_Test $test
77      * @param Exception              $e
78      * @param float                  $time
79      */
80     public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
81     {
82         $this->writeCase(
83             'error',
84             $time,
85             PHPUnit_Util_Filter::getFilteredStacktrace($e, false),
86             'Incomplete Test: ' . $e->getMessage(),
87             $test
88         );
89
90         $this->currentTestPass = false;
91     }
92
93     /**
94      * Risky test.
95      *
96      * @param PHPUnit_Framework_Test $test
97      * @param Exception              $e
98      * @param float                  $time
99      *
100      * @since  Method available since Release 4.0.0
101      */
102     public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
103     {
104         $this->writeCase(
105             'error',
106             $time,
107             PHPUnit_Util_Filter::getFilteredStacktrace($e, false),
108             'Risky Test: ' . $e->getMessage(),
109             $test
110         );
111
112         $this->currentTestPass = false;
113     }
114
115     /**
116      * Skipped test.
117      *
118      * @param PHPUnit_Framework_Test $test
119      * @param Exception              $e
120      * @param float                  $time
121      */
122     public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
123     {
124         $this->writeCase(
125             'error',
126             $time,
127             PHPUnit_Util_Filter::getFilteredStacktrace($e, false),
128             'Skipped Test: ' . $e->getMessage(),
129             $test
130         );
131
132         $this->currentTestPass = false;
133     }
134
135     /**
136      * A testsuite started.
137      *
138      * @param PHPUnit_Framework_TestSuite $suite
139      */
140     public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
141     {
142         $this->currentTestSuiteName = $suite->getName();
143         $this->currentTestName      = '';
144
145         $this->write(
146             array(
147             'event' => 'suiteStart',
148             'suite' => $this->currentTestSuiteName,
149             'tests' => count($suite)
150             )
151         );
152     }
153
154     /**
155      * A testsuite ended.
156      *
157      * @param PHPUnit_Framework_TestSuite $suite
158      */
159     public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
160     {
161         $this->currentTestSuiteName = '';
162         $this->currentTestName      = '';
163     }
164
165     /**
166      * A test started.
167      *
168      * @param PHPUnit_Framework_Test $test
169      */
170     public function startTest(PHPUnit_Framework_Test $test)
171     {
172         $this->currentTestName = PHPUnit_Util_Test::describe($test);
173         $this->currentTestPass = true;
174
175         $this->write(
176             array(
177             'event' => 'testStart',
178             'suite' => $this->currentTestSuiteName,
179             'test'  => $this->currentTestName
180             )
181         );
182     }
183
184     /**
185      * A test ended.
186      *
187      * @param PHPUnit_Framework_Test $test
188      * @param float                  $time
189      */
190     public function endTest(PHPUnit_Framework_Test $test, $time)
191     {
192         if ($this->currentTestPass) {
193             $this->writeCase('pass', $time, array(), '', $test);
194         }
195     }
196
197     /**
198      * @param string                          $status
199      * @param float                           $time
200      * @param array                           $trace
201      * @param string                          $message
202      * @param PHPUnit_Framework_TestCase|null $test
203      */
204     protected function writeCase($status, $time, array $trace = array(), $message = '', $test = null)
205     {
206         $output = '';
207         // take care of TestSuite producing error (e.g. by running into exception) as TestSuite doesn't have hasOutput
208         if ($test !== null && method_exists($test, 'hasOutput') && $test->hasOutput()) {
209             $output = $test->getActualOutput();
210         }
211         $this->write(
212             array(
213             'event'   => 'test',
214             'suite'   => $this->currentTestSuiteName,
215             'test'    => $this->currentTestName,
216             'status'  => $status,
217             'time'    => $time,
218             'trace'   => $trace,
219             'message' => PHPUnit_Util_String::convertToUtf8($message),
220             'output'  => $output,
221             )
222         );
223     }
224
225     /**
226      * @param string $buffer
227      */
228     public function write($buffer)
229     {
230         array_walk_recursive($buffer, function (&$input) {
231             if (is_string($input)) {
232                 $input = PHPUnit_Util_String::convertToUtf8($input);
233             }
234         });
235
236         $flags = 0;
237
238         if (defined('JSON_PRETTY_PRINT')) {
239             $flags |= JSON_PRETTY_PRINT;
240         }
241
242         parent::write(json_encode($buffer, $flags));
243     }
244 }