]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHPUnit/Util/Log/JSON.php
Add .gitignore
[Github/sugarcrm.git] / tests / PHPUnit / PHPUnit / Util / Log / JSON.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 /**
47  * A TestListener that generates JSON messages.
48  *
49  * @package    PHPUnit
50  * @subpackage Util_Log
51  * @author     Sebastian Bergmann <sebastian@phpunit.de>
52  * @copyright  2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
53  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
54  * @version    Release: 3.5.13
55  * @link       http://www.phpunit.de/
56  * @since      Class available since Release 3.0.0
57  */
58 class PHPUnit_Util_Log_JSON extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
59 {
60     /**
61      * @var    string
62      */
63     protected $currentTestSuiteName = '';
64
65     /**
66      * @var    string
67      */
68     protected $currentTestName = '';
69
70     /**
71      * @var     boolean
72      * @access  private
73      */
74     protected $currentTestPass = TRUE;
75
76     /**
77      * An error occurred.
78      *
79      * @param  PHPUnit_Framework_Test $test
80      * @param  Exception              $e
81      * @param  float                  $time
82      */
83     public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
84     {
85         $this->writeCase(
86           'error',
87           $time,
88           PHPUnit_Util_Filter::getFilteredStacktrace(
89             $e,
90             TRUE,
91             FALSE
92           ),
93           $e->getMessage()
94         );
95
96         $this->currentTestPass = FALSE;
97     }
98
99     /**
100      * A failure occurred.
101      *
102      * @param  PHPUnit_Framework_Test                 $test
103      * @param  PHPUnit_Framework_AssertionFailedError $e
104      * @param  float                                  $time
105      */
106     public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
107     {
108         $this->writeCase(
109           'fail',
110           $time,
111           PHPUnit_Util_Filter::getFilteredStacktrace(
112             $e,
113             TRUE,
114             FALSE
115           ),
116           $e->getMessage()
117         );
118
119         $this->currentTestPass = FALSE;
120     }
121
122     /**
123      * Incomplete test.
124      *
125      * @param  PHPUnit_Framework_Test $test
126      * @param  Exception              $e
127      * @param  float                  $time
128      */
129     public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
130     {
131         $this->writeCase('error', $time, array(), 'Incomplete Test');
132
133         $this->currentTestPass = FALSE;
134     }
135
136     /**
137      * Skipped test.
138      *
139      * @param  PHPUnit_Framework_Test $test
140      * @param  Exception              $e
141      * @param  float                  $time
142      */
143     public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
144     {
145         $this->writeCase('error', $time, array(), 'Skipped Test');
146
147         $this->currentTestPass = FALSE;
148     }
149
150     /**
151      * A testsuite started.
152      *
153      * @param  PHPUnit_Framework_TestSuite $suite
154      */
155     public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
156     {
157         $this->currentTestSuiteName = $suite->getName();
158         $this->currentTestName      = '';
159
160         $this->write(
161           array(
162             'event' => 'suiteStart',
163             'suite' => $this->currentTestSuiteName,
164             'tests' => count($suite)
165           )
166         );
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->currentTestSuiteName = '';
177         $this->currentTestName      = '';
178     }
179
180     /**
181      * A test started.
182      *
183      * @param  PHPUnit_Framework_Test $test
184      */
185     public function startTest(PHPUnit_Framework_Test $test)
186     {
187         $this->currentTestName = PHPUnit_Util_Test::describe($test);
188         $this->currentTestPass = TRUE;
189
190         $this->write(
191           array(
192             'event' => 'testStart',
193             'suite' => $this->currentTestSuiteName,
194             'test'  => $this->currentTestName
195           )
196         );
197     }
198
199     /**
200      * A test ended.
201      *
202      * @param  PHPUnit_Framework_Test $test
203      * @param  float                  $time
204      */
205     public function endTest(PHPUnit_Framework_Test $test, $time)
206     {
207         if ($this->currentTestPass) {
208             $this->writeCase('pass', $time);
209         }
210     }
211
212     /**
213      * @param string $status
214      * @param float  $time
215      * @param array  $trace
216      * @param string $message
217      */
218     protected function writeCase($status, $time, array $trace = array(), $message = '')
219     {
220         $this->write(
221           array(
222             'event'   => 'test',
223             'suite'   => $this->currentTestSuiteName,
224             'test'    => $this->currentTestName,
225             'status'  => $status,
226             'time'    => $time,
227             'trace'   => $trace,
228             'message' => $message
229           )
230         );
231     }
232
233     /**
234      * @param string $buffer
235      */
236     public function write($buffer)
237     {
238         parent::write(json_encode($buffer));
239     }
240 }