Commit 9dfab03e authored by jwage's avatar jwage

[2.0] Small changes to ClassLoader and add basic unit test for it

parent 554adc32
...@@ -18,29 +18,52 @@ namespace Doctrine\Common; ...@@ -18,29 +18,52 @@ namespace Doctrine\Common;
* 4) Use an opcode-cache (i.e. APC) (STRONGLY RECOMMENDED). * 4) Use an opcode-cache (i.e. APC) (STRONGLY RECOMMENDED).
* *
* @since 2.0 * @since 2.0
* @author romanb <roman@code-factory.org> * @author Roman S. Borschel <roman@code-factory.org>
*/ */
class ClassLoader class ClassLoader
{ {
private $_namespaceSeparator = '\\'; private
private $_fileExtension = '.php'; $_namespaceSeparator = '\\',
private $_checkFileExists = false; $_fileExtension = '.php',
private $_basePaths = array(); $_checkFileExists = false,
$_basePaths = array();
/**
* Constructor registers the autoloader automatically
*/
public function __construct() public function __construct()
{ {
spl_autoload_register(array($this, 'loadClass'));
} }
/**
* Set check file exists
*
* @param boolean $bool
* @return void
*/
public function setCheckFileExists($bool) public function setCheckFileExists($bool)
{ {
$this->_checkFileExists = $bool; $this->_checkFileExists = $bool;
} }
/**
* Set class file extension
*
* @param string $extension
* @return void
*/
public function setClassFileExtension($extension) public function setClassFileExtension($extension)
{ {
$this->_fileExtension = $extension; $this->_fileExtension = $extension;
} }
/**
* Set namespace separator
*
* @param string $separator
* @return void
*/
public function setNamespaceSeparator($separator) public function setNamespaceSeparator($separator)
{ {
$this->_namespaceSeparator = $separator; $this->_namespaceSeparator = $separator;
...@@ -50,6 +73,7 @@ class ClassLoader ...@@ -50,6 +73,7 @@ class ClassLoader
* Sets a static base path for classes with a certain prefix that is prepended * Sets a static base path for classes with a certain prefix that is prepended
* to the path derived from the class itself. * to the path derived from the class itself.
* *
* @param string $classPrefix
* @param string $basePath * @param string $basePath
*/ */
public function setBasePath($classPrefix, $basePath) public function setBasePath($classPrefix, $basePath)
...@@ -89,17 +113,4 @@ class ClassLoader ...@@ -89,17 +113,4 @@ class ClassLoader
return true; 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
...@@ -22,6 +22,7 @@ class AllTests ...@@ -22,6 +22,7 @@ class AllTests
$suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Common Tests'); $suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Common Tests');
$suite->addTestSuite('Doctrine\Tests\Common\EventManagerTest'); $suite->addTestSuite('Doctrine\Tests\Common\EventManagerTest');
$suite->addTestSuite('Doctrine\Tests\Common\ClassLoaderTest');
$suite->addTest(Collections\AllTests::suite()); $suite->addTest(Collections\AllTests::suite());
......
<?php
namespace Doctrine\Tests\Common;
use Doctrine\Common\ClassLoader;
class ClassLoaderTest extends \Doctrine\Tests\DoctrineTestCase
{
public function testCustomFileExtensionAndNamespaceSeparator()
{
$classLoader = new \Doctrine\Common\ClassLoader();
$classLoader->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
<?php
class ClassLoaderTest_ClassA
{
}
\ No newline at end of file
<?php
class ClassLoaderTest_ClassB
{
}
\ No newline at end of file
...@@ -9,9 +9,6 @@ require_once 'PHPUnit/TextUI/TestRunner.php'; ...@@ -9,9 +9,6 @@ require_once 'PHPUnit/TextUI/TestRunner.php';
require_once '../lib/Doctrine/Common/ClassLoader.php'; require_once '../lib/Doctrine/Common/ClassLoader.php';
$classLoader = new \Doctrine\Common\ClassLoader(); $classLoader = new \Doctrine\Common\ClassLoader();
// checking for existance should not be necessary, remove as soon as possible
//$classLoader->setCheckFileExists(true);
$classLoader->register();
$modelDir = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'models'; $modelDir = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'models';
set_include_path( set_include_path(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment