Allow use of phpunit.phar with MediaWiki's phpunit.php test entry point
authoraddshore <addshorewiki@gmail.com>
Mon, 5 May 2014 16:40:54 +0000 (17:40 +0100)
committerOri.livneh <ori@wikimedia.org>
Sat, 2 Aug 2014 21:41:38 +0000 (21:41 +0000)
MediaWiki tends to assume installation of phpunit via PEAR.
Distribution of phpunit via PEAR will eventually be discontinued,
in favor of distribution via PHAR or composer. PEAR installation
of phpunit 4.0 essentially distributes a phar.

https://github.com/sebastianbergmann/phpunit/wiki/Release-Announcement-for-PHPUnit-4.0.0

This patch allows phpunit.php to work with a phpunit.phar file,
as long as it's in the include path.  --with-phpunitdir can be used
to add a directory to the include path.

With composer install, no options are needed and it works.

Bug: 58881
Change-Id: Ibac9b36bbb9614d990c697a091947a64c8987d1d

.gitignore
tests/phpunit/phpunit.php

index 0351cf5..93c429f 100644 (file)
@@ -41,6 +41,7 @@ sftp-config.json
 
 # Building & testing
 node_modules/
+/tests/phpunit/phpunit.phar
 
 # Composer
 /vendor
index d323188..7dd932f 100755 (executable)
@@ -103,17 +103,12 @@ class PHPUnitMaintClass extends Maintenance {
                }
 
                # --with-phpunitdir let us override the default PHPUnit version
+               # Can use with either or phpunit.phar in the directory or the
+               # full PHPUnit code base.
                if ( $this->hasOption( 'with-phpunitdir' ) ) {
                        $phpunitDir = $this->getOption( 'with-phpunitdir' );
-                       # Sanity checks
-                       if ( !is_dir( $phpunitDir ) ) {
-                               $this->error( "--with-phpunitdir should be set to an existing directory", 1 );
-                       }
-                       if ( !is_readable( $phpunitDir . "/PHPUnit/Runner/Version.php" ) ) {
-                               $this->error( "No usable PHPUnit installation in $phpunitDir.\nAborting.\n", 1 );
-                       }
 
-                       # Now prepends provided PHPUnit directory
+                       # prepends provided PHPUnit directory or phar
                        $this->output( "Will attempt loading PHPUnit from `$phpunitDir`\n" );
                        set_include_path( $phpunitDir . PATH_SEPARATOR . get_include_path() );
 
@@ -203,18 +198,12 @@ class PHPUnitMaintClass extends Maintenance {
 $maintClass = 'PHPUnitMaintClass';
 require RUN_MAINTENANCE_IF_MAIN;
 
-if ( !class_exists( 'PHPUnit_Runner_Version' ) ) {
-       require_once 'PHPUnit/Runner/Version.php';
-}
-
-if ( PHPUnit_Runner_Version::id() !== '@package_version@'
-       && version_compare( PHPUnit_Runner_Version::id(), '3.7.0', '<' )
-) {
-       die( 'PHPUnit 3.7.0 or later required, you have ' . PHPUnit_Runner_Version::id() . ".\n" );
-}
+$pharFile = stream_resolve_include_path( 'phpunit.phar' );
+$isValidPhar = Phar::isValidPharFilename( $pharFile );
 
-if ( !class_exists( 'PHPUnit_TextUI_Command' ) ) {
-       require_once 'PHPUnit/Autoload.php';
+if ( !$isValidPhar && !class_exists( 'PHPUnit_Runner_Version' ) ) {
+       // try loading phpunit via PEAR
+       require_once 'PHPUnit/Runner/Version.php';
 }
 
 // Prevent segfault when we have lots of unit tests (bug 62623)
@@ -227,4 +216,18 @@ if ( version_compare( PHP_VERSION, '5.4.0', '<' )
        } );
 }
 
-PHPUnit_TextUI_Command::main();
+if ( $isValidPhar ) {
+       require $pharFile;
+} else {
+       if ( PHPUnit_Runner_Version::id() !== '@package_version@'
+           && version_compare( PHPUnit_Runner_Version::id(), '3.7.0', '<' )
+       ) {
+           die( 'PHPUnit 3.7.0 or later required, you have ' . PHPUnit_Runner_Version::id() . ".\n" );
+       }
+
+       if ( !class_exists( 'PHPUnit_TextUI_Command' ) ) {
+           require_once 'PHPUnit/Autoload.php';
+       }
+
+       PHPUnit_TextUI_Command::main();
+}