Completed
Push — master ( 2d5ed5...57ba8c )
by Daniel
12s
created

TwigRendererTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 49
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A testExtensions() 0 13 1
A testNoTemplate() 0 9 1
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