Completed
Pull Request — master (#37)
by Daniel
04:38
created

TwigRendererTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Psi\Bridge\ContentType\Twig\Tests\Unit;
4
5
use Psi\Component\ContentType\View\ViewInterface;
6
use Psi\Bridge\ContentType\Twig\TwigRenderer;
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);
0 ignored issues
show
Bug introduced by
The property twig does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
15
        $this->renderer = new TwigRenderer($this->twig->reveal());
16
17
        $this->view = $this->prophesize(ViewInterface::class);
0 ignored issues
show
Bug introduced by
The property view does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
18
        $this->template = $this->prophesize(\Twig_Template::class);
0 ignored issues
show
Bug introduced by
The property template does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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