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
|
|
|
|
12
|
|
|
public function setUp() |
13
|
|
|
{ |
14
|
|
|
$this->twig = $this->prophesize(\Twig_Environment::class); |
|
|
|
|
15
|
|
|
$this->renderer = new TwigRenderer($this->twig->reveal()); |
16
|
|
|
|
17
|
|
|
$this->view = $this->prophesize(ViewInterface::class); |
|
|
|
|
18
|
|
|
$this->template = $this->prophesize(\Twig_Template::class); |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* It should try each extension until it find one that works... |
23
|
|
|
*/ |
24
|
|
|
public function testExtensions() |
25
|
|
|
{ |
26
|
|
|
$this->view->getTemplate()->willReturn('foobar'); |
27
|
|
|
$this->twig->loadTemplate('foobar.html.twig')->willThrow(new \Twig_Error_Loader('foobar')); |
28
|
|
|
$this->twig->loadTemplate('foobar')->willThrow(new \Twig_Error_Loader('foobar')); |
29
|
|
|
$this->twig->loadTemplate('foobar.twig')->willReturn($this->template->reveal()); |
30
|
|
|
$this->template->render([ |
31
|
|
|
'view' => $this->view->reveal(), |
32
|
|
|
])->willReturn('This is a test'); |
33
|
|
|
|
34
|
|
|
$output = $this->renderer->render($this->view->reveal()); |
35
|
|
|
$this->assertEquals('This is a test', $output); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* If no template was found it should throw an exception. |
40
|
|
|
* |
41
|
|
|
* @expectedException \InvalidArgumentException |
42
|
|
|
* @expectedExceptionMessage Could not load template "foobar" |
43
|
|
|
*/ |
44
|
|
|
public function testNoTemplate() |
45
|
|
|
{ |
46
|
|
|
$this->view->getTemplate()->willReturn('foobar'); |
47
|
|
|
$this->twig->loadTemplate('foobar.html.twig')->willThrow(new \Twig_Error_Loader('foobar')); |
48
|
|
|
$this->twig->loadTemplate('foobar')->willThrow(new \Twig_Error_Loader('foobar')); |
49
|
|
|
$this->twig->loadTemplate('foobar.twig')->willThrow(new \Twig_Error_Loader('foobar')); |
50
|
|
|
|
51
|
|
|
$this->renderer->render($this->view->reveal()); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: