From 161a35a0a284572c4e8ba6de03e013f4780b8af4 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Wed, 24 Apr 2024 19:56:01 -0400 Subject: [PATCH 1/6] 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 c43a35f6..9397cc6a 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -121,6 +121,12 @@ public function render(string $file, ?array $data = null): void \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 fcb06c06..2432f150 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -152,4 +152,18 @@ public static function normalizePath(string $path, string $separator = DIRECTORY $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 00000000..cf0a36f0 --- /dev/null +++ b/tests/views/myComponent.php @@ -0,0 +1 @@ +
From 1b6cb088b7cbbbc2400fa5f28ddd033f7083ff34 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Wed, 24 Apr 2024 19:56:16 -0400 Subject: [PATCH 2/6] Added test on FlightTest too --- tests/FlightTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/FlightTest.php b/tests/FlightTest.php index d84336be..fe5b1442 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -354,4 +354,20 @@ public function after() $this->expectOutputString('Thisisaroutewithhtml'); } + + public function testItDoesNotKeepThePreviousStateOfOneViewComponentUsingFlightRender(): void + { + Flight::set('flight.views.path', __DIR__ . '/views'); + + $this->expectOutputString("
Hi
\n
\n"); + Flight::render('myComponent', ['prop' => 'Hi']); + + set_error_handler(function (int $code, string $message): void { + $this->assertMatchesRegularExpression('/^Undefined variable:? \$?prop$/', $message); + }); + + Flight::render('myComponent'); + + restore_error_handler(); + } } From eadf4333342e445e33800f34b23b3855541f2a19 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Wed, 24 Apr 2024 20:33:12 -0400 Subject: [PATCH 3/6] Added flag to not break Flight users code --- flight/template/View.php | 4 +++- tests/FlightTest.php | 12 ++++++++++-- tests/ViewTest.php | 11 ++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/flight/template/View.php b/flight/template/View.php index 9397cc6a..f78a968f 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -20,6 +20,8 @@ class View /** File extension. */ public string $extension = '.php'; + public bool $preserveVars = true; + /** * View variables. * @@ -122,7 +124,7 @@ public function render(string $file, ?array $data = null): void include $this->template; - if ($data !== null) { + if ($this->preserveVars === false && $data !== null) { foreach (array_keys($data) as $variable) { unset($this->vars[$variable]); } diff --git a/tests/FlightTest.php b/tests/FlightTest.php index fe5b1442..f9f28557 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -22,6 +22,7 @@ protected function setUp(): void $_REQUEST = []; Flight::init(); Flight::setEngine(new Engine()); + Flight::set('flight.views.path', __DIR__ . '/views'); } protected function tearDown(): void @@ -355,9 +356,9 @@ public function after() $this->expectOutputString('Thisisaroutewithhtml'); } - public function testItDoesNotKeepThePreviousStateOfOneViewComponentUsingFlightRender(): void + public function testDoesNotPreserveVarsWhenFlagIsDisabled(): void { - Flight::set('flight.views.path', __DIR__ . '/views'); + Flight::view()->preserveVars = false; $this->expectOutputString("
Hi
\n
\n"); Flight::render('myComponent', ['prop' => 'Hi']); @@ -370,4 +371,11 @@ public function testItDoesNotKeepThePreviousStateOfOneViewComponentUsingFlightRe restore_error_handler(); } + + public function testKeepThePreviousStateOfOneViewComponentByDefault(): void + { + $this->expectOutputString("
Hi
\n
Hi
\n"); + Flight::render('myComponent', ['prop' => 'Hi']); + Flight::render('myComponent'); + } } diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 2432f150..361bb3c7 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -153,8 +153,10 @@ public static function normalizePath(string $path, string $separator = DIRECTORY ); } - public function testItDoesNotKeepThePreviousStateOfOneViewComponent(): void + public function testDoesNotPreserveVarsWhenFlagIsDisabled(): void { + $this->view->preserveVars = false; + $this->expectOutputString("
Hi
\n
\n"); $this->view->render('myComponent', ['prop' => 'Hi']); @@ -166,4 +168,11 @@ public function testItDoesNotKeepThePreviousStateOfOneViewComponent(): void restore_error_handler(); } + + public function testKeepThePreviousStateOfOneViewComponentByDefault(): void + { + $this->expectOutputString("
Hi
\n
Hi
\n"); + $this->view->render('myComponent', ['prop' => 'Hi']); + $this->view->render('myComponent'); + } } From 276a9e69f3993da79e18caf2dd239823d27f7cd3 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Wed, 24 Apr 2024 21:03:44 -0400 Subject: [PATCH 4/6] Added more tests when component defines a default value for no passed vars --- tests/FlightTest.php | 30 +++++++++++++++++------ tests/ViewTest.php | 57 +++++++++++++++++++++++++++++++++++++------ tests/views/input.php | 7 ++++++ 3 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 tests/views/input.php diff --git a/tests/FlightTest.php b/tests/FlightTest.php index f9f28557..5d196d6f 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -356,26 +356,42 @@ public function after() $this->expectOutputString('Thisisaroutewithhtml'); } - public function testDoesNotPreserveVarsWhenFlagIsDisabled(): void + /** @dataProvider \tests\ViewTest::renderDataProvider */ + public function testDoesNotPreserveVarsWhenFlagIsDisabled( + string $output, + array $renderParams, + string $regexp + ): void { Flight::view()->preserveVars = false; - $this->expectOutputString("
Hi
\n
\n"); - Flight::render('myComponent', ['prop' => 'Hi']); + $this->expectOutputString($output); + Flight::render(...$renderParams); - set_error_handler(function (int $code, string $message): void { - $this->assertMatchesRegularExpression('/^Undefined variable:? \$?prop$/', $message); + set_error_handler(function (int $code, string $message) use ($regexp): void { + $this->assertMatchesRegularExpression($regexp, $message); }); - Flight::render('myComponent'); + Flight::render($renderParams[0]); restore_error_handler(); } public function testKeepThePreviousStateOfOneViewComponentByDefault(): void { - $this->expectOutputString("
Hi
\n
Hi
\n"); + $this->expectOutputString(<<Hi +
Hi
+ + + + + + html); + Flight::render('myComponent', ['prop' => 'Hi']); Flight::render('myComponent'); + Flight::render('input', ['type' => 'number']); + Flight::render('input'); } } diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 361bb3c7..38cabe06 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -153,26 +153,67 @@ public static function normalizePath(string $path, string $separator = DIRECTORY ); } - public function testDoesNotPreserveVarsWhenFlagIsDisabled(): void - { + /** @dataProvider renderDataProvider */ + public function testDoesNotPreserveVarsWhenFlagIsDisabled( + string $output, + array $renderParams, + string $regexp + ): void { $this->view->preserveVars = false; - $this->expectOutputString("
Hi
\n
\n"); - $this->view->render('myComponent', ['prop' => 'Hi']); + $this->expectOutputString($output); + $this->view->render(...$renderParams); - set_error_handler(function (int $code, string $message): void { - $this->assertMatchesRegularExpression('/^Undefined variable:? \$?prop$/', $message); + set_error_handler(function (int $code, string $message) use ($regexp): void { + $this->assertMatchesRegularExpression($regexp, $message); }); - $this->view->render('myComponent'); + $this->view->render($renderParams[0]); restore_error_handler(); } public function testKeepThePreviousStateOfOneViewComponentByDefault(): void { - $this->expectOutputString("
Hi
\n
Hi
\n"); + $this->expectOutputString(<<Hi +
Hi
+ + + + + + html); + $this->view->render('myComponent', ['prop' => 'Hi']); $this->view->render('myComponent'); + $this->view->render('input', ['type' => 'number']); + $this->view->render('input'); + } + + public static function renderDataProvider(): array + { + return [ + [ + <<Hi +
+ + html, + ['myComponent', ['prop' => 'Hi']], + '/^Undefined variable:? \$?prop$/' + ], + [ + << + + + + html, + ['input', ['type' => 'number']], + '/^.*$/' + ], + ]; } } diff --git a/tests/views/input.php b/tests/views/input.php new file mode 100644 index 00000000..19e7182c --- /dev/null +++ b/tests/views/input.php @@ -0,0 +1,7 @@ + + + From f3453cab178809216f71ed832d041bece1b8a33a Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Sat, 1 Jun 2024 21:07:55 -0400 Subject: [PATCH 5/6] Simplified fix thanks to @vlakoff (https://github.com/vlakoff) --- flight/template/View.php | 14 ++++++-------- tests/ViewTest.php | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/flight/template/View.php b/flight/template/View.php index f78a968f..d347a50b 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -116,19 +116,17 @@ public function render(string $file, ?array $data = null): void throw new \Exception("Template file not found: {$normalized_path}."); } - if (\is_array($data)) { - $this->vars = \array_merge($this->vars, $data); - } - \extract($this->vars); - include $this->template; + if (\is_array($data)) { + \extract($data); - if ($this->preserveVars === false && $data !== null) { - foreach (array_keys($data) as $variable) { - unset($this->vars[$variable]); + if ($this->preserveVars) { + $this->vars = \array_merge($this->vars, $data); } } + + include $this->template; } /** diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 38cabe06..d6754c93 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -191,6 +191,22 @@ public function testKeepThePreviousStateOfOneViewComponentByDefault(): void $this->view->render('input'); } + public function testKeepThePreviousStateOfDataSettedBySetMethod(): void + { + $this->view->preserveVars = false; + + $this->view->set('prop', 'bar'); + + $this->expectOutputString(<<qux +
bar
+ + html); + + $this->view->render('myComponent', ['prop' => 'qux']); + $this->view->render('myComponent'); + } + public static function renderDataProvider(): array { return [ From a68fc3220418c304a69daf6b2abb4f91fb431933 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Mon, 3 Jun 2024 11:17:34 -0400 Subject: [PATCH 6/6] Unnecessary explicitness :| --- flight/template/View.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flight/template/View.php b/flight/template/View.php index d347a50b..15e4fc87 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -118,10 +118,10 @@ public function render(string $file, ?array $data = null): void \extract($this->vars); - if (\is_array($data)) { + if (\is_array($data) === true) { \extract($data); - if ($this->preserveVars) { + if ($this->preserveVars === true) { $this->vars = \array_merge($this->vars, $data); } }