diff --git a/src/Handlebars/Autoloader.php b/src/Handlebars/Autoloader.php index 5223aed..fd22f05 100755 --- a/src/Handlebars/Autoloader.php +++ b/src/Handlebars/Autoloader.php @@ -77,16 +77,16 @@ class Autoloader */ public function autoload($class) { - if ($class[0] === '\\') { - $class = substr($class, 1); + if ($class[0] !== '\\') { + $class = '\\' . $class; } - if (strpos($class, 'Handlebars') !== 0) { + if (strpos($class, 'Handlebars') !== 1) { return; } $file = sprintf( - '%s/%s.php', + '%s%s.php', $this->_baseDir, str_replace('\\', '/', $class) ); diff --git a/src/Handlebars/Loader/FilesystemLoader.php b/src/Handlebars/Loader/FilesystemLoader.php index c4d7d70..0df119b 100755 --- a/src/Handlebars/Loader/FilesystemLoader.php +++ b/src/Handlebars/Loader/FilesystemLoader.php @@ -20,6 +20,7 @@ */ namespace Handlebars\Loader; + use Handlebars\Loader; use Handlebars\String; @@ -64,7 +65,8 @@ class FilesystemLoader implements Loader } else { foreach ($baseDirs as &$dir) { $dir = rtrim(realpath($dir), '/'); - } unset( $dir ); + } + unset($dir); } $this->_baseDir = $baseDirs; diff --git a/tests/Xamin/HandlebarsTest.php b/tests/Xamin/HandlebarsTest.php index a4ffced..efc5df2 100644 --- a/tests/Xamin/HandlebarsTest.php +++ b/tests/Xamin/HandlebarsTest.php @@ -19,6 +19,10 @@ */ class HandlebarsTest extends \PHPUnit_Framework_TestCase { + public function setUp() + { + \Handlebars\Autoloader::register(); + } /** * Test handlebars autoloader * @@ -29,7 +33,10 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase Handlebars\Autoloader::register(realpath(__DIR__ . '/../fixture/')); $this->assertTrue(class_exists('Handlebars\\Test')); + $this->assertTrue(class_exists('\\Handlebars\\Test')); $this->assertTrue(class_exists('Handlebars\\Example\\Test')); + $this->assertTrue(class_exists('\\Handlebars\\Example\\Test')); + $this->assertFalse(class_exists('\\Another\\Example\\Test')); } /** @@ -326,6 +333,46 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase $engine->setLoader($loader); $this->assertEquals('test', $engine->render('loader', array())); } + /** + * Test file system loader + */ + public function testFileSystemLoaderMultipleFolder() + { + $paths = array( + realpath(__DIR__ . '/../fixture/data'), + realpath(__DIR__ . '/../fixture/another') + ); + + $options = array( + 'prefix' => '__', + 'extension' => 'hb' + ); + $loader = new \Handlebars\Loader\FilesystemLoader($paths, $options); + $engine = new \Handlebars\Handlebars(); + $engine->setLoader($loader); + $this->assertEquals('test_extra', $engine->render('loader', array())); + $this->assertEquals('another_extra', $engine->render('another', array())); + } + + /** + * Test file system loader + * @expectedException \InvalidArgumentException + */ + public function testFileSystemLoaderNotFound() + { + $loader = new \Handlebars\Loader\FilesystemLoader(realpath(__DIR__ . '/../fixture/data')); + $engine = new \Handlebars\Handlebars(); + $engine->setLoader($loader); + $engine->render('invalid_file', array()); + } + /** + * Test file system loader + * @expectedException \RuntimeException + */ + public function testFileSystemLoaderInvalidFolder() + { + new \Handlebars\Loader\FilesystemLoader(realpath(__DIR__ . '/../fixture/') . 'invalid/path'); + } /** * Test partial loader diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 2f72e3d..ed69c96 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,4 +1,2 @@