PageRenderTime 85ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/library/PHPUnit/Util/Filter.php

https://github.com/humansky/qframe
PHP | 500 lines | 246 code | 43 blank | 211 comment | 42 complexity | 2e965f22924903453bac9610d0e2b3a5 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0
  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 1823 2007-12-05 10:46:12Z sb $
  43. * @link http://www.phpunit.de/
  44. * @since File available since Release 2.0.0
  45. */
  46. require_once 'PHPUnit/Util/FilterIterator.php';
  47. /**
  48. * Utility class for code filtering.
  49. *
  50. * @category Testing
  51. * @package PHPUnit
  52. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  53. * @copyright 2002-2007 Sebastian Bergmann <sb@sebastian-bergmann.de>
  54. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  55. * @version Release: 3.2.5
  56. * @link http://www.phpunit.de/
  57. * @since Class available since Release 2.0.0
  58. */
  59. class PHPUnit_Util_Filter
  60. {
  61. /**
  62. * @var boolean
  63. * @access public
  64. * @static
  65. */
  66. public static $addUncoveredFilesFromWhitelist = TRUE;
  67. /**
  68. * @var boolean
  69. * @access public
  70. * @static
  71. */
  72. public static $filterPHPUnit = TRUE;
  73. /**
  74. * @var boolean
  75. * @access protected
  76. * @static
  77. */
  78. protected static $filter = TRUE;
  79. /**
  80. * Source files that are blacklisted.
  81. *
  82. * @var array
  83. * @access protected
  84. * @static
  85. */
  86. protected static $blacklistedFiles = array(
  87. 'DEFAULT' => array(),
  88. 'PHPUNIT' => array(),
  89. 'TESTS' => array(),
  90. 'PEAR' => array(
  91. 'Console/Getopt.php',
  92. 'Image/GraphViz.php',
  93. 'Log/composite.php',
  94. 'Log/console.php',
  95. 'Log/daemon.php',
  96. 'Log/display.php',
  97. 'Log/error_log.php',
  98. 'Log/file.php',
  99. 'Log/mail.php',
  100. 'Log/mcal.php',
  101. 'Log/mdb2.php',
  102. 'Log/null.php',
  103. 'Log/observer.php',
  104. 'Log/sql.php',
  105. 'Log/sqlite.php',
  106. 'Log/syslog.php',
  107. 'Log/win.php',
  108. 'Log.php',
  109. 'PEAR/Installer/Role/Common.php',
  110. 'PEAR/Installer/Role.php',
  111. 'PEAR/Config.php',
  112. 'PEAR/DependencyDB.php',
  113. 'PEAR/Registry.php',
  114. 'PEAR/Remote.php',
  115. 'PEAR/RunTest.php',
  116. 'PEAR/XMLParser.php',
  117. 'PEAR.php',
  118. 'System.php'
  119. )
  120. );
  121. /**
  122. * Source files that are whitelisted.
  123. *
  124. * @var array
  125. * @access protected
  126. * @static
  127. */
  128. protected static $whitelistedFiles = array();
  129. /**
  130. * Adds a directory to the blacklist (recursively).
  131. *
  132. * @param string $directory
  133. * @param string $suffix
  134. * @param string $group
  135. * @access public
  136. * @static
  137. * @since Method available since Release 3.1.5
  138. */
  139. public static function addDirectoryToFilter($directory, $suffix = '.php', $group = 'DEFAULT')
  140. {
  141. if (file_exists($directory)) {
  142. foreach (self::getIterator($directory, $suffix) as $file) {
  143. self::addFileToFilter($file->getPathName(), $group);
  144. }
  145. }
  146. }
  147. /**
  148. * Adds a new file to be filtered (blacklist).
  149. *
  150. * @param string $filename
  151. * @param string $group
  152. * @access public
  153. * @static
  154. * @since Method available since Release 2.1.0
  155. */
  156. public static function addFileToFilter($filename, $group = 'DEFAULT')
  157. {
  158. if (file_exists($filename)) {
  159. $filename = realpath($filename);
  160. if (!isset(self::$blacklistedFiles[$group])) {
  161. self::$blacklistedFiles[$group] = array($filename);
  162. }
  163. else if (!in_array($filename, self::$blacklistedFiles[$group])) {
  164. self::$blacklistedFiles[$group][] = $filename;
  165. }
  166. }
  167. }
  168. /**
  169. * Removes a directory from the blacklist (recursively).
  170. *
  171. * @param string $directory
  172. * @param string $suffix
  173. * @param string $group
  174. * @access public
  175. * @static
  176. * @since Method available since Release 3.1.5
  177. */
  178. public static function removeDirectoryFromFilter($directory, $suffix = '.php', $group = 'DEFAULT')
  179. {
  180. if (file_exists($directory)) {
  181. foreach (self::getIterator($directory, $suffix) as $file) {
  182. self::removeFileFromFilter($file->getPathName(), $group);
  183. }
  184. }
  185. }
  186. /**
  187. * Removes a file from the filter (blacklist).
  188. *
  189. * @param string $filename
  190. * @param string $group
  191. * @access public
  192. * @static
  193. * @since Method available since Release 2.1.0
  194. */
  195. public static function removeFileFromFilter($filename, $group = 'DEFAULT')
  196. {
  197. if (file_exists($filename)) {
  198. if (isset(self::$blacklistedFiles[$group])) {
  199. $filename = realpath($filename);
  200. foreach (self::$blacklistedFiles[$group] as $key => $_filename) {
  201. if ($filename == $_filename) {
  202. unset(self::$blacklistedFiles[$group][$key]);
  203. }
  204. }
  205. }
  206. }
  207. }
  208. /**
  209. * Adds a directory to the whitelist (recursively).
  210. *
  211. * @param string $directory
  212. * @param string $suffix
  213. * @access public
  214. * @static
  215. * @since Method available since Release 3.1.5
  216. */
  217. public static function addDirectoryToWhitelist($directory, $suffix = '.php')
  218. {
  219. if (file_exists($directory)) {
  220. foreach (self::getIterator($directory, $suffix) as $file) {
  221. self::addFileToWhitelist($file->getPathName());
  222. }
  223. }
  224. }
  225. /**
  226. * Adds a new file to the whitelist.
  227. *
  228. * When the whitelist is empty (default), blacklisting is used.
  229. * When the whitelist is not empty, whitelisting is used.
  230. *
  231. * @param string $filename
  232. * @access public
  233. * @static
  234. * @since Method available since Release 3.1.0
  235. */
  236. public static function addFileToWhitelist($filename)
  237. {
  238. if (file_exists($filename)) {
  239. $filename = realpath($filename);
  240. if (!in_array($filename, self::$whitelistedFiles)) {
  241. self::$whitelistedFiles[] = $filename;
  242. }
  243. }
  244. }
  245. /**
  246. * Removes a directory from the whitelist (recursively).
  247. *
  248. * @param string $directory
  249. * @param string $suffix
  250. * @access public
  251. * @static
  252. * @since Method available since Release 3.1.5
  253. */
  254. public static function removeDirectoryFromWhitelist($directory, $suffix = '.php')
  255. {
  256. if (file_exists($directory)) {
  257. foreach (self::getIterator($directory, $suffix) as $file) {
  258. self::removeFileFromWhitelist($file->getPathName());
  259. }
  260. }
  261. }
  262. /**
  263. * Removes a file from the whitelist.
  264. *
  265. * @param string $filename
  266. * @access public
  267. * @static
  268. * @since Method available since Release 3.1.0
  269. */
  270. public static function removeFileFromWhitelist($filename)
  271. {
  272. if (file_exists($filename)) {
  273. $filename = realpath($filename);
  274. foreach (self::$whitelistedFiles as $key => $_filename) {
  275. if ($filename == $_filename) {
  276. unset(self::$whitelistedFiles[$key]);
  277. }
  278. }
  279. }
  280. }
  281. /**
  282. * Filters source lines from PHPUnit classes.
  283. *
  284. * @param array $codeCoverageInformation
  285. * @param boolean $filterTests
  286. * @return array
  287. * @access public
  288. * @static
  289. */
  290. public static function getFilteredCodeCoverage(array $codeCoverageInformation, $filterTests = TRUE)
  291. {
  292. if (self::$filter) {
  293. $coveredFiles = array();
  294. $isFilteredCache = array();
  295. foreach ($codeCoverageInformation as $k => $test) {
  296. foreach (array_keys($test['files']) as $file) {
  297. if (!isset($isFilteredCache[$file])) {
  298. $isFilteredCache[$file] = self::isFiltered($file, $filterTests);
  299. }
  300. if ($isFilteredCache[$file]) {
  301. unset($codeCoverageInformation[$k]['files'][$file]);
  302. } else {
  303. $coveredFiles[$file] = 1;
  304. }
  305. }
  306. }
  307. if (self::$addUncoveredFilesFromWhitelist) {
  308. foreach (self::$whitelistedFiles as $whitelistedFile) {
  309. if (!isset($coveredFiles[$whitelistedFile])) {
  310. if (file_exists($whitelistedFile)) {
  311. xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
  312. include_once $whitelistedFile;
  313. $coverage = xdebug_get_code_coverage();
  314. xdebug_stop_code_coverage();
  315. if (isset($coverage[$whitelistedFile])) {
  316. foreach ($coverage[$whitelistedFile] as $line => $flag) {
  317. if ($flag > 0) {
  318. $coverage[$whitelistedFile][$line] = -1;
  319. }
  320. }
  321. $codeCoverageInformation[] = array(
  322. 'test' => NULL,
  323. 'files' => array(
  324. $whitelistedFile => $coverage[$whitelistedFile]
  325. )
  326. );
  327. $coveredFiles[$whitelistedFile] = 1;
  328. }
  329. }
  330. }
  331. }
  332. }
  333. }
  334. return $codeCoverageInformation;
  335. }
  336. /**
  337. * Filters stack frames from PHPUnit classes.
  338. *
  339. * @param Exception $e
  340. * @param boolean $filterTests
  341. * @param boolean $asString
  342. * @return string
  343. * @access public
  344. * @static
  345. */
  346. public static function getFilteredStacktrace(Exception $e, $filterTests = TRUE, $asString = TRUE)
  347. {
  348. if ($asString === TRUE) {
  349. $filteredStacktrace = '';
  350. } else {
  351. $filteredStacktrace = array();
  352. }
  353. foreach ($e->getTrace() as $frame) {
  354. if (!self::$filter || (isset($frame['file']) && !self::isFiltered($frame['file'], $filterTests, TRUE))) {
  355. if ($asString === TRUE) {
  356. $filteredStacktrace .= sprintf(
  357. "%s:%s\n",
  358. $frame['file'],
  359. isset($frame['line']) ? $frame['line'] : '?'
  360. );
  361. } else {
  362. $filteredStacktrace[] = $frame;
  363. }
  364. }
  365. }
  366. return $filteredStacktrace;
  367. }
  368. /**
  369. * Activates or deactivates filtering.
  370. *
  371. * @param boolean $filter
  372. * @throws InvalidArgumentException
  373. * @access public
  374. * @static
  375. * @since Method available since Release 3.0.0
  376. */
  377. public static function setFilter($filter)
  378. {
  379. if (is_bool($filter)) {
  380. self::$filter = $filter;
  381. } else {
  382. throw new InvalidArgumentException;
  383. }
  384. }
  385. /**
  386. * Returns a PHPUnit_Util_FilterIterator that iterates
  387. * over all files in the given directory that have the
  388. * given suffix.
  389. *
  390. * @param string $directory
  391. * @param string $suffix
  392. * @return Iterator
  393. * @access protected
  394. * @static
  395. * @since Method available since Release 3.1.5
  396. */
  397. protected static function getIterator($directory, $suffix)
  398. {
  399. return new PHPUnit_Util_FilterIterator(
  400. new RecursiveIteratorIterator(
  401. new RecursiveDirectoryIterator($directory)
  402. ),
  403. $suffix
  404. );
  405. }
  406. /**
  407. * @param string $filename
  408. * @param boolean $filterTests
  409. * @param boolean $ignoreWhitelist
  410. * @return boolean
  411. * @access protected
  412. * @static
  413. * @since Method available since Release 2.1.3
  414. */
  415. protected static function isFiltered($filename, $filterTests = TRUE, $ignoreWhitelist = FALSE)
  416. {
  417. $filename = realpath($filename);
  418. // Use blacklist.
  419. if ($ignoreWhitelist || empty(self::$whitelistedFiles)) {
  420. $blacklistedFiles = array_merge(
  421. self::$blacklistedFiles['DEFAULT'],
  422. self::$blacklistedFiles['PEAR']
  423. );
  424. if ($filterTests) {
  425. $blacklistedFiles = array_merge(
  426. $blacklistedFiles,
  427. self::$blacklistedFiles['TESTS']
  428. );
  429. }
  430. if (self::$filterPHPUnit) {
  431. $blacklistedFiles = array_merge(
  432. $blacklistedFiles,
  433. self::$blacklistedFiles['PHPUNIT']
  434. );
  435. }
  436. if (in_array($filename, $blacklistedFiles)) {
  437. return TRUE;
  438. }
  439. foreach ($blacklistedFiles as $filteredFile) {
  440. if (strpos($filename, $filteredFile) !== FALSE) {
  441. return TRUE;
  442. }
  443. }
  444. return FALSE;
  445. }
  446. // Use whitelist.
  447. else
  448. {
  449. if (in_array($filename, self::$whitelistedFiles)) {
  450. return FALSE;
  451. }
  452. return TRUE;
  453. }
  454. }
  455. }
  456. PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
  457. ?>