Commit 62641b42 authored by Taddeus Kroes's avatar Taddeus Kroes

Simplified Autoloader class into an easy-to-use singleton

parent c7d4d826
This diff is collapsed.
...@@ -63,7 +63,7 @@ abstract class Base { ...@@ -63,7 +63,7 @@ abstract class Base {
* @return string * @return string
*/ */
static function pathWithSlash($directory) { static function pathWithSlash($directory) {
return $directory[strlen($directory) - 1] == '/' ? $directory : $directory.'/'; return $directory[strlen($directory) - 1] == '/' ? $directory : $directory . '/';
} }
} }
......
<?php <?php
require_once 'SingletonTestCase.php';
require_once 'autoloader.php'; require_once 'autoloader.php';
use webbasics\Autoloader; use webbasics\Autoloader;
define('PATH', 'tests/_files/'); define('PATH', 'tests/_files/');
class AutoloaderTest extends PHPUnit_Framework_TestCase { class AutoloaderTest extends SingletonTestCase {
function setUp() { function getClassName() {
$this->autoloader = new Autoloader(PATH); return 'webbasics\Autoloader';
} }
function testSetRootNamespace() { function tearDown() {
$this->assertAttributeEquals('\\', 'root_namespace', $this->autoloader); Autoloader::getInstance()->setThrowExceptions(false);
$this->autoloader->setRootNamespace('Foo');
$this->assertAttributeEquals('Foo\\', 'root_namespace', $this->autoloader);
$this->autoloader->setRootNamespace('Foo\\');
$this->assertAttributeEquals('Foo\\', 'root_namespace', $this->autoloader);
} }
/** function testStripNamespace() {
* @depends testSetRootNamespace $rmethod = new ReflectionMethod('webbasics\Autoloader', 'stripNamespace');
*/ $rmethod->setAccessible(true);
function testGetRootNamespace() { $this->assertEquals('Bar', $rmethod->invoke(null, 'foo', 'foo\Bar'));
$this->autoloader->setRootNamespace('Foo'); $this->assertEquals('Bar', $rmethod->invoke(null, '\foo', '\foo\Bar'));
$this->assertEquals($this->autoloader->getRootNamespace(), 'Foo\\');
}
/**
* @depends testSetRootNamespace
*/
function testConstructRootNamespace() {
$autoloader = new Autoloader(PATH, 'Foo');
$this->assertAttributeEquals('Foo\\', 'root_namespace', $autoloader);
} }
/** function testAddDirectory() {
* @depends testSetRootNamespace $autoloader = Autoloader::getInstance();
*/ $rprop = new ReflectionProperty($autoloader, 'directories');
function testStripRootNamespace() { $rprop->setAccessible(true);
$strip = new ReflectionMethod('webbasics\Autoloader', 'stripRootNamespace');
$strip->setAccessible(true);
$this->autoloader->setRootNamespace('Foo'); $autoloader->addDirectory(PATH);
$this->assertEquals($strip->invoke($this->autoloader, 'Foo\Bar'), 'Bar'); $this->assertEquals(array(
} '\\' => array(PATH)
), $rprop->getValue($autoloader));
function testSetRootDirectory() {
$this->autoloader->setRootDirectory('tests'); $autoloader->addDirectory(PATH);
$this->assertEquals($this->autoloader->getRootDirectory(), 'tests/'); $this->assertEquals(array(
} '\\' => array(PATH)
), $rprop->getValue($autoloader));
function testClassnameToFilename() {
$this->assertEquals(Autoloader::classnameToFilename('Foo'), 'foo'); $autoloader->addDirectory('foo');
$this->assertEquals(Autoloader::classnameToFilename('FooBar'), 'foo_bar'); $this->assertEquals(array(
$this->assertEquals(Autoloader::classnameToFilename('fooBar'), 'foo_bar'); '\\' => array(PATH, 'foo/')
$this->assertEquals(Autoloader::classnameToFilename('FooBarBaz'), 'foo_bar_baz'); ), $rprop->getValue($autoloader));
$autoloader->addDirectory('bar');
$this->assertEquals(array(
'\\' => array(PATH, 'foo/', 'bar/')
), $rprop->getValue($autoloader));
$autoloader->addDirectory('foodir', 'foo');
$this->assertEquals(array(
'\\' => array(PATH, 'foo/', 'bar/'),
'\foo' => array('foodir/')
), $rprop->getValue($autoloader));
$autoloader->addDirectory('foobardir', 'foobar');
$this->assertEquals(array(
'\\' => array(PATH, 'foo/', 'bar/'),
'\foo' => array('foodir/'),
'\foobar' => array('foobardir/')
), $rprop->getValue($autoloader));
} }
/**
* @depends testClassnameToFilename
*/
function testCreatePath() { function testCreatePath() {
$this->assertEquals($this->autoloader->createPath('Foo'), PATH.'foo.php'); $rmethod = new ReflectionMethod('webbasics\Autoloader', 'createPath');
$this->assertEquals($this->autoloader->createPath('\Foo'), PATH.'foo.php'); $rmethod->setAccessible(true);
$this->assertEquals($this->autoloader->createPath('Foo\Bar'), PATH.'foo/bar.php'); $this->assertEquals($rmethod->invoke(null, 'Foo'), 'Foo.php');
$this->assertEquals($this->autoloader->createPath('Foo\Bar\Baz'), PATH.'foo/bar/baz.php'); $this->assertEquals($rmethod->invoke(null, '\Foo'), 'Foo.php');
$this->assertEquals($this->autoloader->createPath('FooBar\Baz'), PATH.'foo_bar/baz.php'); $this->assertEquals($rmethod->invoke(null, 'foo\Bar'), 'foo/Bar.php');
$this->assertEquals($rmethod->invoke(null, 'foo\Bar\Baz'), 'foo/Bar/Baz.php');
$this->assertEquals($rmethod->invoke(null, 'fooBar\Baz'), 'fooBar/Baz.php');
$this->assertEquals($rmethod->invoke(null, 'foo_bar\Baz'), 'foo_bar/Baz.php');
} }
function testThrowErrors() { function testThrowExceptions() {
$this->assertFalse($this->autoloader->getThrowErrors()); $autoloader = Autoloader::getInstance();
$this->autoloader->setThrowErrors(true); $this->assertFalse($autoloader->getThrowExceptions());
$this->assertTrue($this->autoloader->getThrowErrors()); $autoloader->setThrowExceptions(true);
$this->assertTrue($autoloader->getThrowExceptions());
} }
/** /**
* @depends testCreatePath * @depends testCreatePath
* @depends testThrowErrors * @depends testThrowExceptions
*/ */
function testLoadClassNotFound() { function testLoadClassNotFound() {
$this->assertFalse($this->autoloader->loadClass('foobar')); $this->assertFalse(Autoloader::getInstance()->loadClass('foobar'));
}
/**
* @depends testLoadClassNotFound
* @expectedException webbasics\FileNotFoundError
* @expectedExceptionMessage File "tests/_files/foobar.php" does not exist.
*/
function testLoadClassNotFoundError() {
$this->autoloader->setThrowErrors(true);
$this->autoloader->loadClass('foobar');
} }
/** /**
* @depends testLoadClassNotFound * @depends testLoadClassNotFound
* @expectedException webbasics\FileNotFoundError * @expectedException webbasics\ClassNotFoundError
* @expectedExceptionMessage File "tests/_files/foobar.php" does not exist.
*/ */
function testLoadClassNotFoundNoerrorOverwrite() { function testLoadClassNotFoundException() {
$this->autoloader->loadClass('foobar', true); $autoloader = Autoloader::getInstance();
$autoloader->setThrowExceptions(true);
$autoloader->loadClass('foobar');
} }
/** /**
* @depends testLoadClassNotFound * @depends testLoadClassNotFound
*/ */
function testLoadClassNotFoundErrorOverwrite() { function testLoadClassSuccess() {
$this->autoloader->setThrowErrors(true); $autoloader = Autoloader::getInstance();
$this->assertFalse($this->autoloader->loadClass('foobar', false)); $this->assertTrue($autoloader->loadClass('Foo'));
}
/**
* @depends testLoadClassNotFound
*/
function testLoadClass() {
$this->assertTrue($this->autoloader->loadClass('Foo'));
$this->assertTrue(class_exists('Foo', false)); $this->assertTrue(class_exists('Foo', false));
$this->assertTrue($this->autoloader->loadClass('Foo\Bar')); $this->assertTrue($autoloader->loadClass('Foo\Bar'));
$this->assertTrue(class_exists('Foo\Bar', false)); $this->assertTrue(class_exists('Foo\Bar', false));
} }
/**
* @depends testLoadClass
* @depends testStripRootNamespace
*/
function testLoadClassRootNamespace() {
$autoloader = new Autoloader(PATH.'foo');
$autoloader->setRootNamespace('Foo');
$this->assertTrue($autoloader->loadClass('Bar'));
$this->assertTrue(class_exists('Foo\Bar', false));
}
/**
* @depends testLoadClass
*/
function testRegister() {
$this->autoloader->register();
$this->assertTrue(class_exists('Baz'));
}
/**
* @depends testRegister
* @depends testThrowErrors
*/
function testRegisterPrepend() {
$second_loader = new Autoloader(PATH.'second');
$this->autoloader->register();
$second_loader->register(true); // Prepend so that the second loader attemps to load Bar first
$this->assertInstanceOf('Foo', new FooBaz());
}
} }
?> ?>
\ No newline at end of file
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