add more test

This commit is contained in:
fzerorubigd 2013-12-06 21:32:36 +03:30
parent fa65e99dfe
commit ff503a90a2
No known key found for this signature in database
GPG Key ID: D6EE858AF9D2999A
7 changed files with 57 additions and 7 deletions

View File

@ -77,16 +77,16 @@ class Autoloader
*/ */
public function autoload($class) public function autoload($class)
{ {
if ($class[0] === '\\') { if ($class[0] !== '\\') {
$class = substr($class, 1); $class = '\\' . $class;
} }
if (strpos($class, 'Handlebars') !== 0) { if (strpos($class, 'Handlebars') !== 1) {
return; return;
} }
$file = sprintf( $file = sprintf(
'%s/%s.php', '%s%s.php',
$this->_baseDir, $this->_baseDir,
str_replace('\\', '/', $class) str_replace('\\', '/', $class)
); );

View File

@ -20,6 +20,7 @@
*/ */
namespace Handlebars\Loader; namespace Handlebars\Loader;
use Handlebars\Loader; use Handlebars\Loader;
use Handlebars\String; use Handlebars\String;
@ -64,7 +65,8 @@ class FilesystemLoader implements Loader
} else { } else {
foreach ($baseDirs as &$dir) { foreach ($baseDirs as &$dir) {
$dir = rtrim(realpath($dir), '/'); $dir = rtrim(realpath($dir), '/');
} unset( $dir ); }
unset($dir);
} }
$this->_baseDir = $baseDirs; $this->_baseDir = $baseDirs;

View File

@ -19,6 +19,10 @@
*/ */
class HandlebarsTest extends \PHPUnit_Framework_TestCase class HandlebarsTest extends \PHPUnit_Framework_TestCase
{ {
public function setUp()
{
\Handlebars\Autoloader::register();
}
/** /**
* Test handlebars autoloader * Test handlebars autoloader
* *
@ -29,7 +33,10 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
Handlebars\Autoloader::register(realpath(__DIR__ . '/../fixture/')); Handlebars\Autoloader::register(realpath(__DIR__ . '/../fixture/'));
$this->assertTrue(class_exists('Handlebars\\Test')); $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->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); $engine->setLoader($loader);
$this->assertEquals('test', $engine->render('loader', array())); $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 * Test partial loader

View File

@ -1,4 +1,2 @@
<?php <?php
include __DIR__ . "/../src/Handlebars/Autoloader.php"; include __DIR__ . "/../src/Handlebars/Autoloader.php";
$base = __DIR__ . "/../src";
$loader = \Handlebars\Autoloader::register();

View File

@ -0,0 +1 @@
another_extra

View File

@ -0,0 +1 @@
another

View File

@ -0,0 +1 @@
test_extra