SHINDIG-1056 by lipeng, BasicRemoteContentTest doesn't depend on static private key...
[shindig.git] / php / external / PHPUnit / Util / Filter.php
blob1194c8d9ed76d9fdcf2198034cb2f72688e71067
1 <?php
2 /**
3 * PHPUnit
5 * Copyright (c) 2002-2008, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
37 * @category Testing
38 * @package PHPUnit
39 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
40 * @copyright 2002-2008 Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
42 * @version SVN: $Id: Filter.php 2041 2008-01-08 10:00:39Z sb $
43 * @link http://www.phpunit.de/
44 * @since File available since Release 2.0.0
47 require_once 'PHPUnit/Util/FilterIterator.php';
49 /**
50 * Utility class for code filtering.
52 * @category Testing
53 * @package PHPUnit
54 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
55 * @copyright 2002-2008 Sebastian Bergmann <sb@sebastian-bergmann.de>
56 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
57 * @version Release: 3.2.9
58 * @link http://www.phpunit.de/
59 * @since Class available since Release 2.0.0
61 class PHPUnit_Util_Filter {
62 /**
63 * @var boolean
64 * @access public
65 * @static
67 public static $addUncoveredFilesFromWhitelist = TRUE;
69 /**
70 * @var boolean
71 * @access public
72 * @static
74 public static $filterPHPUnit = TRUE;
76 /**
77 * @var boolean
78 * @access protected
79 * @static
81 protected static $filter = TRUE;
83 /**
84 * @var boolean
85 * @access protected
86 * @static
88 protected static $blackListConverstionForWindowsDone = FALSE;
90 /**
91 * Source files that are blacklisted.
93 * @var array
94 * @access protected
95 * @static
97 protected static $blacklistedFiles = array('DEFAULT' => array(), 'PHPUNIT' => array(), 'TESTS' => array(),
98 'PEAR' => array('Console/Getopt.php', 'Image/GraphViz.php', 'Log/composite.php',
99 'Log/console.php', 'Log/daemon.php', 'Log/display.php',
100 'Log/error_log.php', 'Log/file.php', 'Log/mail.php', 'Log/mcal.php',
101 'Log/mdb2.php', 'Log/null.php', 'Log/observer.php', 'Log/sql.php',
102 'Log/sqlite.php', 'Log/syslog.php', 'Log/win.php', 'Log.php',
103 'PEAR/Installer/Role/Common.php', 'PEAR/Installer/Role.php',
104 'PEAR/Config.php', 'PEAR/DependencyDB.php', 'PEAR/Registry.php',
105 'PEAR/Remote.php', 'PEAR/RunTest.php', 'PEAR/XMLParser.php',
106 'PEAR.php', 'System.php'));
109 * Source files that are whitelisted.
111 * @var array
112 * @access protected
113 * @static
115 protected static $whitelistedFiles = array();
118 * Adds a directory to the blacklist (recursively).
120 * @param string $directory
121 * @param string $suffix
122 * @param string $group
123 * @access public
124 * @static
125 * @since Method available since Release 3.1.5
127 public static function addDirectoryToFilter($directory, $suffix = '.php', $group = 'DEFAULT') {
128 if (file_exists($directory)) {
129 foreach (self::getIterator($directory, $suffix) as $file) {
130 self::addFileToFilter($file->getPathName(), $group);
136 * Adds a new file to be filtered (blacklist).
138 * @param string $filename
139 * @param string $group
140 * @access public
141 * @static
142 * @since Method available since Release 2.1.0
144 public static function addFileToFilter($filename, $group = 'DEFAULT') {
145 if (file_exists($filename)) {
146 $filename = realpath($filename);
148 if (! isset(self::$blacklistedFiles[$group])) {
149 self::$blacklistedFiles[$group] = array($filename);
152 else if (! in_array($filename, self::$blacklistedFiles[$group])) {
153 self::$blacklistedFiles[$group][] = $filename;
159 * Removes a directory from the blacklist (recursively).
161 * @param string $directory
162 * @param string $suffix
163 * @param string $group
164 * @access public
165 * @static
166 * @since Method available since Release 3.1.5
168 public static function removeDirectoryFromFilter($directory, $suffix = '.php', $group = 'DEFAULT') {
169 if (file_exists($directory)) {
170 foreach (self::getIterator($directory, $suffix) as $file) {
171 self::removeFileFromFilter($file->getPathName(), $group);
177 * Removes a file from the filter (blacklist).
179 * @param string $filename
180 * @param string $group
181 * @access public
182 * @static
183 * @since Method available since Release 2.1.0
185 public static function removeFileFromFilter($filename, $group = 'DEFAULT') {
186 if (file_exists($filename)) {
187 if (isset(self::$blacklistedFiles[$group])) {
188 $filename = realpath($filename);
190 foreach (self::$blacklistedFiles[$group] as $key => $_filename) {
191 if ($filename == $_filename) {
192 unset(self::$blacklistedFiles[$group][$key]);
200 * Adds a directory to the whitelist (recursively).
202 * @param string $directory
203 * @param string $suffix
204 * @access public
205 * @static
206 * @since Method available since Release 3.1.5
208 public static function addDirectoryToWhitelist($directory, $suffix = '.php') {
209 if (file_exists($directory)) {
210 foreach (self::getIterator($directory, $suffix) as $file) {
211 self::addFileToWhitelist($file->getPathName());
217 * Adds a new file to the whitelist.
219 * When the whitelist is empty (default), blacklisting is used.
220 * When the whitelist is not empty, whitelisting is used.
222 * @param string $filename
223 * @access public
224 * @static
225 * @since Method available since Release 3.1.0
227 public static function addFileToWhitelist($filename) {
228 if (file_exists($filename)) {
229 $filename = realpath($filename);
231 if (! in_array($filename, self::$whitelistedFiles)) {
232 self::$whitelistedFiles[] = $filename;
238 * Removes a directory from the whitelist (recursively).
240 * @param string $directory
241 * @param string $suffix
242 * @access public
243 * @static
244 * @since Method available since Release 3.1.5
246 public static function removeDirectoryFromWhitelist($directory, $suffix = '.php') {
247 if (file_exists($directory)) {
248 foreach (self::getIterator($directory, $suffix) as $file) {
249 self::removeFileFromWhitelist($file->getPathName());
255 * Removes a file from the whitelist.
257 * @param string $filename
258 * @access public
259 * @static
260 * @since Method available since Release 3.1.0
262 public static function removeFileFromWhitelist($filename) {
263 if (file_exists($filename)) {
264 $filename = realpath($filename);
266 foreach (self::$whitelistedFiles as $key => $_filename) {
267 if ($filename == $_filename) {
268 unset(self::$whitelistedFiles[$key]);
275 * Returns data about files within code coverage information, specifically
276 * which ones will be filtered out and which ones may be whitelisted but not
277 * touched by coverage.
279 * Returns a two-item array. The first item is an array indexed by filenames
280 * with a boolean payload of whether they should be filtered out.
282 * The second item is an array of filenames which are
283 * whitelisted but which are absent from the coverage information.
285 * @param array $codeCoverageInformation
286 * @param boolean $filterTests
287 * @return array
288 * @access public
289 * @static
291 public static function getFileCodeCoverageDisposition(array $codeCoverageInformation, $filterTests = TRUE) {
292 if (! self::$filter) {
293 return array(array(), array());
296 $isFilteredCache = array();
297 $coveredFiles = array();
299 foreach ($codeCoverageInformation as $k => $test) {
300 foreach (array_keys($test['files']) as $file) {
301 if (! isset($isFilteredCache[$file])) {
302 $isFilteredCache[$file] = self::isFiltered($file, $filterTests);
307 $coveredFiles = array_keys($isFilteredCache);
308 $missedFiles = array_diff(self::$whitelistedFiles, $coveredFiles);
309 $missedFiles = array_filter($missedFiles, 'file_exists');
311 return array($isFilteredCache, $missedFiles);
315 * @param array $codeCoverageInformation
316 * @param boolean $addUncoveredFilesFromWhitelist
317 * @return array
318 * @access public
319 * @static
321 public static function getFilteredCodeCoverage(array $codeCoverageInformation, $filterTests = TRUE) {
322 if (self::$filter) {
323 list($isFilteredCache, $missedFiles) = self::getFileCodeCoverageDisposition($codeCoverageInformation, $filterTests);
325 foreach ($codeCoverageInformation as $k => $test) {
326 foreach (array_keys($test['files']) as $file) {
327 if ($isFilteredCache[$file]) {
328 unset($codeCoverageInformation[$k]['files'][$file]);
333 if (self::$addUncoveredFilesFromWhitelist) {
334 foreach ($missedFiles as $missedFile) {
335 xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
336 include_once $missedFile;
337 $coverage = xdebug_get_code_coverage();
338 xdebug_stop_code_coverage();
340 if (isset($coverage[$missedFile])) {
341 foreach ($coverage[$missedFile] as $line => $flag) {
342 if ($flag > 0) {
343 $coverage[$missedFile][$line] = - 1;
347 $codeCoverageInformation[] = array('test' => NULL,
348 'files' => array(
349 $missedFile => $coverage[$missedFile]));
355 return $codeCoverageInformation;
359 * Filters stack frames from PHPUnit classes.
361 * @param Exception $e
362 * @param boolean $filterTests
363 * @param boolean $asString
364 * @return string
365 * @access public
366 * @static
368 public static function getFilteredStacktrace(Exception $e, $filterTests = TRUE, $asString = TRUE) {
369 if ($asString === TRUE) {
370 $filteredStacktrace = '';
371 } else {
372 $filteredStacktrace = array();
375 foreach ($e->getTrace() as $frame) {
376 if (! self::$filter || (isset($frame['file']) && ! self::isFiltered($frame['file'], $filterTests, TRUE))) {
377 if ($asString === TRUE) {
378 $filteredStacktrace .= sprintf("%s:%s\n",
380 $frame['file'], isset($frame['line']) ? $frame['line'] : '?');
381 } else {
382 $filteredStacktrace[] = $frame;
387 return $filteredStacktrace;
391 * Activates or deactivates filtering.
393 * @param boolean $filter
394 * @throws InvalidArgumentException
395 * @access public
396 * @static
397 * @since Method available since Release 3.0.0
399 public static function setFilter($filter) {
400 if (is_bool($filter)) {
401 self::$filter = $filter;
402 } else {
403 throw new InvalidArgumentException();
408 * Returns a PHPUnit_Util_FilterIterator that iterates
409 * over all files in the given directory that have the
410 * given suffix.
412 * @param string $directory
413 * @param string $suffix
414 * @return Iterator
415 * @access protected
416 * @static
417 * @since Method available since Release 3.1.5
419 protected static function getIterator($directory, $suffix) {
420 return new PHPUnit_Util_FilterIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)), $suffix);
424 * @param string $filename
425 * @param boolean $filterTests
426 * @param boolean $ignoreWhitelist
427 * @return boolean
428 * @access protected
429 * @static
430 * @since Method available since Release 2.1.3
432 protected static function isFiltered($filename, $filterTests = TRUE, $ignoreWhitelist = FALSE) {
433 $filename = realpath($filename);
435 // Use blacklist.
436 if ($ignoreWhitelist || empty(self::$whitelistedFiles)) {
437 if (DIRECTORY_SEPARATOR == '\\' && ! self::$blackListConverstionForWindowsDone) {
438 $count = count(self::$blacklistedFiles['PEAR']);
440 for ($i = 0; $i < $count; $i ++) {
441 self::$blacklistedFiles['PEAR'][$i] = str_replace('/', '\\', self::$blacklistedFiles['PEAR'][$i]);
444 self::$blackListConverstionForWindowsDone = TRUE;
447 $blacklistedFiles = array_merge(self::$blacklistedFiles['DEFAULT'], self::$blacklistedFiles['PEAR']);
449 if ($filterTests) {
450 $blacklistedFiles = array_merge($blacklistedFiles, self::$blacklistedFiles['TESTS']);
453 if (self::$filterPHPUnit) {
454 $blacklistedFiles = array_merge($blacklistedFiles, self::$blacklistedFiles['PHPUNIT']);
457 if (in_array($filename, $blacklistedFiles)) {
458 return TRUE;
461 foreach ($blacklistedFiles as $filteredFile) {
462 if (strpos($filename, $filteredFile) !== FALSE) {
463 return TRUE;
467 return FALSE;
470 // Use whitelist.
471 else {
472 if (in_array($filename, self::$whitelistedFiles)) {
473 return FALSE;
476 return TRUE;
481 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');