1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shoot\Shoot\Tests; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use Shoot\Shoot\Extension; |
10
|
|
|
use Shoot\Shoot\Middleware\PresenterMiddleware; |
11
|
|
|
use Shoot\Shoot\Pipeline; |
12
|
|
|
use Shoot\Shoot\Tests\Mocks\ContainerStub; |
13
|
|
|
use Twig_Environment as Environment; |
14
|
|
|
use Twig_Error_Runtime; |
15
|
|
|
use Twig_Loader_Filesystem as FilesystemLoader; |
16
|
|
|
|
17
|
|
|
final class TwigIntegrationTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** @var Pipeline */ |
20
|
|
|
private $pipeline; |
21
|
|
|
|
22
|
|
|
/** @var ServerRequestInterface|MockObject */ |
23
|
|
|
private $request; |
24
|
|
|
|
25
|
|
|
/** @var Environment */ |
26
|
|
|
private $twig; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
protected function setUp() |
32
|
|
|
{ |
33
|
|
|
$container = new ContainerStub(); |
34
|
|
|
$pipeline = new Pipeline([new PresenterMiddleware($container)]); |
35
|
|
|
$extension = new Extension($pipeline); |
36
|
|
|
|
37
|
|
|
$loader = new FilesystemLoader([realpath(__DIR__ . '/Fixtures/Templates')]); |
38
|
|
|
$twig = new Environment($loader, ['cache' => false, 'strict_variables' => true]); |
39
|
|
|
$twig->addExtension($extension); |
40
|
|
|
|
41
|
|
|
$this->pipeline = $pipeline; |
42
|
|
|
$this->request = $this->createMock(ServerRequestInterface::class); |
43
|
|
|
$this->twig = $twig; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function testShouldRenderASingleModel() |
50
|
|
|
{ |
51
|
|
|
$output = $this->renderTemplate('item.twig'); |
52
|
|
|
|
53
|
|
|
$this->assertSame([ |
54
|
|
|
'## item', |
55
|
|
|
'description', |
56
|
|
|
], $output); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function testShouldRenderAListOfModels() |
63
|
|
|
{ |
64
|
|
|
$output = $this->renderTemplate('item_list.twig'); |
65
|
|
|
|
66
|
|
|
$this->assertSame([ |
67
|
|
|
'# items', |
68
|
|
|
'## item 1', |
69
|
|
|
'description', |
70
|
|
|
'## item 2', |
71
|
|
|
'description', |
72
|
|
|
'## item 3', |
73
|
|
|
'description', |
74
|
|
|
], $output); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function testAssigningMultipleModelsToAViewShouldThrowAnException() |
81
|
|
|
{ |
82
|
|
|
$this->expectExceptionMessage('model has already been assigned'); |
83
|
|
|
|
84
|
|
|
$this->renderTemplate('multiple_models.twig'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function testEmbeddedTemplatesShouldReceiveAllVariables() |
91
|
|
|
{ |
92
|
|
|
$output = $this->renderTemplate('has_embedded_template.twig'); |
93
|
|
|
|
94
|
|
|
$this->assertSame([ |
95
|
|
|
'# title: item', |
96
|
|
|
'description', |
97
|
|
|
], $output); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
|
|
public function testOptionalBlocksShouldBeHiddenIfTheyFail() |
104
|
|
|
{ |
105
|
|
|
$output = $this->renderTemplate('optional_runtime_exception.twig'); |
106
|
|
|
|
107
|
|
|
$this->assertContains('header', $output); |
108
|
|
|
$this->assertNotContains('should not be rendered', $output); |
109
|
|
|
$this->assertContains('footer', $output); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
public function testOptionalBlocksShouldNotSuppressUnknownVariables() |
116
|
|
|
{ |
117
|
|
|
$this->expectException(Twig_Error_Runtime::class); |
118
|
|
|
|
119
|
|
|
$this->renderTemplate('optional_unknown_variable.twig'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
public function testNestedOptionalBlocksShouldNotAffectedParents() |
126
|
|
|
{ |
127
|
|
|
$output = $this->renderTemplate('optional_nested.twig'); |
128
|
|
|
|
129
|
|
|
$this->assertContains('header', $output); |
130
|
|
|
$this->assertNotContains('should not be rendered', $output); |
131
|
|
|
$this->assertContains('not affected', $output); |
132
|
|
|
$this->assertContains('footer', $output); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $template |
137
|
|
|
* |
138
|
|
|
* @return string[] |
139
|
|
|
*/ |
140
|
|
|
private function renderTemplate(string $template): array |
141
|
|
|
{ |
142
|
|
|
return $this->pipeline->withRequest($this->request, function () use ($template): array { |
143
|
|
|
$output = $this->twig->render($template); |
144
|
|
|
$output = trim($output); |
145
|
|
|
$output = explode(PHP_EOL, $output); |
146
|
|
|
$output = array_map('trim', $output); |
147
|
|
|
|
148
|
|
|
return $output; |
149
|
|
|
}); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|