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