Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / vendor / phpunit / phpunit / src / Util / Log / TAP.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 a logfile of the
13  * test execution using the Test Anything Protocol (TAP).
14  *
15  * @since Class available since Release 3.0.0
16  */
17 class PHPUnit_Util_Log_TAP extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
18 {
19     /**
20      * @var int
21      */
22     protected $testNumber = 0;
23
24     /**
25      * @var int
26      */
27     protected $testSuiteLevel = 0;
28
29     /**
30      * @var bool
31      */
32     protected $testSuccessful = true;
33
34     /**
35      * Constructor.
36      *
37      * @param mixed $out
38      *
39      * @throws PHPUnit_Framework_Exception
40      *
41      * @since  Method available since Release 3.3.4
42      */
43     public function __construct($out = null)
44     {
45         parent::__construct($out);
46         $this->write("TAP version 13\n");
47     }
48
49     /**
50      * An error occurred.
51      *
52      * @param PHPUnit_Framework_Test $test
53      * @param Exception              $e
54      * @param float                  $time
55      */
56     public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
57     {
58         $this->writeNotOk($test, 'Error');
59     }
60
61     /**
62      * A failure occurred.
63      *
64      * @param PHPUnit_Framework_Test                 $test
65      * @param PHPUnit_Framework_AssertionFailedError $e
66      * @param float                                  $time
67      */
68     public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
69     {
70         $this->writeNotOk($test, 'Failure');
71
72         $message = explode(
73             "\n",
74             PHPUnit_Framework_TestFailure::exceptionToString($e)
75         );
76
77         $diagnostic = array(
78           'message'  => $message[0],
79           'severity' => 'fail'
80         );
81
82         if ($e instanceof PHPUnit_Framework_ExpectationFailedException) {
83             $cf = $e->getComparisonFailure();
84
85             if ($cf !== null) {
86                 $diagnostic['data'] = array(
87                   'got'      => $cf->getActual(),
88                   'expected' => $cf->getExpected()
89                 );
90             }
91         }
92
93         $yaml = new Symfony\Component\Yaml\Dumper;
94
95         $this->write(
96             sprintf(
97                 "  ---\n%s  ...\n",
98                 $yaml->dump($diagnostic, 2, 2)
99             )
100         );
101     }
102
103     /**
104      * Incomplete test.
105      *
106      * @param PHPUnit_Framework_Test $test
107      * @param Exception              $e
108      * @param float                  $time
109      */
110     public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
111     {
112         $this->writeNotOk($test, '', 'TODO Incomplete Test');
113     }
114
115     /**
116      * Risky test.
117      *
118      * @param PHPUnit_Framework_Test $test
119      * @param Exception              $e
120      * @param float                  $time
121      *
122      * @since  Method available since Release 4.0.0
123      */
124     public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
125     {
126         $this->write(
127             sprintf(
128                 "ok %d - # RISKY%s\n",
129                 $this->testNumber,
130                 $e->getMessage() != '' ? ' ' . $e->getMessage() : ''
131             )
132         );
133
134         $this->testSuccessful = false;
135     }
136
137     /**
138      * Skipped test.
139      *
140      * @param PHPUnit_Framework_Test $test
141      * @param Exception              $e
142      * @param float                  $time
143      *
144      * @since  Method available since Release 3.0.0
145      */
146     public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
147     {
148         $this->write(
149             sprintf(
150                 "ok %d - # SKIP%s\n",
151                 $this->testNumber,
152                 $e->getMessage() != '' ? ' ' . $e->getMessage() : ''
153             )
154         );
155
156         $this->testSuccessful = false;
157     }
158
159     /**
160      * A testsuite started.
161      *
162      * @param PHPUnit_Framework_TestSuite $suite
163      */
164     public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
165     {
166         $this->testSuiteLevel++;
167     }
168
169     /**
170      * A testsuite ended.
171      *
172      * @param PHPUnit_Framework_TestSuite $suite
173      */
174     public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
175     {
176         $this->testSuiteLevel--;
177
178         if ($this->testSuiteLevel == 0) {
179             $this->write(sprintf("1..%d\n", $this->testNumber));
180         }
181     }
182
183     /**
184      * A test started.
185      *
186      * @param PHPUnit_Framework_Test $test
187      */
188     public function startTest(PHPUnit_Framework_Test $test)
189     {
190         $this->testNumber++;
191         $this->testSuccessful = true;
192     }
193
194     /**
195      * A test ended.
196      *
197      * @param PHPUnit_Framework_Test $test
198      * @param float                  $time
199      */
200     public function endTest(PHPUnit_Framework_Test $test, $time)
201     {
202         if ($this->testSuccessful === true) {
203             $this->write(
204                 sprintf(
205                     "ok %d - %s\n",
206                     $this->testNumber,
207                     PHPUnit_Util_Test::describe($test)
208                 )
209             );
210         }
211
212         $this->writeDiagnostics($test);
213     }
214
215     /**
216      * @param PHPUnit_Framework_Test $test
217      * @param string                 $prefix
218      * @param string                 $directive
219      */
220     protected function writeNotOk(PHPUnit_Framework_Test $test, $prefix = '', $directive = '')
221     {
222         $this->write(
223             sprintf(
224                 "not ok %d - %s%s%s\n",
225                 $this->testNumber,
226                 $prefix != '' ? $prefix . ': ' : '',
227                 PHPUnit_Util_Test::describe($test),
228                 $directive != '' ? ' # ' . $directive : ''
229             )
230         );
231
232         $this->testSuccessful = false;
233     }
234
235     /**
236      * @param PHPUnit_Framework_Test $test
237      */
238     private function writeDiagnostics(PHPUnit_Framework_Test $test)
239     {
240         if (!$test instanceof PHPUnit_Framework_TestCase) {
241             return;
242         }
243
244         if (!$test->hasOutput()) {
245             return;
246         }
247
248         foreach (explode("\n", trim($test->getActualOutput())) as $line) {
249             $this->write(
250                 sprintf(
251                     "# %s\n",
252                     $line
253                 )
254             );
255         }
256     }
257 }