PageRenderTime 22ms CodeModel.GetById 16ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/php/PEAR/PHPUnit2/Util/Filter.php

https://bitbucket.org/haggen/ibsolution-log-viewer
PHP | 263 lines | 142 code | 17 blank | 104 comment | 8 complexity | b3eac446d5eed2a98b14e9cb34fe7ec0 MD5 | raw file
  1<?php
  2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3
  4/**
  5 * PHP Version 5
  6 *
  7 * Copyright (c) 2002-2006, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  8 * All rights reserved.
  9 *
 10 * Redistribution and use in source and binary forms, with or without
 11 * modification, are permitted provided that the following conditions
 12 * are met:
 13 *
 14 *   * Redistributions of source code must retain the above copyright
 15 *     notice, this list of conditions and the following disclaimer.
 16 * 
 17 *   * Redistributions in binary form must reproduce the above copyright
 18 *     notice, this list of conditions and the following disclaimer in
 19 *     the documentation and/or other materials provided with the
 20 *     distribution.
 21 *
 22 *   * Neither the name of Sebastian Bergmann nor the names of his
 23 *     contributors may be used to endorse or promote products derived
 24 *     from this software without specific prior written permission.
 25 *
 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 29 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 30 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 32 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 36 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 37 * POSSIBILITY OF SUCH DAMAGE.
 38 *
 39 * @category   Testing
 40 * @package    PHPUnit2
 41 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
 42 * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
 43 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
 44 * @version    CVS: $Id: Filter.php,v 1.32.2.5 2005/12/17 16:04:58 sebastian Exp $
 45 * @link       http://pear.php.net/package/PHPUnit2
 46 * @since      File available since Release 2.0.0
 47 */
 48
 49/**
 50 * Utility class for code filtering.
 51 *
 52 * @category   Testing
 53 * @package    PHPUnit2
 54 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
 55 * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
 56 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
 57 * @version    Release: 2.3.6
 58 * @link       http://pear.php.net/package/PHPUnit2
 59 * @since      Class available since Release 2.0.0
 60 */
 61class PHPUnit2_Util_Filter {
 62    /**
 63     * Source files that are to be filtered.
 64     *
 65     * @var    array
 66     * @access protected
 67     * @static
 68     */
 69    protected static $filteredFiles = array(
 70      'PHPUnit2/Extensions/ExceptionTestCase.php',
 71      'PHPUnit2/Extensions/PerformanceTestCase.php',
 72      'PHPUnit2/Extensions/RepeatedTest.php',
 73      'PHPUnit2/Extensions/TestDecorator.php',
 74      'PHPUnit2/Extensions/TestSetup.php',
 75      'PHPUnit2/Framework/Assert.php',
 76      'PHPUnit2/Framework/AssertionFailedError.php',
 77      'PHPUnit2/Framework/ComparisonFailure.php',
 78      'PHPUnit2/Framework/Error.php',
 79      'PHPUnit2/Framework/IncompleteTest.php',
 80      'PHPUnit2/Framework/IncompleteTestError.php',
 81      'PHPUnit2/Framework/Test.php',
 82      'PHPUnit2/Framework/TestCase.php',
 83      'PHPUnit2/Framework/TestFailure.php',
 84      'PHPUnit2/Framework/TestListener.php',
 85      'PHPUnit2/Framework/TestResult.php',
 86      'PHPUnit2/Framework/TestSuite.php',
 87      'PHPUnit2/Framework/Warning.php',
 88      'PHPUnit2/Runner/BaseTestRunner.php',
 89      'PHPUnit2/Runner/IncludePathTestCollector.php',
 90      'PHPUnit2/Runner/StandardTestSuiteLoader.php',
 91      'PHPUnit2/Runner/TestCollector.php',
 92      'PHPUnit2/Runner/TestSuiteLoader.php',
 93      'PHPUnit2/Runner/Version.php',
 94      'PHPUnit2/TextUI/ResultPrinter.php',
 95      'PHPUnit2/TextUI/TestRunner.php',
 96      'PHPUnit2/Util/CodeCoverage/Renderer/HTML.php',
 97      'PHPUnit2/Util/CodeCoverage/Renderer/Text.php',
 98      'PHPUnit2/Util/CodeCoverage/Renderer.php',
 99      'PHPUnit2/Util/Log/PEAR.php',
100      'PHPUnit2/Util/Log/XML.php',
101      'PHPUnit2/Util/TestDox/ResultPrinter/HTML.php',
102      'PHPUnit2/Util/TestDox/ResultPrinter/Text.php',
103      'PHPUnit2/Util/TestDox/NamePrettifier.php',
104      'PHPUnit2/Util/TestDox/ResultPrinter.php',
105      'PHPUnit2/Util/ErrorHandler.php',
106      'PHPUnit2/Util/Fileloader.php',
107      'PHPUnit2/Util/Filter.php',
108      'PHPUnit2/Util/Printer.php',
109      'PHPUnit2/Util/Skeleton.php',
110      'Benchmark/Timer.php',
111      'Console/Getopt.php',
112      'Log/composite.php',
113      'Log/console.php',
114      'Log/display.php',
115      'Log/error.php',
116      'Log/file.php',
117      'Log/mail.php',
118      'Log/mcal.php',
119      'Log/null.php',
120      'Log/observer.php',
121      'Log/sql.php',
122      'Log/sqlite.php',
123      'Log/syslog.php',
124      'Log/win.php',
125      'Log.php',
126      'PEAR/Config.php',
127      'PEAR.php'
128    );
129
130    /**
131     * Adds a new file to be filtered.
132     *
133     * @param  string
134     * @access public
135     * @static
136     * @since  Method available since Release 2.1.0
137     */
138    public static function addFileToFilter($filename) {
139        $filename = self::getCanonicalFilename($filename);
140
141        if (!self::isFiltered($filename)) {
142            self::$filteredFiles[] = $filename;
143        }
144    }
145
146    /**
147     * Removes a file from the filter.
148     *
149     * @param  string
150     * @access public
151     * @static
152     * @since  Method available since Release 2.1.0
153     */
154    public static function removeFileFromFilter($filename) {
155        $filename = self::getCanonicalFilename($filename);
156        $keys     = array_keys(self::$filteredFiles);
157
158        for ($i = 0; $i < sizeof($keys); $i++) {
159            if (self::$filteredFiles[$keys[$i]] == $filename) {
160                unset(self::$filteredFiles[$keys[$i]]);
161                break;
162            }
163        }
164    }
165
166    /**
167     * Filters source lines from PHPUnit classes.
168     *
169     * @param  array
170     * @return array
171     * @access public
172     * @static
173     */
174    public static function getFilteredCodeCoverage($codeCoverageInformation) {
175        $files = array_keys($codeCoverageInformation);
176
177        foreach ($files as $file) {
178            if (self::isFiltered($file)) {
179                unset($codeCoverageInformation[$file]);
180            }
181        }
182
183        return $codeCoverageInformation;
184    }
185
186    /**
187     * Filters stack frames from PHPUnit classes.
188     *
189     * @param  Exception $e
190     * @return string
191     * @access public
192     * @static
193     */
194    public static function getFilteredStacktrace(Exception $e) {
195        $filteredStacktrace = '';
196        $stacktrace         = $e->getTrace();
197
198        foreach ($stacktrace as $frame) {
199            $filtered = FALSE;
200
201            if (isset($frame['file']) && !self::isFiltered($frame['file'])) {
202                $filteredStacktrace .= sprintf(
203                  "%s:%s\n",
204
205                  $frame['file'],
206                  isset($frame['line']) ? $frame['line'] : '?'
207                );
208            }
209        }
210
211        return $filteredStacktrace;
212    }
213
214    /**
215     * Canonicalizes a source file name.
216     *
217     * @param  string $filename
218     * @return string
219     * @access protected
220     * @static
221     */
222    protected static function getCanonicalFilename($filename) {
223        foreach (array('PHPUnit2', 'Benchmark', 'Console', 'PEAR') as $package) {
224            $pos = strpos($filename, $package);
225
226            if ($pos !== FALSE) {
227                $filename = substr($filename, $pos);
228                break;
229            }
230        }
231
232        return str_replace(
233          '\\',
234          '/',
235          $filename
236        );
237    }
238
239    /**
240     * @param  string $filename
241     * @return boolean
242     * @access protected
243     * @static
244     * @since  Method available since Release 2.1.3
245     */
246    protected static function isFiltered($filename) {
247        if (substr($filename, -7) == 'phpunit' ||
248            in_array(self::getCanonicalFilename($filename), self::$filteredFiles)) {
249            return TRUE;
250        }
251
252        return FALSE;
253    }
254}
255
256/*
257 * Local variables:
258 * tab-width: 4
259 * c-basic-offset: 4
260 * c-hanging-comment-ender-p: nil
261 * End:
262 */
263?>