From f43344b9a3a8426587c4f791e48af14045238986 Mon Sep 17 00:00:00 2001 From: Nigel Greenway Date: Tue, 8 Mar 2016 21:41:40 +0000 Subject: [PATCH 1/5] Rename file for ease of separation --- tests/another/{example.html => another_example.html} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/another/{example.html => another_example.html} (100%) 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 From fcc832956cc65e94c477c199f4265a770b148540 Mon Sep 17 00:00:00 2001 From: Nigel Greenway Date: Tue, 8 Mar 2016 21:51:56 +0000 Subject: [PATCH 2/5] Fixes #52 - Allow multiple paths per namespace Fixes the BC break that was introduced with commit 09eca0e. authors: @NigelGreenway --- src/Twig.php | 2 +- tests/TwigTest.php | 70 ++++++++++++++++----- tests/multi/directory/template/example.html | 1 + 3 files changed, 56 insertions(+), 17 deletions(-) create mode 100644 tests/multi/directory/template/example.html diff --git a/src/Twig.php b/src/Twig.php index e275f66..ce5bccd 100644 --- a/src/Twig.php +++ b/src/Twig.php @@ -118,7 +118,7 @@ private function addPaths(array $paths) $loader = new \Twig_Loader_Filesystem(); foreach ($paths as $namespace => $path) { - $loader->addPath($path, $namespace); + $loader->setPaths($path, $namespace); } return $loader; diff --git a/tests/TwigTest.php b/tests/TwigTest.php index 296749a..3e28218 100644 --- a/tests/TwigTest.php +++ b/tests/TwigTest.php @@ -14,29 +14,55 @@ class TwigTest extends \PHPUnit_Framework_TestCase { - /** - * @var Twig - */ - protected $view; - - public function setUp() - { - $this->view = new Twig(dirname(__FILE__) . '/templates'); - } - public function testFetch() { - $output = $this->view->fetch('example.html', [ + $view = new Twig(dirname(__FILE__) . '/templates'); + + $output = $view->fetch('example.html', [ 'name' => 'Josh' ]); $this->assertEquals("

Hi, my name is Josh.

\n", $output); } + public function testSingleNamespaceAndMultipleDirectories() + { + $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 testSingleTemplateWithANamespace() { $views = new Twig([ - 'One' => __DIR__.'/templates', + 'One' => [ + __DIR__.'/templates', + ], ]); $output = $views->fetch('@One/example.html', [ @@ -48,26 +74,38 @@ public function testSingleTemplateWithANamespace() public function testMultipleTemplatesWithMulNamespace() { + $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 testRender() { + $view = new Twig(dirname(__FILE__) . '/templates'); + $mockBody = $this->getMockBuilder('Psr\Http\Message\StreamInterface') ->disableOriginalConstructor() ->getMock(); @@ -85,7 +123,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/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 From 379c9e58182c34802f684b0bc05170dd39e797dc Mon Sep 17 00:00:00 2001 From: Nigel Greenway Date: Tue, 8 Mar 2016 21:57:26 +0000 Subject: [PATCH 3/5] Correct spelling on test name --- tests/TwigTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TwigTest.php b/tests/TwigTest.php index 3e28218..dcd154d 100644 --- a/tests/TwigTest.php +++ b/tests/TwigTest.php @@ -72,7 +72,7 @@ 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'); From 49e2ecc94ec2ea626f32f07c4f2bfe7dc82c5a2f Mon Sep 17 00:00:00 2001 From: Nigel Greenway Date: Sat, 12 Mar 2016 22:19:57 +0000 Subject: [PATCH 4/5] Fixes #52 - Refactor `addPaths` method Allows the use of `new Slim\Views\Twig(['path1', 'path2']);` again as it was previously broken by 09eca0e28c7763938a450fa322691fce3d119319. --- src/Twig.php | 6 +++++- tests/TwigTest.php | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Twig.php b/src/Twig.php index ce5bccd..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->setPaths($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 dcd154d..40eab88 100644 --- a/tests/TwigTest.php +++ b/tests/TwigTest.php @@ -102,6 +102,23 @@ public function testMultipleTemplatesWithMultipleNamespace() $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'); From 9b0a35379a6910f33a77331df9ea7fb17197deba Mon Sep 17 00:00:00 2001 From: Nigel Greenway Date: Sun, 13 Mar 2016 20:30:50 +0000 Subject: [PATCH 5/5] Add missing test for single directory and NS Please see [here](https://github.com/slimphp/Twig-View/pull/53/files#r55942231) for the discussion. --- tests/TwigTest.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/TwigTest.php b/tests/TwigTest.php index 40eab88..79fcaf9 100644 --- a/tests/TwigTest.php +++ b/tests/TwigTest.php @@ -57,7 +57,7 @@ public function testSingleNamespaceAndMultipleDirectories() $this->assertEquals('Happy Tuesday!', $outputMulti); } - public function testSingleTemplateWithANamespace() + public function testArrayWithASingleTemplateWithANamespace() { $views = new Twig([ 'One' => [ @@ -72,6 +72,19 @@ public function testSingleTemplateWithANamespace() $this->assertEquals("

Hi, my name is Josh.

\n", $output); } + public function testASingleTemplateWithANamespace() + { + $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 testMultipleTemplatesWithMultipleNamespace() { $weekday = (new \DateTimeImmutable('2016-03-08'))->format('l');