Passed
Push — master ( 9dda0c...c240f3 )
by Victor
02:31
created

testEmbeddedTemplatesShouldReceiveAllVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoot\Shoot\Tests;
5
6
use PHPUnit\Framework\TestCase;
7
use Shoot\Shoot\Middleware\PresenterMiddleware;
8
use Shoot\Shoot\Pipeline;
9
use Shoot\Shoot\Tests\Fixtures\Container;
10
use Twig_Environment as Environment;
11
use Twig_Error as Error;
12
use Twig_Loader_Filesystem as FilesystemLoader;
13
14
final class TwigIntegrationTest extends TestCase
15
{
16
    /** @var Environment */
17
    private $twig;
18
19
    /**
20
     * @return void
21
     */
22
    protected function setUp()
23
    {
24
        $container = new Container();
25
        $middleware = [new PresenterMiddleware($container)];
26
        $pipeline = new Pipeline($middleware);
27
        $loader = new FilesystemLoader([realpath(__DIR__ . '/Fixtures/Templates')]);
28
        $this->twig = new Environment($loader, ['cache' => false, 'strict_variables' => true]);
29
        $this->twig->addExtension($pipeline);
30
    }
31
32
    /**
33
     * @throws Error
34
     *
35
     * @return void
36
     */
37
    public function testRenderSingleModel()
38
    {
39
        $output = $this->renderTemplate('item.twig');
40
41
        $this->assertSame([
42
            '## item',
43
            'description',
44
        ], $output);
45
    }
46
47
    /**
48
     * @throws Error
49
     *
50
     * @return void
51
     */
52
    public function testRenderListOfModels()
53
    {
54
        $output = $this->renderTemplate('item_list.twig');
55
56
        $this->assertSame([
57
            '# items',
58
            '## item 1',
59
            'description',
60
            '## item 2',
61
            'description',
62
            '## item 3',
63
            'description',
64
        ], $output);
65
    }
66
67
    /**
68
     * @throws Error
69
     *
70
     * @return void
71
     */
72
    public function testAssigningMultipleModelsToAViewShouldThrowAnException()
73
    {
74
        $this->expectExceptionMessage('model has already been assigned');
75
76
        $this->renderTemplate('duplicate_models.twig');
77
    }
78
79
    /**
80
     * @throws Error
81
     *
82
     * @return void
83
     */
84
    public function testEmbeddedTemplatesShouldReceiveAllVariables()
85
    {
86
        $output = $this->renderTemplate('has_embedded_template.twig');
87
88
        $this->assertSame([
89
            '# title: item',
90
            'description',
91
        ], $output);
92
    }
93
94
    /**
95
     * @param string $template
96
     *
97
     * @throws Error
98
     *
99
     * @return string[]
100
     */
101
    private function renderTemplate(string $template): array
102
    {
103
        $output = $this->twig->render($template);
104
        $output = trim($output);
105
        $output = explode(PHP_EOL, $output);
106
        $output = array_map('trim', $output);
107
108
        return $output;
109
    }
110
}
111