ComposerPhpunitXmlCoverageEdit.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. */
  18. /**
  19. * Edit phpunit.xml to speed up code coverage generation.
  20. *
  21. * Usage: composer phpunit:coverage-edit -- extensions/ExtensionName
  22. *
  23. * This class runs *outside* of the normal MediaWiki
  24. * environment and cannot depend upon any MediaWiki
  25. * code.
  26. */
  27. class ComposerPhpunitXmlCoverageEdit {
  28. public static function onEvent( $event ) {
  29. $IP = dirname( dirname( __DIR__ ) );
  30. // TODO: Support passing arbitrary directories for core (or extensions/skins).
  31. $args = $event->getArguments();
  32. if ( count( $args ) !== 1 ) {
  33. throw new InvalidArgumentException( 'Pass extensions/$extensionName as an argument, ' .
  34. 'e.g. "composer phpunit:coverage-edit -- extensions/BoilerPlate"' );
  35. }
  36. $project = current( $args );
  37. $phpunitXml = \PHPUnit\Util\Xml::loadFile( $IP . '/phpunit.xml.dist' );
  38. $whitelist = iterator_to_array( $phpunitXml->getElementsByTagName( 'whitelist' ) );
  39. /** @var DOMNode $childNode */
  40. foreach ( $whitelist as $childNode ) {
  41. $childNode->parentNode->removeChild( $childNode );
  42. }
  43. $whitelistElement = $phpunitXml->createElement( 'whitelist' );
  44. $whitelistElement->setAttribute( 'addUncoveredFilesFromWhitelist', 'false' );
  45. // TODO: Use AutoloadClasses from extension.json to load the relevant directories
  46. foreach ( [ 'includes', 'src', 'maintenance' ] as $dir ) {
  47. $dirElement = $phpunitXml->createElement( 'directory', $project . '/' . $dir );
  48. $dirElement->setAttribute( 'suffix', '.php' );
  49. $whitelistElement->appendChild( $dirElement );
  50. }
  51. $phpunitXml->getElementsByTagName( 'filter' )->item( 0 )
  52. ->appendChild( $whitelistElement );
  53. $phpunitXml->formatOutput = true;
  54. $phpunitXml->save( $IP . '/phpunit.xml' );
  55. }
  56. }