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