fetch('example.html', [ 'name' => 'Josh' ]); $this->assertEquals("

Hi, my name is Josh.

\n", $output); } public function testFetchFromString() { $view = new Twig(dirname(__FILE__) . '/templates'); $output = $view->fetchFromString("

Hi, my name is {{ name }}.

", [ 'name' => 'Josh' ]); $this->assertEquals("

Hi, my name is Josh.

", $output); } public function testFetchBlock() { $view = new Twig(dirname(__FILE__) . '/templates'); $outputOne = $view->fetchBlock('block_example.html', 'first', [ 'name' => 'Josh' ]); $outputTwo = $view->fetchBlock('block_example.html', 'second', [ 'name' => 'Peter' ]); $this->assertEquals("

Hi, my name is Josh.

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

My name is not Peter.

", $outputTwo); } 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 testArrayWithASingleTemplateWithANamespace() { $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 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'); $views = new Twig([ 'One' => __DIR__.'/templates', 'Two' => __DIR__.'/another', 'Three' => [ __DIR__.'/multi', ], ]); $outputOne = $views->fetch('@One/example.html', [ 'name' => 'Peter' ]); $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(); $mockResponse = $this->getMockBuilder('Psr\Http\Message\ResponseInterface') ->disableOriginalConstructor() ->getMock(); $mockBody->expects($this->once()) ->method('write') ->with("

Hi, my name is Josh.

\n") ->willReturn(28); $mockResponse->expects($this->once()) ->method('getBody') ->willReturn($mockBody); $response = $view->render($mockResponse, 'example.html', [ 'name' => 'Josh' ]); $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response); } }