@@ -152,4 +152,84 @@ public static function normalizePath(string $path, string $separator = DIRECTORY
152152 $ viewMock ::normalizePath ('C:/xampp/htdocs/libs/Flight\core\index.php ' , '° ' )
153153 );
154154 }
155+
156+ /** @dataProvider renderDataProvider */
157+ public function testDoesNotPreserveVarsWhenFlagIsDisabled (
158+ string $ output ,
159+ array $ renderParams ,
160+ string $ regexp
161+ ): void {
162+ $ this ->view ->preserveVars = false ;
163+
164+ $ this ->expectOutputString ($ output );
165+ $ this ->view ->render (...$ renderParams );
166+
167+ set_error_handler (function (int $ code , string $ message ) use ($ regexp ): void {
168+ $ this ->assertMatchesRegularExpression ($ regexp , $ message );
169+ });
170+
171+ $ this ->view ->render ($ renderParams [0 ]);
172+
173+ restore_error_handler ();
174+ }
175+
176+ public function testKeepThePreviousStateOfOneViewComponentByDefault (): void
177+ {
178+ $ this ->expectOutputString (<<<html
179+ <div>Hi</div>
180+ <div>Hi</div>
181+
182+ <input type="number" />
183+
184+ <input type="number" />
185+
186+ html );
187+
188+ $ this ->view ->render ('myComponent ' , ['prop ' => 'Hi ' ]);
189+ $ this ->view ->render ('myComponent ' );
190+ $ this ->view ->render ('input ' , ['type ' => 'number ' ]);
191+ $ this ->view ->render ('input ' );
192+ }
193+
194+ public function testKeepThePreviousStateOfDataSettedBySetMethod (): void
195+ {
196+ $ this ->view ->preserveVars = false ;
197+
198+ $ this ->view ->set ('prop ' , 'bar ' );
199+
200+ $ this ->expectOutputString (<<<html
201+ <div>qux</div>
202+ <div>bar</div>
203+
204+ html );
205+
206+ $ this ->view ->render ('myComponent ' , ['prop ' => 'qux ' ]);
207+ $ this ->view ->render ('myComponent ' );
208+ }
209+
210+ public static function renderDataProvider (): array
211+ {
212+ return [
213+ [
214+ <<<html
215+ <div>Hi</div>
216+ <div></div>
217+
218+ html ,
219+ ['myComponent ' , ['prop ' => 'Hi ' ]],
220+ '/^Undefined variable:? \$?prop$/ '
221+ ],
222+ [
223+ <<<html
224+
225+ <input type="number" />
226+
227+ <input type="text" />
228+
229+ html ,
230+ ['input ' , ['type ' => 'number ' ]],
231+ '/^.*$/ '
232+ ],
233+ ];
234+ }
155235}
0 commit comments