Merge pull request #581 from flightphp/fix/bug-with-multiple-view-renders

Fix/bug with multiple view renders
pull/597/head v3.10.1
fadrian06 8 months ago committed by GitHub
commit 39ac87c54b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -20,6 +20,8 @@ class View
/** File extension. */ /** File extension. */
public string $extension = '.php'; public string $extension = '.php';
public bool $preserveVars = true;
/** /**
* View variables. * View variables.
* *
@ -114,12 +116,16 @@ class View
throw new \Exception("Template file not found: {$normalized_path}."); throw new \Exception("Template file not found: {$normalized_path}.");
} }
if (\is_array($data)) {
$this->vars = \array_merge($this->vars, $data);
}
\extract($this->vars); \extract($this->vars);
if (\is_array($data) === true) {
\extract($data);
if ($this->preserveVars === true) {
$this->vars = \array_merge($this->vars, $data);
}
}
include $this->template; include $this->template;
} }

@ -22,6 +22,7 @@ class FlightTest extends TestCase
$_REQUEST = []; $_REQUEST = [];
Flight::init(); Flight::init();
Flight::setEngine(new Engine()); Flight::setEngine(new Engine());
Flight::set('flight.views.path', __DIR__ . '/views');
} }
protected function tearDown(): void protected function tearDown(): void
@ -354,4 +355,43 @@ class FlightTest extends TestCase
$this->expectOutputString('Thisisaroutewithhtml'); $this->expectOutputString('Thisisaroutewithhtml');
} }
/** @dataProvider \tests\ViewTest::renderDataProvider */
public function testDoesNotPreserveVarsWhenFlagIsDisabled(
string $output,
array $renderParams,
string $regexp
): void
{
Flight::view()->preserveVars = false;
$this->expectOutputString($output);
Flight::render(...$renderParams);
set_error_handler(function (int $code, string $message) use ($regexp): void {
$this->assertMatchesRegularExpression($regexp, $message);
});
Flight::render($renderParams[0]);
restore_error_handler();
}
public function testKeepThePreviousStateOfOneViewComponentByDefault(): void
{
$this->expectOutputString(<<<html
<div>Hi</div>
<div>Hi</div>
<input type="number" />
<input type="number" />
html);
Flight::render('myComponent', ['prop' => 'Hi']);
Flight::render('myComponent');
Flight::render('input', ['type' => 'number']);
Flight::render('input');
}
} }

@ -152,4 +152,84 @@ class ViewTest extends TestCase
$viewMock::normalizePath('C:/xampp/htdocs/libs/Flight\core\index.php', '°') $viewMock::normalizePath('C:/xampp/htdocs/libs/Flight\core\index.php', '°')
); );
} }
/** @dataProvider renderDataProvider */
public function testDoesNotPreserveVarsWhenFlagIsDisabled(
string $output,
array $renderParams,
string $regexp
): void {
$this->view->preserveVars = false;
$this->expectOutputString($output);
$this->view->render(...$renderParams);
set_error_handler(function (int $code, string $message) use ($regexp): void {
$this->assertMatchesRegularExpression($regexp, $message);
});
$this->view->render($renderParams[0]);
restore_error_handler();
}
public function testKeepThePreviousStateOfOneViewComponentByDefault(): void
{
$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');
$this->view->render('input', ['type' => 'number']);
$this->view->render('input');
}
public function testKeepThePreviousStateOfDataSettedBySetMethod(): void
{
$this->view->preserveVars = false;
$this->view->set('prop', 'bar');
$this->expectOutputString(<<<html
<div>qux</div>
<div>bar</div>
html);
$this->view->render('myComponent', ['prop' => 'qux']);
$this->view->render('myComponent');
}
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 ?>" />

@ -0,0 +1 @@
<div><?= $prop ?></div>
Loading…
Cancel
Save