From 9dfab03ee0c5732541ee06534452a6afa1612518 Mon Sep 17 00:00:00 2001 From: jwage Date: Tue, 17 Feb 2009 22:30:13 +0000 Subject: [PATCH] [2.0] Small changes to ClassLoader and add basic unit test for it --- lib/Doctrine/Common/ClassLoader.php | 67 +++++++++++-------- tests/Doctrine/Tests/Common/AllTests.php | 1 + .../Doctrine/Tests/Common/ClassLoaderTest.php | 29 ++++++++ .../Common/ClassLoaderTest/ClassA.class.php | 5 ++ .../Common/ClassLoaderTest/ClassB.class.php | 5 ++ tests/Doctrine/Tests/TestInit.php | 3 - 6 files changed, 79 insertions(+), 31 deletions(-) create mode 100644 tests/Doctrine/Tests/Common/ClassLoaderTest.php create mode 100644 tests/Doctrine/Tests/Common/ClassLoaderTest/ClassA.class.php create mode 100644 tests/Doctrine/Tests/Common/ClassLoaderTest/ClassB.class.php diff --git a/lib/Doctrine/Common/ClassLoader.php b/lib/Doctrine/Common/ClassLoader.php index 5782c6d38..36546c191 100644 --- a/lib/Doctrine/Common/ClassLoader.php +++ b/lib/Doctrine/Common/ClassLoader.php @@ -16,47 +16,71 @@ namespace Doctrine\Common; * * 3) DO NOT setCheckFileExists(true). Doing so is expensive in terms of performance. * 4) Use an opcode-cache (i.e. APC) (STRONGLY RECOMMENDED). - * + * * @since 2.0 - * @author romanb + * @author Roman S. Borschel */ class ClassLoader -{ - private $_namespaceSeparator = '\\'; - private $_fileExtension = '.php'; - private $_checkFileExists = false; - private $_basePaths = array(); - +{ + private + $_namespaceSeparator = '\\', + $_fileExtension = '.php', + $_checkFileExists = false, + $_basePaths = array(); + + /** + * Constructor registers the autoloader automatically + */ public function __construct() { + spl_autoload_register(array($this, 'loadClass')); } - + + /** + * Set check file exists + * + * @param boolean $bool + * @return void + */ public function setCheckFileExists($bool) { $this->_checkFileExists = $bool; } - + + /** + * Set class file extension + * + * @param string $extension + * @return void + */ public function setClassFileExtension($extension) { $this->_fileExtension = $extension; } - + + /** + * Set namespace separator + * + * @param string $separator + * @return void + */ public function setNamespaceSeparator($separator) { $this->_namespaceSeparator = $separator; } - + /** * Sets a static base path for classes with a certain prefix that is prepended * to the path derived from the class itself. * + * @param string $classPrefix * @param string $basePath */ public function setBasePath($classPrefix, $basePath) { $this->_basePaths[$classPrefix] = $basePath; } - + /** * Loads the given class or interface. * @@ -86,20 +110,7 @@ class ClassLoader } require $class; - + return true; } - - /** - * Registers this class loader using spl_autoload_register(). - * - * @return void - */ - public function register() - { - spl_autoload_register(array($this, 'loadClass')); - } -} - - -?> \ No newline at end of file +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Common/AllTests.php b/tests/Doctrine/Tests/Common/AllTests.php index 52f1886d5..96328a1b2 100644 --- a/tests/Doctrine/Tests/Common/AllTests.php +++ b/tests/Doctrine/Tests/Common/AllTests.php @@ -22,6 +22,7 @@ class AllTests $suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Common Tests'); $suite->addTestSuite('Doctrine\Tests\Common\EventManagerTest'); + $suite->addTestSuite('Doctrine\Tests\Common\ClassLoaderTest'); $suite->addTest(Collections\AllTests::suite()); diff --git a/tests/Doctrine/Tests/Common/ClassLoaderTest.php b/tests/Doctrine/Tests/Common/ClassLoaderTest.php new file mode 100644 index 000000000..ac3b2b361 --- /dev/null +++ b/tests/Doctrine/Tests/Common/ClassLoaderTest.php @@ -0,0 +1,29 @@ +setBasePath('ClassLoaderTest', __DIR__); + $classLoader->setClassFileExtension('.class.php'); + $classLoader->setNamespaceSeparator('_'); + + $this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassA'), true); + $this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassB'), true); + } + + public function testClassLoaderCheckFileExists() + { + $classLoader = new \Doctrine\Common\ClassLoader(); + $classLoader->setBasePath('ClassLoaderTest', __DIR__); + $classLoader->setCheckFileExists(true); + + // This would return a fatal error without check file exists true + $this->assertEquals($classLoader->loadClass('SomeInvalidClass'), false); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Common/ClassLoaderTest/ClassA.class.php b/tests/Doctrine/Tests/Common/ClassLoaderTest/ClassA.class.php new file mode 100644 index 000000000..43811074c --- /dev/null +++ b/tests/Doctrine/Tests/Common/ClassLoaderTest/ClassA.class.php @@ -0,0 +1,5 @@ +setCheckFileExists(true); -$classLoader->register(); $modelDir = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'models'; set_include_path(