PageRenderTime 65ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/extlib/PHPUnit/Util/Filter.php

https://github.com/lmorchard/feedmagick2
PHP | 280 lines | 142 code | 14 blank | 124 comment | 15 complexity | 53d5935c680c5bab4b6aa59088fb9c6b MD5 | raw file
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2007, 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-2007 Sebastian Bergmann <sb@sebastian-bergmann.de>
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. * @version SVN: $Id: Filter.php 579 2007-03-18 16:10:52Z sb $
  43. * @link http://www.phpunit.de/
  44. * @since File available since Release 2.0.0
  45. */
  46. /**
  47. * Utility class for code filtering.
  48. *
  49. * @category Testing
  50. * @package PHPUnit
  51. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  52. * @copyright 2002-2007 Sebastian Bergmann <sb@sebastian-bergmann.de>
  53. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  54. * @version Release: 3.1.0beta3
  55. * @link http://www.phpunit.de/
  56. * @since Class available since Release 2.0.0
  57. */
  58. class PHPUnit_Util_Filter
  59. {
  60. /**
  61. * @var boolean
  62. * @access private
  63. * @static
  64. */
  65. private static $filter = TRUE;
  66. /**
  67. * Source files that are to be filtered.
  68. *
  69. * @var array
  70. * @access protected
  71. * @static
  72. */
  73. protected static $filteredFiles = array(
  74. 'DEFAULT' => array(),
  75. 'PHPUNIT' => array(),
  76. 'TESTS' => array(),
  77. 'PEAR' => array(
  78. 'Image/GraphViz.php',
  79. 'Log/composite.php',
  80. 'Log/console.php',
  81. 'Log/daemon.php',
  82. 'Log/display.php',
  83. 'Log/error_log.php',
  84. 'Log/file.php',
  85. 'Log/mail.php',
  86. 'Log/mcal.php',
  87. 'Log/mdb2.php',
  88. 'Log/null.php',
  89. 'Log/observer.php',
  90. 'Log/sql.php',
  91. 'Log/sqlite.php',
  92. 'Log/syslog.php',
  93. 'Log/win.php',
  94. 'Log.php',
  95. 'PEAR/Config.php',
  96. 'PEAR.php'
  97. )
  98. );
  99. /**
  100. * Adds a new file to be filtered.
  101. *
  102. * @param string $filename
  103. * @param string $group
  104. * @access public
  105. * @static
  106. * @since Method available since Release 2.1.0
  107. */
  108. public static function addFileToFilter($filename, $group = 'DEFAULT')
  109. {
  110. self::$filteredFiles[$group][] = self::getCanonicalFilename($filename);
  111. }
  112. /**
  113. * Removes a file from the filter.
  114. *
  115. * @param string $filename
  116. * @param string $group
  117. * @access public
  118. * @static
  119. * @since Method available since Release 2.1.0
  120. */
  121. public static function removeFileFromFilter($filename, $group = 'DEFAULT')
  122. {
  123. if (isset(self::$filteredFiles[$group])) {
  124. $filename = self::getCanonicalFilename($filename);
  125. foreach (self::$filteredFiles[$group] as $key => $_filename) {
  126. if ($filename == $_filename) {
  127. unset(self::$filteredFiles[$group][$key]);
  128. }
  129. }
  130. }
  131. }
  132. /**
  133. * Filters source lines from PHPUnit classes.
  134. *
  135. * @param array $codeCoverageInformation
  136. * @param boolean $filterTests
  137. * @param boolean $filterPHPUnit
  138. * @return array
  139. * @access public
  140. * @static
  141. */
  142. public static function getFilteredCodeCoverage(array $codeCoverageInformation, $filterTests = TRUE, $filterPHPUnit = TRUE)
  143. {
  144. if (self::$filter) {
  145. $max = count($codeCoverageInformation);
  146. for ($i = 0; $i < $max; $i++) {
  147. foreach (array_keys($codeCoverageInformation[$i]['files']) as $file) {
  148. if (self::isFiltered($file, $filterTests, $filterPHPUnit)) {
  149. unset($codeCoverageInformation[$i]['files'][$file]);
  150. }
  151. }
  152. }
  153. }
  154. return $codeCoverageInformation;
  155. }
  156. /**
  157. * Filters stack frames from PHPUnit classes.
  158. *
  159. * @param Exception $e
  160. * @param boolean $filterTests
  161. * @param boolean $filterPHPUnit
  162. * @param boolean $asString
  163. * @return string
  164. * @access public
  165. * @static
  166. */
  167. public static function getFilteredStacktrace(Exception $e, $filterTests = TRUE, $filterPHPUnit = TRUE, $asString = TRUE)
  168. {
  169. if ($asString === TRUE) {
  170. $filteredStacktrace = '';
  171. } else {
  172. $filteredStacktrace = array();
  173. }
  174. foreach ($e->getTrace() as $frame) {
  175. if (!self::$filter || (isset($frame['file']) && !self::isFiltered($frame['file'], $filterTests, $filterPHPUnit))) {
  176. if ($asString === TRUE) {
  177. $filteredStacktrace .= sprintf(
  178. "%s:%s\n",
  179. $frame['file'],
  180. isset($frame['line']) ? $frame['line'] : '?'
  181. );
  182. } else {
  183. $filteredStacktrace[] = $frame;
  184. }
  185. }
  186. }
  187. return $filteredStacktrace;
  188. }
  189. /**
  190. * Activates or deactivates filtering.
  191. *
  192. * @param boolean $filter
  193. * @throws InvalidArgumentException
  194. * @access public
  195. * @static
  196. * @since Method available since Release 3.0.0
  197. */
  198. public static function setFilter($filter)
  199. {
  200. if (is_bool($filter)) {
  201. self::$filter = $filter;
  202. } else {
  203. throw new InvalidArgumentException;
  204. }
  205. }
  206. /**
  207. * Canonicalizes a source file name.
  208. *
  209. * @param string $filename
  210. * @return string
  211. * @access protected
  212. * @static
  213. */
  214. protected static function getCanonicalFilename($filename)
  215. {
  216. return str_replace('\\', '/', $filename);
  217. }
  218. /**
  219. * @param string $filename
  220. * @param boolean $filterTests
  221. * @param boolean $filterPHPUnit
  222. * @return boolean
  223. * @access protected
  224. * @static
  225. * @since Method available since Release 2.1.3
  226. */
  227. protected static function isFiltered($filename, $filterTests = TRUE, $filterPHPUnit = TRUE)
  228. {
  229. $filename = self::getCanonicalFilename($filename);
  230. $filteredFiles = array_merge(
  231. self::$filteredFiles['DEFAULT'],
  232. self::$filteredFiles['PEAR']
  233. );
  234. if ($filterTests) {
  235. $filteredFiles = array_merge(
  236. $filteredFiles,
  237. self::$filteredFiles['TESTS']
  238. );
  239. }
  240. if ($filterPHPUnit) {
  241. $filteredFiles = array_merge(
  242. $filteredFiles,
  243. self::$filteredFiles['PHPUNIT']
  244. );
  245. }
  246. if (in_array($filename, $filteredFiles)) {
  247. return TRUE;
  248. }
  249. foreach ($filteredFiles as $filteredFile) {
  250. if (strpos($filename, $filteredFile) !== FALSE) {
  251. return TRUE;
  252. }
  253. }
  254. return FALSE;
  255. }
  256. }
  257. PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
  258. ?>