From 276a9e69f3993da79e18caf2dd239823d27f7cd3 Mon Sep 17 00:00:00 2001
From: fadrian06
Date: Wed, 24 Apr 2024 21:03:44 -0400
Subject: [PATCH] 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 f9f2855..5d196d6 100644
--- a/tests/FlightTest.php
+++ b/tests/FlightTest.php
@@ -356,26 +356,42 @@ class FlightTest extends TestCase
$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
\nHi
\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 361bb3c..38cabe0 100644
--- a/tests/ViewTest.php
+++ b/tests/ViewTest.php
@@ -153,26 +153,67 @@ class ViewTest extends TestCase
);
}
- 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
\nHi
\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 0000000..19e7182
--- /dev/null
+++ b/tests/views/input.php
@@ -0,0 +1,7 @@
+
+
+