Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ComposerPhpunitXmlCoverageEdit
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 1
 onEvent
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
20
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
19namespace MediaWiki\Composer;
20
21use Composer\Script\Event;
22use DOMDocument;
23use DOMNode;
24use InvalidArgumentException;
25
26/**
27 * Edit phpunit.xml to speed up code coverage generation.
28 *
29 * Usage: composer phpunit:coverage-edit -- extensions/ExtensionName
30 *
31 * This class runs *outside* of the normal MediaWiki
32 * environment and cannot depend upon any MediaWiki
33 * code.
34 */
35class ComposerPhpunitXmlCoverageEdit {
36    /**
37     * @param Event $event
38     */
39    public static function onEvent( $event ) {
40        $IP = dirname( dirname( __DIR__ ) );
41        // TODO: Support passing arbitrary directories for core (or extensions/skins).
42        $args = $event->getArguments();
43        if ( count( $args ) !== 1 ) {
44            throw new InvalidArgumentException( 'Pass extensions/$extensionName as an argument, ' .
45                'e.g. "composer phpunit:coverage-edit -- extensions/BoilerPlate"' );
46        }
47        $project = current( $args );
48        $phpunitXml = new DomDocument();
49        $phpunitXml->load( $IP . '/phpunit.xml.dist' );
50        $include = iterator_to_array( $phpunitXml->getElementsByTagName( 'include' ) );
51        /** @var DOMNode $childNode */
52        foreach ( $include as $childNode ) {
53            $childNode->parentNode->removeChild( $childNode );
54        }
55        $whitelistElement = $phpunitXml->createElement( 'include' );
56        // TODO: Use AutoloadClasses from extension.json to load the relevant directories
57        foreach ( [ 'includes', 'src', 'maintenance' ] as $dir ) {
58            $dirElement = $phpunitXml->createElement( 'directory', $project . '/' . $dir );
59            $dirElement->setAttribute( 'suffix', '.php' );
60            $whitelistElement->appendChild( $dirElement );
61
62        }
63        $phpunitXml->getElementsByTagName( 'coverage' )->item( 0 )
64            ->appendChild( $whitelistElement );
65        $phpunitXml->formatOutput = true;
66        $phpunitXml->save( $IP . '/phpunit.xml' );
67    }
68}