Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/bug with multiple view renders #581

Merged
merged 6 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions flight/template/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class View
/** File extension. */
public string $extension = '.php';

public bool $preserveVars = true;

/**
* View variables.
*
Expand Down Expand Up @@ -114,12 +116,16 @@ 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);

if (\is_array($data) === true) {
\extract($data);

if ($this->preserveVars === true) {
$this->vars = \array_merge($this->vars, $data);
}
}

include $this->template;
}

Expand Down
40 changes: 40 additions & 0 deletions tests/FlightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -354,4 +355,43 @@ public function after()

$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');
}
}
80 changes: 80 additions & 0 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,84 @@ public static function normalizePath(string $path, string $separator = DIRECTORY
$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']],
'/^.*$/'
],
];
}
}
7 changes: 7 additions & 0 deletions tests/views/input.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$type ??= 'text';

?>

<input type="<?= $type ?>" />
1 change: 1 addition & 0 deletions tests/views/myComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div><?= $prop ?></div>