Passed
Pull Request — master (#2)
by Victor
01:48
created

TwigIntegrationTest::testRenderListOfModels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A TwigIntegrationTest::testShouldRenderASingleModel() 0 8 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoot\Shoot\Tests;
5
6
use PHPUnit\Framework\TestCase;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Shoot\Shoot\Extension;
9
use Shoot\Shoot\Middleware\PresenterMiddleware;
10
use Shoot\Shoot\Pipeline;
11
use Shoot\Shoot\Tests\Mocks\ContainerStub;
12
use Twig_Environment as Environment;
13
use Twig_Error_Runtime;
14
use Twig_Loader_Filesystem as FilesystemLoader;
15
16
final class TwigIntegrationTest extends TestCase
17
{
18
    /** @var Pipeline */
19
    private $pipeline;
20
21
    /** @var ServerRequestInterface */
22
    private $request;
23
24
    /** @var Environment */
25
    private $twig;
26
27
    /**
28
     * @return void
29
     */
30
    protected function setUp()
31
    {
32
        $container = new ContainerStub();
33
        $pipeline = new Pipeline([new PresenterMiddleware($container)]);
34
        $extension = new Extension($pipeline);
35
36
        $loader = new FilesystemLoader([realpath(__DIR__ . '/Fixtures/Templates')]);
37
        $twig = new Environment($loader, ['cache' => false, 'strict_variables' => true]);
38
        $twig->addExtension($extension);
39
40
        $this->pipeline = $pipeline;
41
        $this->request = $this->createMock(ServerRequestInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Psr\Ht...equestInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Psr\Http\Message\ServerRequestInterface of property $request.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
        $this->twig = $twig;
43
    }
44
45
    /**
46
     * @return void
47
     */
48
    public function testShouldRenderASingleModel()
49
    {
50
        $output = $this->renderTemplate('item.twig');
51
52
        $this->assertSame([
53
            '## item',
54
            'description',
55
        ], $output);
56
    }
57
58
    /**
59
     * @return void
60
     */
61
    public function testShouldRenderAListOfModels()
62
    {
63
        $output = $this->renderTemplate('item_list.twig');
64
65
        $this->assertSame([
66
            '# items',
67
            '## item 1',
68
            'description',
69
            '## item 2',
70
            'description',
71
            '## item 3',
72
            'description',
73
        ], $output);
74
    }
75
76
    /**
77
     * @return void
78
     */
79
    public function testAssigningMultipleModelsToAViewShouldThrowAnException()
80
    {
81
        $this->expectExceptionMessage('model has already been assigned');
82
83
        $this->renderTemplate('multiple_models.twig');
84
    }
85
86
    /**
87
     * @return void
88
     */
89
    public function testEmbeddedTemplatesShouldReceiveAllVariables()
90
    {
91
        $output = $this->renderTemplate('has_embedded_template.twig');
92
93
        $this->assertSame([
94
            '# title: item',
95
            'description',
96
        ], $output);
97
    }
98
99
    /**
100
     * @return void
101
     */
102
    public function testOptionalBlocksShouldBeHiddenIfTheyFail()
103
    {
104
        $output = $this->renderTemplate('optional_runtime_exception.twig');
105
106
        $this->assertContains('header', $output);
107
        $this->assertNotContains('should not be rendered', $output);
108
        $this->assertContains('footer', $output);
109
    }
110
111
    /**
112
     * @return void
113
     */
114
    public function testOptionalBlocksShouldNotSuppressUnknownVariables()
115
    {
116
        $this->expectException(Twig_Error_Runtime::class);
117
118
        $this->renderTemplate('optional_unknown_variable.twig');
119
    }
120
121
    /**
122
     * @param string $template
123
     *
124
     * @return string[]
125
     */
126
    private function renderTemplate(string $template): array
127
    {
128
        return $this->pipeline->withRequest($this->request, function () use ($template) {
129
            $output = $this->twig->render($template);
130
            $output = trim($output);
131
            $output = explode(PHP_EOL, $output);
132
            $output = array_map('trim', $output);
133
134
            return $output;
135
        });
136
    }
137
}
138