]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Util/Log/PEAR.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Util / Log / PEAR.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.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  * @category   Testing
38  * @package    PHPUnit
39  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
40  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42
43  * @link       http://www.phpunit.de/
44  * @since      File available since Release 2.3.0
45  */
46
47 require_once 'PHPUnit/Framework.php';
48 require_once 'PHPUnit/Util/Filter.php';
49
50 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
51
52 /**
53  * A TestListener that logs to a PEAR_Log sink.
54  *
55  * @category   Testing
56  * @package    PHPUnit
57  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
58  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
59  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
60  * @version    Release: 3.3.17
61  * @link       http://www.phpunit.de/
62  * @since      Class available since Release 2.1.0
63  */
64 class PHPUnit_Util_Log_PEAR implements PHPUnit_Framework_TestListener
65 {
66     /**
67      * Log.
68      *
69      * @var    Log
70      */
71     protected $log;
72
73     /**
74      * @param string $type      The type of concrete Log subclass to use.
75      *                          Currently, valid values are 'console',
76      *                          'syslog', 'sql', 'file', and 'mcal'.
77      * @param string $name      The name of the actually log file, table, or
78      *                          other specific store to use. Defaults to an
79      *                          empty string, with which the subclass will
80      *                          attempt to do something intelligent.
81      * @param string $ident     The identity reported to the log system.
82      * @param array  $conf      A hash containing any additional configuration
83      *                          information that a subclass might need.
84      * @param int $maxLevel     Maximum priority level at which to log.
85      */
86     public function __construct($type, $name = '', $ident = '', $conf = array(), $maxLevel = PEAR_LOG_DEBUG)
87     {
88         if (PHPUnit_Util_Filesystem::fileExistsInIncludePath('Log.php')) {
89             PHPUnit_Util_Filesystem::collectStart();
90             require_once 'Log.php';
91             $this->log = Log::factory($type, $name, $ident, $conf, $maxLevel);
92
93             foreach (PHPUnit_Util_Filesystem::collectEnd() as $blacklistedFile) {
94                 PHPUnit_Util_Filter::addFileToFilter($blacklistedFile, 'PHPUNIT');
95             }
96         } else {
97             throw new RuntimeException('Log is not available.');
98         }
99     }
100
101     /**
102      * An error occurred.
103      *
104      * @param  PHPUnit_Framework_Test $test
105      * @param  Exception              $e
106      * @param  float                  $time
107      */
108     public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
109     {
110         $this->log->crit(
111           sprintf(
112             'Test "%s" failed: %s',
113
114             $test->getName(),
115             $e->getMessage()
116           )
117         );
118     }
119
120     /**
121      * A failure occurred.
122      *
123      * @param  PHPUnit_Framework_Test                 $test
124      * @param  PHPUnit_Framework_AssertionFailedError $e
125      * @param  float                                  $time
126      */
127     public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
128     {
129         $this->log->err(
130           sprintf(
131             'Test "%s" failed: %s',
132
133             $test->getName(),
134             $e->getMessage()
135           )
136         );
137     }
138
139     /**
140      * Incomplete test.
141      *
142      * @param  PHPUnit_Framework_Test $test
143      * @param  Exception              $e
144      * @param  float                  $time
145      */
146     public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
147     {
148         $this->log->info(
149           sprintf(
150             'Test "%s" incomplete: %s',
151
152             $test->getName(),
153             $e->getMessage()
154           )
155         );
156     }
157
158     /**
159      * Skipped test.
160      *
161      * @param  PHPUnit_Framework_Test $test
162      * @param  Exception              $e
163      * @param  float                  $time
164      * @since  Method available since Release 3.0.0
165      */
166     public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
167     {
168         $this->log->info(
169           sprintf(
170             'Test "%s" skipped: %s',
171
172             $test->getName(),
173             $e->getMessage()
174           )
175         );
176     }
177
178     /**
179      * A test suite started.
180      *
181      * @param  PHPUnit_Framework_TestSuite $suite
182      * @since  Method available since Release 2.2.0
183      */
184     public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
185     {
186         $this->log->info(
187           sprintf(
188             'TestSuite "%s" started.',
189
190             $suite->getName()
191           )
192         );
193     }
194
195     /**
196      * A test suite ended.
197      *
198      * @param  PHPUnit_Framework_TestSuite $suite
199      * @since  Method available since Release 2.2.0
200      */
201     public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
202     {
203         $this->log->info(
204           sprintf(
205             'TestSuite "%s" ended.',
206
207             $suite->getName()
208           )
209         );
210     }
211
212     /**
213      * A test started.
214      *
215      * @param  PHPUnit_Framework_Test $test
216      */
217     public function startTest(PHPUnit_Framework_Test $test)
218     {
219         $this->log->info(
220           sprintf(
221             'Test "%s" started.',
222
223             $test->getName()
224           )
225         );
226     }
227
228     /**
229      * A test ended.
230      *
231      * @param  PHPUnit_Framework_Test $test
232      * @param  float                  $time
233      */
234     public function endTest(PHPUnit_Framework_Test $test, $time)
235     {
236         $this->log->info(
237           sprintf(
238             'Test "%s" ended.',
239
240             $test->getName()
241           )
242         );
243     }
244 }
245 ?>