FileRepo: check constructor parameters
authorAntoine Musso <hashar@users.mediawiki.org>
Mon, 23 Jan 2012 14:50:54 +0000 (14:50 +0000)
committerAntoine Musso <hashar@users.mediawiki.org>
Mon, 23 Jan 2012 14:50:54 +0000 (14:50 +0000)
new FileRepo() requires an array of parameters having at least the
'name' and 'backend' key setup.

TODO: 'backend' keyword should probably default to FileBackend.

includes/filerepo/FileRepo.php
tests/phpunit/includes/filerepo/FileRepoTest.php [new file with mode: 0644]

index e7608ca..c9e5e7f 100644 (file)
@@ -39,7 +39,16 @@ class FileRepo {
        var $oldFileFactory = false;
        var $fileFactoryKey = false, $oldFileFactoryKey = false;
 
-       function __construct( $info ) {
+       function __construct( Array $info = null ) {
+               // Verify required settings presence
+               if(
+                       $info === null
+                       || !array_key_exists( 'name', $info )
+                       || !array_key_exists( 'backend', $info )
+               ) {
+                       throw new MWException( __CLASS__ . " requires an array of options having both 'name' and 'backend' keys.\n" );
+               }
+
                // Required settings
                $this->name = $info['name'];
                if ( $info['backend'] instanceof FileBackendBase ) {
diff --git a/tests/phpunit/includes/filerepo/FileRepoTest.php b/tests/phpunit/includes/filerepo/FileRepoTest.php
new file mode 100644 (file)
index 0000000..ca34442
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+
+class FileRepoTest extends MediaWikiTestCase {
+
+       /**
+        * @expectedException MWException
+        */
+       function testFileRepoConstructionOptionCanNotBeNull() {
+               $f = new FileRepo();
+       }
+       /**
+        * @expectedException MWException
+        */
+       function testFileRepoConstructionOptionCanNotBeAnEmptyArray() {
+               $f = new FileRepo( array() );
+       }
+       /**
+        * @expectedException MWException
+        */
+       function testFileRepoConstructionOptionNeedNameKey() {
+               $f = new FileRepo( array(
+                       'backend' => 'foobar'
+               ) );
+       }
+       /**
+        * @expectedException MWException
+        */
+       function testFileRepoConstructionOptionNeedBackendKey() {
+               $f = new FileRepo( array(
+                       'name' => 'foobar'
+               ) );
+       }
+
+}