1818use Slim \Views \Exception \PhpTemplateNotFoundException ;
1919use Slim \Views \PhpRenderer ;
2020use Throwable ;
21+ use UnexpectedValueException ;
2122
2223class PhpRendererTest extends TestCase
2324{
2425 public function testRenderer (): void
2526 {
2627 $ renderer = new PhpRenderer (__DIR__ . '/_files/ ' );
2728 $ headers = new Headers ();
28- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
29+ $ body = $ this -> createStream ( );
2930 $ response = new Response (200 , $ headers , $ body );
3031 $ newResponse = $ renderer ->render ($ response , 'template.phtml ' , ['hello ' => 'Hi ' ]);
3132 $ newResponse ->getBody ()->rewind ();
@@ -36,7 +37,7 @@ public function testRenderConstructor(): void
3637 {
3738 $ renderer = new PhpRenderer (__DIR__ . '/_files ' );
3839 $ headers = new Headers ();
39- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
40+ $ body = $ this -> createStream ( );
4041 $ response = new Response (200 , $ headers , $ body );
4142 $ newResponse = $ renderer ->render ($ response , 'template.phtml ' , ['hello ' => 'Hi ' ]);
4243 $ newResponse ->getBody ()->rewind ();
@@ -45,12 +46,11 @@ public function testRenderConstructor(): void
4546
4647 public function testAttributeMerging (): void
4748 {
48-
4949 $ renderer = new PhpRenderer (__DIR__ . '/_files/ ' , [
5050 'hello ' => 'Hello '
5151 ]);
5252 $ headers = new Headers ();
53- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
53+ $ body = $ this -> createStream ( );
5454 $ response = new Response (200 , $ headers , $ body );
5555 $ newResponse = $ renderer ->render ($ response , 'template.phtml ' , [
5656 'hello ' => 'Hi '
@@ -63,12 +63,12 @@ public function testExceptionInTemplate(): void
6363 {
6464 $ renderer = new PhpRenderer (__DIR__ . '/_files/ ' );
6565 $ headers = new Headers ();
66- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
66+ $ body = $ this -> createStream ( );
6767 $ response = new Response (200 , $ headers , $ body );
6868 try {
6969 $ newResponse = $ renderer ->render ($ response , 'exception_layout.phtml ' );
7070 } catch (Throwable $ t ) {
71- // Simulates an error template
71+ // Simulates an error template
7272 $ newResponse = $ renderer ->render ($ response , 'template.phtml ' , [
7373 'hello ' => 'Hi '
7474 ]);
@@ -82,7 +82,7 @@ public function testExceptionForTemplateInData(): void
8282 {
8383 $ renderer = new PhpRenderer (__DIR__ . '/_files/ ' );
8484 $ headers = new Headers ();
85- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
85+ $ body = $ this -> createStream ( );
8686 $ response = new Response (200 , $ headers , $ body );
8787 $ this ->expectException (InvalidArgumentException::class);
8888 $ renderer ->render ($ response , 'template.phtml ' , [
@@ -94,7 +94,7 @@ public function testTemplateNotFound(): void
9494 {
9595 $ renderer = new PhpRenderer (__DIR__ . '/_files/ ' );
9696 $ headers = new Headers ();
97- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
97+ $ body = $ this -> createStream ( );
9898 $ response = new Response (200 , $ headers , $ body );
9999 $ this ->expectException (PhpTemplateNotFoundException::class);
100100 $ renderer ->render ($ response , 'adfadftemplate.phtml ' , []);
@@ -105,37 +105,43 @@ public function testLayout(): void
105105 $ renderer = new PhpRenderer (__DIR__ . '/_files/ ' , ['title ' => 'My App ' ]);
106106 $ renderer ->setLayout ('layout.phtml ' );
107107 $ headers = new Headers ();
108- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
108+ $ body = $ this -> createStream ( );
109109 $ response = new Response (200 , $ headers , $ body );
110110 $ newResponse = $ renderer ->render ($ response , 'template.phtml ' , ['title ' => 'Hello - My App ' , 'hello ' => 'Hi ' ]);
111111 $ 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+ );
114117 }
115118
116119 public function testLayoutConstructor (): void
117120 {
118121 $ renderer = new PhpRenderer (__DIR__ . '/_files ' , ['title ' => 'My App ' ], 'layout.phtml ' );
119122 $ headers = new Headers ();
120- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
123+ $ body = $ this -> createStream ( );
121124 $ response = new Response (200 , $ headers , $ body );
122125 $ newResponse = $ renderer ->render ($ response , 'template.phtml ' , ['title ' => 'Hello - My App ' , 'hello ' => 'Hi ' ]);
123126 $ 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+ );
126132 }
127133
128134 public function testExceptionInLayout (): void
129135 {
130136 $ renderer = new PhpRenderer (__DIR__ . '/_files/ ' );
131137 $ renderer ->setLayout ('exception_layout.phtml ' );
132138 $ headers = new Headers ();
133- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
139+ $ body = $ this -> createStream ( );
134140 $ response = new Response (200 , $ headers , $ body );
135141 try {
136142 $ newResponse = $ renderer ->render ($ response , 'template.phtml ' );
137143 } catch (Throwable $ t ) {
138- // PHP 7+
144+ // PHP 7+
139145 // Simulates an error template
140146 $ renderer ->setLayout ('' );
141147 $ newResponse = $ renderer ->render ($ response , 'template.phtml ' , [
@@ -159,22 +165,35 @@ public function testContentDataKeyShouldBeIgnored(): void
159165 $ renderer = new PhpRenderer (__DIR__ . '/_files/ ' );
160166 $ renderer ->setLayout ('layout.phtml ' );
161167 $ headers = new Headers ();
162- $ body = new Stream ( fopen ( ' php://temp ' , ' r+ ' ) );
168+ $ body = $ this -> createStream ( );
163169 $ response = new Response (200 , $ headers , $ body );
164170 $ newResponse = $ renderer ->render (
165171 $ response ,
166172 'template.phtml ' ,
167173 ['title ' => 'Hello - My App ' , 'hello ' => 'Hi ' , 'content ' => 'Ho ' ]
168174 );
169175 $ 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+ );
172181 }
173182
174- public function testTemplateExists ()
183+ public function testTemplateExists (): void
175184 {
176185 $ renderer = new PhpRenderer (__DIR__ . '/_files/ ' );
177186 $ this ->assertTrue ($ renderer ->templateExists ('layout.phtml ' ));
178187 $ this ->assertFalse ($ renderer ->templateExists ('non-existant-template ' ));
179188 }
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+ }
180199}
0 commit comments