]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHPUnit/Util/Log/TAP.php
Release 6.2.0
[Github/sugarcrm.git] / tests / PHPUnit / PHPUnit / Util / Log / TAP.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
15  *   * Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in
17  *     the documentation and/or other materials provided with the
18  *     distribution.
19  *
20  *   * Neither the name of Sebastian Bergmann nor the names of his
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * @package    PHPUnit
38  * @subpackage Util_Log
39  * @author     Sebastian Bergmann <sebastian@phpunit.de>
40  * @copyright  2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42  * @link       http://www.phpunit.de/
43  * @since      File available since Release 3.0.0
44  */
45
46 require_once 'SymfonyComponents/YAML/sfYamlDumper.php';
47
48 /**
49  * A TestListener that generates a logfile of the
50  * test execution using the Test Anything Protocol (TAP).
51  *
52  * @package    PHPUnit
53  * @subpackage Util_Log
54  * @author     Sebastian Bergmann <sebastian@phpunit.de>
55  * @copyright  2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
56  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
57  * @version    Release: 3.5.13
58  * @link       http://www.phpunit.de/
59  * @since      Class available since Release 3.0.0
60  */
61 class PHPUnit_Util_Log_TAP extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
62 {
63     /**
64      * @var    integer
65      */
66     protected $testNumber = 0;
67
68     /**
69      * @var    integer
70      */
71     protected $testSuiteLevel = 0;
72
73     /**
74      * @var    boolean
75      */
76     protected $testSuccessful = TRUE;
77
78     /**
79      * Constructor.
80      *
81      * @param  mixed $out
82      * @throws InvalidArgumentException
83      * @since  Method available since Release 3.3.4
84      */
85     public function __construct($out = NULL)
86     {
87         parent::__construct($out);
88         $this->write("TAP version 13\n");
89     }
90
91     /**
92      * An error occurred.
93      *
94      * @param  PHPUnit_Framework_Test $test
95      * @param  Exception              $e
96      * @param  float                  $time
97      */
98     public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
99     {
100         $this->writeNotOk($test, 'Error');
101     }
102
103     /**
104      * A failure occurred.
105      *
106      * @param  PHPUnit_Framework_Test                 $test
107      * @param  PHPUnit_Framework_AssertionFailedError $e
108      * @param  float                                  $time
109      */
110     public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
111     {
112         $this->writeNotOk($test, 'Failure');
113
114         $message = explode(
115           "\n", PHPUnit_Framework_TestFailure::exceptionToString($e)
116         );
117
118         $diagnostic = array(
119           'message'  => $message[0],
120           'severity' => 'fail'
121         );
122
123         if ($e instanceof PHPUnit_Framework_ExpectationFailedException) {
124             $cf = $e->getComparisonFailure();
125
126             if ($cf !== NULL) {
127                 $diagnostic['data'] = array(
128                   'got'      => $cf->getActual(),
129                   'expected' => $cf->getExpected()
130                 );
131             }
132         }
133
134         $yaml = new sfYamlDumper();
135
136         $this->write(
137           sprintf(
138             "  ---\n%s  ...\n",
139             $yaml->dump($diagnostic, 2, 2)
140           )
141         );
142     }
143
144     /**
145      * Incomplete test.
146      *
147      * @param  PHPUnit_Framework_Test $test
148      * @param  Exception              $e
149      * @param  float                  $time
150      */
151     public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
152     {
153         $this->writeNotOk($test, '', 'TODO Incomplete Test');
154     }
155
156     /**
157      * Skipped test.
158      *
159      * @param  PHPUnit_Framework_Test $test
160      * @param  Exception              $e
161      * @param  float                  $time
162      * @since  Method available since Release 3.0.0
163      */
164     public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
165     {
166         $this->write(
167           sprintf(
168             "ok %d - # SKIP%s\n",
169
170             $this->testNumber,
171             $e->getMessage() != '' ? ' ' . $e->getMessage() : ''
172           )
173         );
174
175         $this->testSuccessful = FALSE;
176     }
177
178     /**
179      * A testsuite started.
180      *
181      * @param  PHPUnit_Framework_TestSuite $suite
182      */
183     public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
184     {
185         $this->testSuiteLevel++;
186     }
187
188     /**
189      * A testsuite ended.
190      *
191      * @param  PHPUnit_Framework_TestSuite $suite
192      */
193     public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
194     {
195         $this->testSuiteLevel--;
196
197         if ($this->testSuiteLevel == 0) {
198             $this->write(sprintf("1..%d\n", $this->testNumber));
199         }
200     }
201
202     /**
203      * A test started.
204      *
205      * @param  PHPUnit_Framework_Test $test
206      */
207     public function startTest(PHPUnit_Framework_Test $test)
208     {
209         $this->testNumber++;
210         $this->testSuccessful = TRUE;
211     }
212
213     /**
214      * A test ended.
215      *
216      * @param  PHPUnit_Framework_Test $test
217      * @param  float                  $time
218      */
219     public function endTest(PHPUnit_Framework_Test $test, $time)
220     {
221         if ($this->testSuccessful === TRUE) {
222             $this->write(
223               sprintf(
224                 "ok %d - %s\n",
225
226                 $this->testNumber,
227                 PHPUnit_Util_Test::describe($test)
228               )
229             );
230         }
231     }
232
233     /**
234      * @param  PHPUnit_Framework_Test $test
235      * @param  string                 $prefix
236      * @param  string                 $directive
237      */
238     protected function writeNotOk(PHPUnit_Framework_Test $test, $prefix = '', $directive = '')
239     {
240         $this->write(
241           sprintf(
242             "not ok %d - %s%s%s\n",
243
244             $this->testNumber,
245             $prefix != '' ? $prefix . ': ' : '',
246             PHPUnit_Util_Test::describe($test),
247             $directive != '' ? ' # ' . $directive : ''
248           )
249         );
250
251         $this->testSuccessful = FALSE;
252     }
253 }