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\Fixtures\Container; |
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 Environment */ |
22
|
|
|
private $twig; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
|
|
protected function setUp() |
28
|
|
|
{ |
29
|
|
|
$container = new Container(); |
30
|
|
|
$pipeline = new Pipeline([new PresenterMiddleware($container)]); |
31
|
|
|
$extension = new Extension($pipeline); |
32
|
|
|
|
33
|
|
|
$loader = new FilesystemLoader([realpath(__DIR__ . '/Fixtures/Templates')]); |
34
|
|
|
$twig = new Environment($loader, ['cache' => false, 'strict_variables' => true]); |
35
|
|
|
$twig->addExtension($extension); |
36
|
|
|
|
37
|
|
|
$this->pipeline = $pipeline; |
38
|
|
|
$this->twig = $twig; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function testRenderSingleModel() |
45
|
|
|
{ |
46
|
|
|
$output = $this->renderTemplate('item.twig'); |
47
|
|
|
|
48
|
|
|
$this->assertSame([ |
49
|
|
|
'## item', |
50
|
|
|
'description', |
51
|
|
|
], $output); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function testRenderListOfModels() |
58
|
|
|
{ |
59
|
|
|
$output = $this->renderTemplate('item_list.twig'); |
60
|
|
|
|
61
|
|
|
$this->assertSame([ |
62
|
|
|
'# items', |
63
|
|
|
'## item 1', |
64
|
|
|
'description', |
65
|
|
|
'## item 2', |
66
|
|
|
'description', |
67
|
|
|
'## item 3', |
68
|
|
|
'description', |
69
|
|
|
], $output); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function testAssigningMultipleModelsToAViewShouldThrowAnException() |
76
|
|
|
{ |
77
|
|
|
$this->expectExceptionMessage('model has already been assigned'); |
78
|
|
|
|
79
|
|
|
$this->renderTemplate('multiple_models.twig'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
public function testEmbeddedTemplatesShouldReceiveAllVariables() |
86
|
|
|
{ |
87
|
|
|
$output = $this->renderTemplate('has_embedded_template.twig'); |
88
|
|
|
|
89
|
|
|
$this->assertSame([ |
90
|
|
|
'# title: item', |
91
|
|
|
'description', |
92
|
|
|
], $output); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
public function testOptionalBlocksShouldBeHiddenIfTheyFail() |
99
|
|
|
{ |
100
|
|
|
$output = $this->renderTemplate('optional_runtime_exception.twig'); |
101
|
|
|
|
102
|
|
|
$this->assertContains('header', $output); |
103
|
|
|
$this->assertNotContains('should not be rendered', $output); |
104
|
|
|
$this->assertContains('footer', $output); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
|
public function testOptionalBlocksShouldNotSuppressUnknownVariables() |
111
|
|
|
{ |
112
|
|
|
$this->expectException(Twig_Error_Runtime::class); |
113
|
|
|
|
114
|
|
|
$this->renderTemplate('optional_unknown_variable.twig'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $template |
119
|
|
|
* |
120
|
|
|
* @return string[] |
121
|
|
|
*/ |
122
|
|
|
private function renderTemplate(string $template): array |
123
|
|
|
{ |
124
|
|
|
/** @var ServerRequestInterface $request */ |
125
|
|
|
$request = $this->prophesize(ServerRequestInterface::class)->reveal(); |
126
|
|
|
|
127
|
|
|
return $this->pipeline->withRequest($request, function () use ($template) { |
128
|
|
|
$output = $this->twig->render($template); |
129
|
|
|
$output = trim($output); |
130
|
|
|
$output = explode(PHP_EOL, $output); |
131
|
|
|
$output = array_map('trim', $output); |
132
|
|
|
|
133
|
|
|
return $output; |
134
|
|
|
}); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|