TestSuite.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. use PHPUnit\Framework\TestSuite;
  3. use PHPUnit\Framework\TestResult;
  4. /**
  5. * A version of the stock PHPUnit Test Suite that supports whitelisting
  6. * for code coverage filter.
  7. *
  8. * @package KO7/UnitTest
  9. *
  10. *
  11. * @copyright (c) 2007-2012 Kohana Team
  12. * @copyright (c) 2016-2018 Koseven Team
  13. * @license https://koseven.dev/LICENSE
  14. *
  15. * @codeCoverageIgnore Basic PhpUnit Test Suite. Unit Tests for this File can be ignored safely. If you change
  16. * this class, removing this may be necessary
  17. */
  18. abstract class KO7_Unittest_TestSuite extends TestSuite
  19. {
  20. /**
  21. * Holds the details of files that should be whitelisted for code coverage
  22. * @var array
  23. */
  24. protected $_filter_calls = [
  25. 'addFileToWhitelist' => []
  26. ];
  27. /**
  28. * Runs the tests and collects their result in a TestResult.
  29. *
  30. * @param TestResult|NULL $result
  31. *
  32. * @return ?TestResult
  33. * @throws ReflectionException
  34. */
  35. public function run(TestResult $result = NULL): TestResult
  36. {
  37. // Get the code coverage filter from the suite's result object
  38. $coverage = FALSE;
  39. if ($result !== NULL) {
  40. $coverage = $result->getCodeCoverage();
  41. }
  42. if ($coverage)
  43. {
  44. $coverage_filter = $coverage->filter();
  45. // Apply the white and blacklisting
  46. foreach ($this->_filter_calls as $method => $args)
  47. {
  48. foreach ($args as $arg)
  49. {
  50. $coverage_filter->$method($arg);
  51. }
  52. }
  53. }
  54. return parent::run($result);
  55. }
  56. /**
  57. * Queues a file to be added to the code coverage whitelist when the suite runs
  58. *
  59. * @param string $file
  60. */
  61. public function addFileToWhitelist(string $file) : void
  62. {
  63. $this->_filter_calls['addFileToWhitelist'][] = $file;
  64. }
  65. }