]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Util/Printer.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Util / Printer.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.0.0
45  */
46
47 require_once 'PHPUnit/Util/Filter.php';
48
49 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
50
51 /**
52  * Utility class that can print to STDOUT or write to a file.
53  *
54  * @category   Testing
55  * @package    PHPUnit
56  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
57  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
58  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
59  * @version    Release: 3.3.17
60  * @link       http://www.phpunit.de/
61  * @since      Class available since Release 2.0.0
62  * @abstract
63  */
64 abstract class PHPUnit_Util_Printer
65 {
66     /**
67      * If TRUE, flush output after every write.
68      *
69      * @var boolean
70      */
71     protected $autoFlush = FALSE;
72
73     /**
74      * @var    resource
75      */
76     protected $out;
77
78     /**
79      * @var    string
80      */
81     protected $outTarget;
82
83     /**
84      * @var    boolean
85      */
86     protected $printsHTML = FALSE;
87
88     /**
89      * Constructor.
90      *
91      * @param  mixed $out
92      * @throws InvalidArgumentException
93      */
94     public function __construct($out = NULL)
95     {
96         if ($out !== NULL) {
97             if (is_string($out)) {
98                 if (strpos($out, 'socket://') === 0) {
99                     $out = explode(':', str_replace('socket://', '', $out));
100
101                     if (sizeof($out) != 2) {
102                         throw new InvalidArgumentException;
103                     }
104
105                     $this->out = fsockopen($out[0], $out[1]);
106                 } else {
107                     $this->out = fopen($out, 'wt');
108                 }
109
110                 $this->outTarget = $out;
111             } else {
112                 $this->out = $out;
113             }
114         }
115     }
116
117     /**
118      * Flush buffer, optionally tidy up HTML, and close output.
119      *
120      */
121     public function flush()
122     {
123         if ($this->out !== NULL) {
124             fclose($this->out);
125         }
126
127         if ($this->printsHTML === TRUE && $this->outTarget !== NULL &&
128             strpos($this->outTarget, 'php://') !== 0 &&
129             strpos($this->outTarget, 'socket://') !== 0 &&
130             extension_loaded('tidy')) {
131             file_put_contents(
132               $this->outTarget,
133               tidy_repair_file(
134                 $this->outTarget, array('indent' => TRUE, 'wrap' => 0), 'utf8'
135               )
136             );
137         }
138     }
139
140     /**
141      * Performs a safe, incremental flush.
142      *
143      * Do not confuse this function with the flush() function of this class,
144      * since the flush() function may close the file being written to, rendering
145      * the current object no longer usable.
146      *
147      * @since  Method available since Release 3.3.0
148      */
149     public function incrementalFlush()
150     {
151         if ($this->out !== NULL) {
152             fflush($this->out);
153         } else {
154             flush();
155         }
156     }
157
158     /**
159      * @param  string $buffer
160      */
161     public function write($buffer)
162     {
163         if ($this->out !== NULL) {
164             fwrite($this->out, $buffer);
165
166             if ($this->autoFlush) {
167                 $this->incrementalFlush();
168             }
169         } else {
170             if (PHP_SAPI != 'cli') {
171                 $buffer = htmlspecialchars($buffer);
172             }
173
174             print $buffer;
175
176             if ($this->autoFlush) {
177                 $this->incrementalFlush();
178             }
179         }
180     }
181
182     /**
183      * Check auto-flush mode.
184      *
185      * @return boolean
186      * @since  Method available since Release 3.3.0
187      */
188     public function getAutoFlush()
189     {
190         return $this->autoFlush;
191     }
192
193     /**
194      * Set auto-flushing mode.
195      *
196      * If set, *incremental* flushes will be done after each write. This should
197      * not be confused with the different effects of this class' flush() method.
198      *
199      * @param boolean $autoFlush
200      * @since  Method available since Release 3.3.0
201      */
202     public function setAutoFlush($autoFlush)
203     {
204         if (is_bool($autoFlush)) {
205             $this->autoFlush = $autoFlush;
206         } else {
207             throw new InvalidArgumentException;
208         }
209     }
210 }
211 ?>