Extensions may add tests by directory
authorAdam Roses Wight <awight@wikimedia.org>
Sun, 28 Sep 2014 21:32:55 +0000 (14:32 -0700)
committerHashar <hashar@free.fr>
Tue, 21 Oct 2014 09:37:10 +0000 (09:37 +0000)
The UnitTestsList hook can now be used to add entire directories of
tests, à la phpunit.xml's <directory> tag.  The test suite is built by
recursively scanning the directory for any files ending in "Test.php".

TODO:
* Update online hook documentation.
* Generate and autoload a classmap for scanned directories.

Bug: 70630
Change-Id: I3089372f9d7c645e16ff0984a959f982a3bc639f

RELEASE-NOTES-1.24
docs/hooks.txt
tests/phpunit/suites/ExtensionsTestSuite.php

index 2b5136e..03cf277 100644 (file)
@@ -505,6 +505,9 @@ changes to languages because of Bugzilla reports.
 * Removed maintenance/purgeOldText.inc and the PurgeRedundantText() function
   it contained (superseded by Maintenance::purgeRedundantText() in 1.16).
   The purgeOldText.php maintenance script has been retained.
+* PHPUnit tests can be found by directory discovery, by adding the directory
+  path from your UnitTestsList callback. Older versions of MediaWiki core will
+  barf at this usage.
 
 ==== Renamed classes ====
 * CLDRPluralRuleConverter_Expression to CLDRPluralRuleConverterExpression
index c60cc76..6fb10b1 100644 (file)
@@ -2729,8 +2729,10 @@ actions).
 $action: action name
 $article: article "acted on"
 
-'UnitTestsList': Called when building a list of files with PHPUnit tests.
-&$files: list of files
+'UnitTestsList': Called when building a list of paths containing PHPUnit tests.
+Since 1.24: Paths pointing to a directory will be recursively scanned for
+test case files matching the suffix "Test.php".
+&$paths: list of test cases and directories to search.
 
 'UnwatchArticle': Before a watch is removed from an article.
 $user: user watching
index 4d24d9d..116065f 100644 (file)
@@ -8,12 +8,25 @@
 class ExtensionsTestSuite extends PHPUnit_Framework_TestSuite {
        public function __construct() {
                parent::__construct();
-               $files = array();
-               wfRunHooks( 'UnitTestsList', array( &$files ) );
-               foreach ( $files as $file ) {
-                       $this->addTestFile( $file );
+               $paths = array();
+               // Extensions can return a list of files or directories
+               wfRunHooks( 'UnitTestsList', array( &$paths ) );
+               foreach ( $paths as $path ) {
+                       if ( is_dir( $path ) ) {
+                               // If the path is a directory, search for test cases.
+                               // @since 1.24
+                               $suffixes = array(
+                                       'Test.php',
+                               );
+                               $fileIterator = new File_Iterator_Facade();
+                               $matchingFiles = $fileIterator->getFilesAsArray( $path, $suffixes );
+                               $this->addTestFiles( $matchingFiles );
+                       } else {
+                               // Add a single test case or suite class
+                               $this->addTestFile( $path );
+                       }
                }
-               if ( !count( $files ) ) {
+               if ( !count( $paths ) ) {
                        $this->addTest( new DummyExtensionsTest( 'testNothing' ) );
                }
        }