18
18
use Slim \Views \Exception \PhpTemplateNotFoundException ;
19
19
use Slim \Views \PhpRenderer ;
20
20
use Throwable ;
21
+ use UnexpectedValueException ;
21
22
22
23
class PhpRendererTest extends TestCase
23
24
{
24
25
public function testRenderer (): void
25
26
{
26
27
$ renderer = new PhpRenderer (__DIR__ . '/_files/ ' );
27
28
$ headers = new Headers ();
28
- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
29
+ $ body = $ this -> createStream ( );
29
30
$ response = new Response (200 , $ headers , $ body );
30
31
$ newResponse = $ renderer ->render ($ response , 'template.phtml ' , ['hello ' => 'Hi ' ]);
31
32
$ newResponse ->getBody ()->rewind ();
@@ -36,7 +37,7 @@ public function testRenderConstructor(): void
36
37
{
37
38
$ renderer = new PhpRenderer (__DIR__ . '/_files ' );
38
39
$ headers = new Headers ();
39
- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
40
+ $ body = $ this -> createStream ( );
40
41
$ response = new Response (200 , $ headers , $ body );
41
42
$ newResponse = $ renderer ->render ($ response , 'template.phtml ' , ['hello ' => 'Hi ' ]);
42
43
$ newResponse ->getBody ()->rewind ();
@@ -45,12 +46,11 @@ public function testRenderConstructor(): void
45
46
46
47
public function testAttributeMerging (): void
47
48
{
48
-
49
49
$ renderer = new PhpRenderer (__DIR__ . '/_files/ ' , [
50
50
'hello ' => 'Hello '
51
51
]);
52
52
$ headers = new Headers ();
53
- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
53
+ $ body = $ this -> createStream ( );
54
54
$ response = new Response (200 , $ headers , $ body );
55
55
$ newResponse = $ renderer ->render ($ response , 'template.phtml ' , [
56
56
'hello ' => 'Hi '
@@ -63,12 +63,12 @@ public function testExceptionInTemplate(): void
63
63
{
64
64
$ renderer = new PhpRenderer (__DIR__ . '/_files/ ' );
65
65
$ headers = new Headers ();
66
- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
66
+ $ body = $ this -> createStream ( );
67
67
$ response = new Response (200 , $ headers , $ body );
68
68
try {
69
69
$ newResponse = $ renderer ->render ($ response , 'exception_layout.phtml ' );
70
70
} catch (Throwable $ t ) {
71
- // Simulates an error template
71
+ // Simulates an error template
72
72
$ newResponse = $ renderer ->render ($ response , 'template.phtml ' , [
73
73
'hello ' => 'Hi '
74
74
]);
@@ -82,7 +82,7 @@ public function testExceptionForTemplateInData(): void
82
82
{
83
83
$ renderer = new PhpRenderer (__DIR__ . '/_files/ ' );
84
84
$ headers = new Headers ();
85
- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
85
+ $ body = $ this -> createStream ( );
86
86
$ response = new Response (200 , $ headers , $ body );
87
87
$ this ->expectException (InvalidArgumentException::class);
88
88
$ renderer ->render ($ response , 'template.phtml ' , [
@@ -94,7 +94,7 @@ public function testTemplateNotFound(): void
94
94
{
95
95
$ renderer = new PhpRenderer (__DIR__ . '/_files/ ' );
96
96
$ headers = new Headers ();
97
- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
97
+ $ body = $ this -> createStream ( );
98
98
$ response = new Response (200 , $ headers , $ body );
99
99
$ this ->expectException (PhpTemplateNotFoundException::class);
100
100
$ renderer ->render ($ response , 'adfadftemplate.phtml ' , []);
@@ -105,37 +105,43 @@ public function testLayout(): void
105
105
$ renderer = new PhpRenderer (__DIR__ . '/_files/ ' , ['title ' => 'My App ' ]);
106
106
$ renderer ->setLayout ('layout.phtml ' );
107
107
$ headers = new Headers ();
108
- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
108
+ $ body = $ this -> createStream ( );
109
109
$ response = new Response (200 , $ headers , $ body );
110
110
$ newResponse = $ renderer ->render ($ response , 'template.phtml ' , ['title ' => 'Hello - My App ' , 'hello ' => 'Hi ' ]);
111
111
$ newResponse ->getBody ()->rewind ();
112
- $ this ->assertEquals ('<html><head><title>Hello - My App</title></head><body>Hi<footer>This is the footer '
113
- . '</footer></body></html> ' , $ newResponse ->getBody ()->getContents ());
112
+ $ this ->assertEquals (
113
+ '<html><head><title>Hello - My App</title></head><body>Hi<footer>This is the footer '
114
+ . '</footer></body></html> ' ,
115
+ $ newResponse ->getBody ()->getContents ()
116
+ );
114
117
}
115
118
116
119
public function testLayoutConstructor (): void
117
120
{
118
121
$ renderer = new PhpRenderer (__DIR__ . '/_files ' , ['title ' => 'My App ' ], 'layout.phtml ' );
119
122
$ headers = new Headers ();
120
- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
123
+ $ body = $ this -> createStream ( );
121
124
$ response = new Response (200 , $ headers , $ body );
122
125
$ newResponse = $ renderer ->render ($ response , 'template.phtml ' , ['title ' => 'Hello - My App ' , 'hello ' => 'Hi ' ]);
123
126
$ newResponse ->getBody ()->rewind ();
124
- $ this ->assertEquals ('<html><head><title>Hello - My App</title></head><body>Hi<footer>This is the footer '
125
- . '</footer></body></html> ' , $ newResponse ->getBody ()->getContents ());
127
+ $ this ->assertEquals (
128
+ '<html><head><title>Hello - My App</title></head><body>Hi<footer>This is the footer '
129
+ . '</footer></body></html> ' ,
130
+ $ newResponse ->getBody ()->getContents ()
131
+ );
126
132
}
127
133
128
134
public function testExceptionInLayout (): void
129
135
{
130
136
$ renderer = new PhpRenderer (__DIR__ . '/_files/ ' );
131
137
$ renderer ->setLayout ('exception_layout.phtml ' );
132
138
$ headers = new Headers ();
133
- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
139
+ $ body = $ this -> createStream ( );
134
140
$ response = new Response (200 , $ headers , $ body );
135
141
try {
136
142
$ newResponse = $ renderer ->render ($ response , 'template.phtml ' );
137
143
} catch (Throwable $ t ) {
138
- // PHP 7+
144
+ // PHP 7+
139
145
// Simulates an error template
140
146
$ renderer ->setLayout ('' );
141
147
$ newResponse = $ renderer ->render ($ response , 'template.phtml ' , [
@@ -159,22 +165,35 @@ public function testContentDataKeyShouldBeIgnored(): void
159
165
$ renderer = new PhpRenderer (__DIR__ . '/_files/ ' );
160
166
$ renderer ->setLayout ('layout.phtml ' );
161
167
$ headers = new Headers ();
162
- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
168
+ $ body = $ this -> createStream ( );
163
169
$ response = new Response (200 , $ headers , $ body );
164
170
$ newResponse = $ renderer ->render (
165
171
$ response ,
166
172
'template.phtml ' ,
167
173
['title ' => 'Hello - My App ' , 'hello ' => 'Hi ' , 'content ' => 'Ho ' ]
168
174
);
169
175
$ newResponse ->getBody ()->rewind ();
170
- $ this ->assertEquals ('<html><head><title>Hello - My App</title></head><body>Hi<footer>This is the footer '
171
- . '</footer></body></html> ' , $ newResponse ->getBody ()->getContents ());
176
+ $ this ->assertEquals (
177
+ '<html><head><title>Hello - My App</title></head><body>Hi<footer>This is the footer '
178
+ . '</footer></body></html> ' ,
179
+ $ newResponse ->getBody ()->getContents ()
180
+ );
172
181
}
173
182
174
- public function testTemplateExists ()
183
+ public function testTemplateExists (): void
175
184
{
176
185
$ renderer = new PhpRenderer (__DIR__ . '/_files/ ' );
177
186
$ this ->assertTrue ($ renderer ->templateExists ('layout.phtml ' ));
178
187
$ this ->assertFalse ($ renderer ->templateExists ('non-existant-template ' ));
179
188
}
189
+
190
+ private function createStream (): Stream
191
+ {
192
+ $ resource = fopen ('php://temp ' , 'r+ ' );
193
+ if ($ resource === false ) {
194
+ throw new UnexpectedValueException ();
195
+ }
196
+
197
+ return new Stream ($ resource );
198
+ }
180
199
}
0 commit comments