Jelajahi Sumber

Added root namespace to Autoloader constructor.

Taddeus Kroes 13 tahun lalu
induk
melakukan
fd39d5228f
2 mengubah file dengan 14 tambahan dan 5 penghapusan
  1. 6 4
      autoloader.php
  2. 8 1
      tests/test_autoloader.php

+ 6 - 4
autoloader.php

@@ -81,11 +81,13 @@ class Autoloader extends Base {
 	/**
 	 * Create a new Autoloader instance.
 	 * 
-	 * @param string $directory Root directory of the autoloader.
+	 * @param string $root_directory Root directory of the autoloader.
+	 * @param string $root_namespace Root namespace of classes loaded by the autoloader.
 	 * @param bool $throw Whether to throw an exception when a class file does not exist.
 	 */
-	function __construct($directory, $throw=false) {
-		$this->set_root_directory($directory);
+	function __construct($root_directory, $root_namespace='\\', $throw=false) {
+		$this->set_root_directory($root_directory);
+		$this->set_root_namespace($root_namespace);
 		$this->set_throw_errors($throw);
 	}
 	
@@ -128,7 +130,7 @@ class Autoloader extends Base {
 	/**
 	 * Set the root namespace that loaded classes are expected to be in.
 	 * 
-	 * @param string $directory The new root namespace.
+	 * @param string $namespace The new root namespace.
 	 */
 	function set_root_namespace($namespace) {
 		// Assert that the namespace ends with a backslash

+ 8 - 1
tests/test_autoloader.php

@@ -10,7 +10,6 @@ class AutoloaderTest extends PHPUnit_Framework_TestCase {
 		$this->autoloader = new Autoloader(PATH);
 	}
 	
-	
 	function test_set_root_namespace() {
 		$this->assertAttributeEquals('\\', 'root_namespace', $this->autoloader);
 		$this->autoloader->set_root_namespace('Foo');
@@ -27,6 +26,14 @@ class AutoloaderTest extends PHPUnit_Framework_TestCase {
 		$this->assertEquals($this->autoloader->get_root_namespace(), 'Foo\\');
 	}
 	
+	/**
+	 * @depends test_set_root_namespace
+	 */
+	function test_construct_root_namespace() {
+		$autoloader = new Autoloader(PATH, 'Foo');
+		$this->assertAttributeEquals('Foo\\', 'root_namespace', $autoloader);
+	}
+	
 	/**
 	 * @depends test_set_root_namespace
 	 */