1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psi\Bridge\ContentType\Twig\Tests\Unit; |
4
|
|
|
|
5
|
|
|
use Psi\Bridge\ContentType\Twig\TwigRenderer; |
6
|
|
|
use Psi\Component\ContentType\View\ViewInterface; |
7
|
|
|
|
8
|
|
|
class TwigRendererTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
private $renderer; |
11
|
|
|
private $twig; |
12
|
|
|
private $template; |
13
|
|
|
private $view; |
14
|
|
|
|
15
|
|
|
public function setUp() |
16
|
|
|
{ |
17
|
|
|
$this->twig = $this->prophesize(\Twig_Environment::class); |
18
|
|
|
$this->renderer = new TwigRenderer($this->twig->reveal()); |
19
|
|
|
|
20
|
|
|
$this->view = $this->prophesize(ViewInterface::class); |
21
|
|
|
$this->template = $this->prophesize(\Twig_Template::class); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* It should try each extension until it find one that works... |
26
|
|
|
*/ |
27
|
|
|
public function testExtensions() |
28
|
|
|
{ |
29
|
|
|
$this->view->getTemplate()->willReturn('foobar'); |
30
|
|
|
$this->twig->loadTemplate('foobar.html.twig')->willThrow(new \Twig_Error_Loader('foobar')); |
31
|
|
|
$this->twig->loadTemplate('foobar')->willThrow(new \Twig_Error_Loader('foobar')); |
32
|
|
|
$this->twig->loadTemplate('foobar.twig')->willReturn($this->template->reveal()); |
33
|
|
|
$this->template->render([ |
34
|
|
|
'view' => $this->view->reveal(), |
35
|
|
|
])->willReturn('This is a test'); |
36
|
|
|
|
37
|
|
|
$output = $this->renderer->render($this->view->reveal()); |
38
|
|
|
$this->assertEquals('This is a test', $output); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* If no template was found it should throw an exception. |
43
|
|
|
* |
44
|
|
|
* @expectedException \InvalidArgumentException |
45
|
|
|
* @expectedExceptionMessage Could not load template "foobar" |
46
|
|
|
*/ |
47
|
|
|
public function testNoTemplate() |
48
|
|
|
{ |
49
|
|
|
$this->view->getTemplate()->willReturn('foobar'); |
50
|
|
|
$this->twig->loadTemplate('foobar.html.twig')->willThrow(new \Twig_Error_Loader('foobar')); |
51
|
|
|
$this->twig->loadTemplate('foobar')->willThrow(new \Twig_Error_Loader('foobar')); |
52
|
|
|
$this->twig->loadTemplate('foobar.twig')->willThrow(new \Twig_Error_Loader('foobar')); |
53
|
|
|
|
54
|
|
|
$this->renderer->render($this->view->reveal()); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|