diff --git a/src/Twig.php b/src/Twig.php index e275f66..c4de879 100644 --- a/src/Twig.php +++ b/src/Twig.php @@ -118,7 +118,11 @@ private function addPaths(array $paths) $loader = new \Twig_Loader_Filesystem(); foreach ($paths as $namespace => $path) { - $loader->addPath($path, $namespace); + if (is_string($namespace)) { + $loader->setPaths($path, $namespace); + } else { + $loader->addPath($path); + } } return $loader; diff --git a/tests/TwigTest.php b/tests/TwigTest.php index 296749a..79fcaf9 100644 --- a/tests/TwigTest.php +++ b/tests/TwigTest.php @@ -14,26 +14,65 @@ class TwigTest extends \PHPUnit_Framework_TestCase { - /** - * @var Twig - */ - protected $view; + public function testFetch() + { + $view = new Twig(dirname(__FILE__) . '/templates'); + + $output = $view->fetch('example.html', [ + 'name' => 'Josh' + ]); - public function setUp() + $this->assertEquals("

Hi, my name is Josh.

\n", $output); + } + + public function testSingleNamespaceAndMultipleDirectories() { - $this->view = new Twig(dirname(__FILE__) . '/templates'); + $weekday = (new \DateTimeImmutable('2016-03-08'))->format('l'); + + $view = new Twig( + [ + 'namespace' => [ + __DIR__.'/another', + __DIR__.'/templates', + __DIR__.'/multi', + ], + ] + ); + + $anotherDirectory = $view->fetch('@namespace/example.html', [ + 'name' => 'Peter' + ]); + + $templatesDirectory = $view->fetch('@namespace/another_example.html', [ + 'name' => 'Peter', + 'gender' => 'male', + ]); + + $outputMulti = $view->fetch('@namespace/directory/template/example.html', [ + 'weekday' => $weekday, + ]); + + $this->assertEquals("

Hi, my name is Peter.

\n", $anotherDirectory); + $this->assertEquals("

Hi, my name is Peter and I am male.

\n", $templatesDirectory); + $this->assertEquals('Happy Tuesday!', $outputMulti); } - public function testFetch() + public function testArrayWithASingleTemplateWithANamespace() { - $output = $this->view->fetch('example.html', [ + $views = new Twig([ + 'One' => [ + __DIR__.'/templates', + ], + ]); + + $output = $views->fetch('@One/example.html', [ 'name' => 'Josh' ]); $this->assertEquals("

Hi, my name is Josh.

\n", $output); } - public function testSingleTemplateWithANamespace() + public function testASingleTemplateWithANamespace() { $views = new Twig([ 'One' => __DIR__.'/templates', @@ -46,28 +85,57 @@ public function testSingleTemplateWithANamespace() $this->assertEquals("

Hi, my name is Josh.

\n", $output); } - public function testMultipleTemplatesWithMulNamespace() + public function testMultipleTemplatesWithMultipleNamespace() { + $weekday = (new \DateTimeImmutable('2016-03-08'))->format('l'); + $views = new Twig([ - 'One' => __DIR__.'/templates', - 'Two' => __DIR__.'/another', + 'One' => __DIR__.'/templates', + 'Two' => __DIR__.'/another', + 'Three' => [ + __DIR__.'/multi', + ], ]); $outputOne = $views->fetch('@One/example.html', [ 'name' => 'Peter' ]); - $outputTwo = $views->fetch('@Two/example.html', [ + $outputTwo = $views->fetch('@Two/another_example.html', [ 'name' => 'Peter', 'gender' => 'male' ]); + $outputThree = $views->fetch('@Three/directory/template/example.html', [ + 'weekday' => $weekday, + ]); + $this->assertEquals("

Hi, my name is Peter.

\n", $outputOne); $this->assertEquals("

Hi, my name is Peter and I am male.

\n", $outputTwo); + $this->assertEquals('Happy Tuesday!', $outputThree); + } + + public function testMultipleDirectoriesWithoutNamespaces() + { + $weekday = (new \DateTimeImmutable('2016-03-08'))->format('l'); + $view = new Twig([__DIR__.'/multi/', __DIR__.'/another/']); + + $rootDirectory = $view->fetch('directory/template/example.html', [ + 'weekday' => $weekday, + ]); + $multiDirectory = $view->fetch('another_example.html', [ + 'name' => 'Peter', + 'gender' => 'male', + ]); + + $this->assertEquals('Happy Tuesday!', $rootDirectory); + $this->assertEquals("

Hi, my name is Peter and I am male.

\n", $multiDirectory); } public function testRender() { + $view = new Twig(dirname(__FILE__) . '/templates'); + $mockBody = $this->getMockBuilder('Psr\Http\Message\StreamInterface') ->disableOriginalConstructor() ->getMock(); @@ -85,7 +153,7 @@ public function testRender() ->method('getBody') ->willReturn($mockBody); - $response = $this->view->render($mockResponse, 'example.html', [ + $response = $view->render($mockResponse, 'example.html', [ 'name' => 'Josh' ]); $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response); diff --git a/tests/another/example.html b/tests/another/another_example.html similarity index 100% rename from tests/another/example.html rename to tests/another/another_example.html diff --git a/tests/multi/directory/template/example.html b/tests/multi/directory/template/example.html new file mode 100644 index 0000000..1f1994d --- /dev/null +++ b/tests/multi/directory/template/example.html @@ -0,0 +1 @@ +Happy {{ weekday }}! \ No newline at end of file