Seb35 has submitted this change and it was merged.

Change subject: Added experimental support for PHPUnit
......................................................................


Added experimental support for PHPUnit

Given this extension is quite unusual, a very specific installation must be 
used.
It is detailled in docs/internals.rst.
For now there are only basic tests.

Change-Id: Ie02c51fdd3cefd546f1d68206b700a748d27e142
---
M docs/config/farms.php
M docs/config/farms.yml
A docs/internals.rst
M extension.json
M src/MediaWikiFarm.php
A tests/phpunit/MediaWikiFarmTest.php
A tests/phpunit/data/config/farms.php
7 files changed, 132 insertions(+), 9 deletions(-)

Approvals:
  Seb35: Verified; Looks good to me, approved



diff --git a/docs/config/farms.php b/docs/config/farms.php
index aa4140b..114ad86 100644
--- a/docs/config/farms.php
+++ b/docs/config/farms.php
@@ -22,7 +22,7 @@
        # Configuration similar to the Wikimedia farm
        'wikimedia' => array(
                
-               'server' => '(?<lang>[a-z-]+)\.(?<family>[a-z]+)\.org',
+               'server' => '(?P<lang>[a-z-]+)\.(?<family>[a-z]+)\.org',
                
                'variables' => array(
                
@@ -56,7 +56,7 @@
        # Configuration for a small wiki farm
        'com-example' => array(
                
-               'server' => '(?<client>[a-z]+)-(?<wiki>[a-z]+)\.example\.com',
+               'server' => '(?P<client>[a-z]+)-(?<wiki>[a-z]+)\.example\.com',
                
                'variables' => array(
                        
@@ -90,7 +90,7 @@
        
        'com-example-redirect' => array(
                
-               'server' => '(?<client>[a-z]+)_(?<wiki>[a-z]+)\.example\.com',
+               'server' => '(?P<client>[a-z]+)_(?<wiki>[a-z]+)\.example\.com',
                'redirect' => '$client-$wiki.example.com',
        ),
 );
diff --git a/docs/config/farms.yml b/docs/config/farms.yml
index 301cc97..f2ba1ba 100644
--- a/docs/config/farms.yml
+++ b/docs/config/farms.yml
@@ -12,7 +12,7 @@
 ## Configuration similar to the Wikimedia farm
 #wikimedia:
 #    
-#    server: '(?<lang>[a-z-]+)\.(?<family>[a-z]+)\.org'
+#    server: '(?P<lang>[a-z-]+)\.(?<family>[a-z]+)\.org'
 #    
 #    variables:
 #        
@@ -39,7 +39,7 @@
 ## Configuration for a small wiki farm
 #com-example:
 #    
-#    server: '(?<client>[a-z]+)-(?<wiki>[a-z]+)\.example\.com'
+#    server: '(?P<client>[a-z]+)-(?<wiki>[a-z]+)\.example\.com'
 #    
 #    variables:
 #        
@@ -66,5 +66,5 @@
 #
 #com-example-redirect:
 #    
-#    server: '(?<client>[a-z]+)_(?<wiki>[a-z]+)\.example\.com'
+#    server: '(?P<client>[a-z]+)_(?<wiki>[a-z]+)\.example\.com'
 #    redirect: '$client-$wiki.example.com'
diff --git a/docs/internals.rst b/docs/internals.rst
new file mode 100644
index 0000000..a4ca3a8
--- /dev/null
+++ b/docs/internals.rst
@@ -0,0 +1,24 @@
+*********
+Internals
+*********
+
+Overview
+========
+
+Most of the code is inside the class MediaWikiFarm, except the code used to 
load the global configuration parameters inside the global scope.
+
+The class MediaWikiFarm is in charge of loading the farm configuration, 
managing the configuration cache, selectionning the requested farm and wiki, 
compiling the configuration for a given wiki. The executable file 
:path:`src/main.php` is the strict equivalent of LocalSettings.php: it loads 
the configuration parameters inside the global scope.
+
+There are a lot of various configurations, depending on the mono- or 
multi-version installation, on the PHP version, on the MediaWiki version, and 
on the PHP SAPI used (Web or CLI). Various flags and :code:`require_once` are 
used to select the right files and functions to load the configuration. The 
main difficulty is to correctly handle the CLI case, mainly because MediaWiki 
is mostly designed to have only one wiki per installation.
+
+Testing
+=======
+
+MediaWikiFarm has an experimental support of unit tests with `PHPUnit`_.
+
+Because of the very specific use case of PHPUnit called in CLI, it is 
recommended to set up a classical wiki installation with a classical 
LocalSettings.php with its own database and (fakely) load MediaWikiFarm with 
:code:`wfLoadExtension( 'MediaWikiFarm' )`. In fact this will not load the 
MediaWikiFarm executable code due to a lack of of support of extension 
registration (difficult problem in this case), so this will only load the class 
MediaWikiFarm needed to execute unit tests.
+
+Then, to execute PHPUnit, go to root MediaWiki directory and run :command:`php 
tests/phpunit/phpunit.php --group MediaWikiFarm`. You can add 
:command:`--debug` if you want more details.
+
+.. _PHPUnit: http://www.phpunit.de
+
diff --git a/extension.json b/extension.json
index 0bc62e5..15f80cf 100644
--- a/extension.json
+++ b/extension.json
@@ -21,6 +21,11 @@
                        "i18n"
                ]
        },
+       "Hooks": {
+               "UnitTestsList": [
+                       "MediaWikiFarm::onUnitTestsList"
+               ]
+       },
        "AutoloadClasses": {
                "MediaWikiFarm": "src/MediaWikiFarm.php"
        },
diff --git a/src/MediaWikiFarm.php b/src/MediaWikiFarm.php
index e8d8de7..cba4ccf 100644
--- a/src/MediaWikiFarm.php
+++ b/src/MediaWikiFarm.php
@@ -165,6 +165,7 @@
                        return;
                
                if( !array_key_exists( 'globals', $this->params ) || !is_array( 
$this->params['globals'] ) )
+                       
                        $this->getMediaWikiConfig();
                
                // Set general parameters as global variables
@@ -192,7 +193,7 @@
                foreach( $this->params['globals']['skins'] as $skin => $value ) 
{
                        
                        if( $value['_loading'] == 'wfLoadSkin' )
-                       
+                               
                                wfLoadSkin( $skin );
                        
                        unset( 
$this->params['globals']['skins'][$skin]['_loading'] );
@@ -329,17 +330,21 @@
         * farm depending of the host (when there are multiple farms). In case 
of error (unreadable
         * directory or file, or unrecognized host), no exception is thrown but 
the property
         * 'unusable' becomes true.
+        * It is a public method for testing needs, but it should never 
directly called in real code.
         * 
         * @param string|null $host Requested host.
         * @param string $configDir Configuration directory.
         * @param string|null $codeDir Code directory; if null, the current 
MediaWiki installation is used.
         * @param string|false|null $cacheDir Cache directory; if null, the 
cache is disabled.
         */
-       private function __construct( $host, $configDir, $codeDir = null, 
$cacheDir = null ) {
+       public function __construct( $host, $configDir, $codeDir = null, 
$cacheDir = null ) {
                
                # Default value for $host
                # Warning: do not use $GLOBALS['_SERVER']['HTTP_HOST']: bug 
with PHP7: it is not initialised in early times of a script
-               if( is_null( $host ) ) $host = $_SERVER['HTTP_HOST'] ? 
$_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
+               if( is_null( $host ) ) {
+                       if( array_key_exists( 'HTTP_HOST', $_SERVER ) || 
array_key_exists( 'SERVER_NAME', $_SERVER ) )
+                               $host = $_SERVER['HTTP_HOST'] ? 
$_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
+               }
                
                # Default value for $cacheDir
                if( is_null( $cacheDir ) ) $cacheDir = '/tmp/mw-cache';
@@ -1181,4 +1186,18 @@
                
                return $out;
        }
+       
+       /**
+        * Add files for unit testing.
+        * 
+        * @param string[] $files The test files.
+        */
+       public static function onUnitTestsList( array &$files ) {
+               
+               $dir = dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 
'tests' . DIRECTORY_SEPARATOR . 'phpunit' . DIRECTORY_SEPARATOR;
+               
+               $files[] = $dir . 'MediaWikiFarmTest.php';
+               
+               return true;
+       }
 }
diff --git a/tests/phpunit/MediaWikiFarmTest.php 
b/tests/phpunit/MediaWikiFarmTest.php
new file mode 100644
index 0000000..3c03a0c
--- /dev/null
+++ b/tests/phpunit/MediaWikiFarmTest.php
@@ -0,0 +1,53 @@
+<?php
+
+/**
+ * @group MediaWikiFarm
+ * @covers MediaWikiFarm
+ */
+class MediaWikiFarmTest extends MediaWikiTestCase {
+       
+       /**
+        * Set up the default MediaWikiFarm object with a sample correct 
configuration file.
+        */
+       public function setUp() {
+               
+               parent::setUp();
+               
+               $this->setMwGlobals( 'wgMediaWikiFarmConfigDir', dirname( 
__FILE__ ) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'config' );
+               MediaWikiFarm::getInstance( 'a.testfarm.example.org' );
+       }
+       
+       /**
+        * Test when there is no configuration file farms.yml/json/php.
+        */
+       public function testFailedConstruction() {
+               
+               $wgMediaWikiFarmBadConfigDir = dirname( __FILE__ ) . 
DIRECTORY_SEPARATOR . 'data';
+               $wgMediaWikiFarm = new MediaWikiFarm( 'a.testfarm.example.org', 
$wgMediaWikiFarmBadConfigDir );
+               $this->assertTrue( $wgMediaWikiFarm->unusable );
+       }
+       
+       /**
+        * Test a successful initialisation of MediaWikiFarm with a correct 
configuration file farms.php.
+        */
+       public function testSuccessfulConstruction() {
+               
+               $this->assertFalse( MediaWikiFarm::getInstance()->unusable );
+       }
+       
+       /**
+        * Test the path of the executable file src/main.php is correct.
+        */
+       public function testFarmLocalSettingsFile() {
+               
+               $this->assertEquals( 
MediaWikiFarm::getInstance()->getConfigFile(), dirname( dirname( dirname( 
__FILE__ ) ) ) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'main.php' 
);
+       }
+       
+       /**
+        * Test the variable read for the URL is correct.
+        */
+       public function testVariables() {
+               
+               $this->assertEquals( MediaWikiFarm::getInstance()->variables, 
array( 'wiki' => 'a' ) );
+       }
+}
diff --git a/tests/phpunit/data/config/farms.php 
b/tests/phpunit/data/config/farms.php
new file mode 100644
index 0000000..8d0c57b
--- /dev/null
+++ b/tests/phpunit/data/config/farms.php
@@ -0,0 +1,22 @@
+<?php
+
+return array(
+       
+       'testfarm' => array(
+               
+               'server' => '(?P<wiki>[a-z])\.testfarm\.example\.org',
+               'suffix' => 'testfarm',
+               'wikiID' => '$wikitestfarm',
+               'config' => array(
+                       array( 'file' => 'LocalSettings.php',
+                              'exec' => true,
+                       ),
+               ),
+       ),
+       
+       'redirect-testfarm' => array(
+               
+               'server' => '(?P<wiki>[a-z])-redirect\.testfarm\.example\.org',
+               'redirect' => '$wiki.testfarm.example.org',
+       ),
+);

-- 
To view, visit https://gerrit.wikimedia.org/r/302110
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie02c51fdd3cefd546f1d68206b700a748d27e142
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MediaWikiFarm
Gerrit-Branch: master
Gerrit-Owner: Seb35 <seb35wikipe...@gmail.com>
Gerrit-Reviewer: Seb35 <seb35wikipe...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to