From 161a35a0a284572c4e8ba6de03e013f4780b8af4 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Wed, 24 Apr 2024 19:56:01 -0400 Subject: [PATCH] Added test on ViewTest --- flight/template/View.php | 6 ++++++ tests/ViewTest.php | 14 ++++++++++++++ tests/views/myComponent.php | 1 + 3 files changed, 21 insertions(+) create mode 100644 tests/views/myComponent.php diff --git a/flight/template/View.php b/flight/template/View.php index c43a35f..9397cc6 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -121,6 +121,12 @@ class View \extract($this->vars); include $this->template; + + if ($data !== null) { + foreach (array_keys($data) as $variable) { + unset($this->vars[$variable]); + } + } } /** diff --git a/tests/ViewTest.php b/tests/ViewTest.php index fcb06c0..2432f15 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -152,4 +152,18 @@ class ViewTest extends TestCase $viewMock::normalizePath('C:/xampp/htdocs/libs/Flight\core\index.php', '°') ); } + + public function testItDoesNotKeepThePreviousStateOfOneViewComponent(): void + { + $this->expectOutputString("
Hi
\n
\n"); + $this->view->render('myComponent', ['prop' => 'Hi']); + + set_error_handler(function (int $code, string $message): void { + $this->assertMatchesRegularExpression('/^Undefined variable:? \$?prop$/', $message); + }); + + $this->view->render('myComponent'); + + restore_error_handler(); + } } diff --git a/tests/views/myComponent.php b/tests/views/myComponent.php new file mode 100644 index 0000000..cf0a36f --- /dev/null +++ b/tests/views/myComponent.php @@ -0,0 +1 @@ +