Added more tests when component defines a default value for no passed vars

pull/581/head
fadrian06 9 months ago
parent eadf433334
commit 276a9e69f3

@ -356,26 +356,42 @@ class FlightTest extends TestCase
$this->expectOutputString('Thisisaroutewithhtml'); $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; Flight::view()->preserveVars = false;
$this->expectOutputString("<div>Hi</div>\n<div></div>\n"); $this->expectOutputString($output);
Flight::render('myComponent', ['prop' => 'Hi']); Flight::render(...$renderParams);
set_error_handler(function (int $code, string $message): void { set_error_handler(function (int $code, string $message) use ($regexp): void {
$this->assertMatchesRegularExpression('/^Undefined variable:? \$?prop$/', $message); $this->assertMatchesRegularExpression($regexp, $message);
}); });
Flight::render('myComponent'); Flight::render($renderParams[0]);
restore_error_handler(); restore_error_handler();
} }
public function testKeepThePreviousStateOfOneViewComponentByDefault(): void public function testKeepThePreviousStateOfOneViewComponentByDefault(): void
{ {
$this->expectOutputString("<div>Hi</div>\n<div>Hi</div>\n"); $this->expectOutputString(<<<html
<div>Hi</div>
<div>Hi</div>
<input type="number" />
<input type="number" />
html);
Flight::render('myComponent', ['prop' => 'Hi']); Flight::render('myComponent', ['prop' => 'Hi']);
Flight::render('myComponent'); Flight::render('myComponent');
Flight::render('input', ['type' => 'number']);
Flight::render('input');
} }
} }

@ -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->view->preserveVars = false;
$this->expectOutputString("<div>Hi</div>\n<div></div>\n"); $this->expectOutputString($output);
$this->view->render('myComponent', ['prop' => 'Hi']); $this->view->render(...$renderParams);
set_error_handler(function (int $code, string $message): void { set_error_handler(function (int $code, string $message) use ($regexp): void {
$this->assertMatchesRegularExpression('/^Undefined variable:? \$?prop$/', $message); $this->assertMatchesRegularExpression($regexp, $message);
}); });
$this->view->render('myComponent'); $this->view->render($renderParams[0]);
restore_error_handler(); restore_error_handler();
} }
public function testKeepThePreviousStateOfOneViewComponentByDefault(): void public function testKeepThePreviousStateOfOneViewComponentByDefault(): void
{ {
$this->expectOutputString("<div>Hi</div>\n<div>Hi</div>\n"); $this->expectOutputString(<<<html
<div>Hi</div>
<div>Hi</div>
<input type="number" />
<input type="number" />
html);
$this->view->render('myComponent', ['prop' => 'Hi']); $this->view->render('myComponent', ['prop' => 'Hi']);
$this->view->render('myComponent'); $this->view->render('myComponent');
$this->view->render('input', ['type' => 'number']);
$this->view->render('input');
}
public static function renderDataProvider(): array
{
return [
[
<<<html
<div>Hi</div>
<div></div>
html,
['myComponent', ['prop' => 'Hi']],
'/^Undefined variable:? \$?prop$/'
],
[
<<<html
<input type="number" />
<input type="text" />
html,
['input', ['type' => 'number']],
'/^.*$/'
],
];
} }
} }

@ -0,0 +1,7 @@
<?php
$type ??= 'text';
?>
<input type="<?= $type ?>" />
Loading…
Cancel
Save