Skip to content

Commit

Permalink
Merge pull request #581 from flightphp/fix/bug-with-multiple-view-ren…
Browse files Browse the repository at this point in the history
…ders

Fix/bug with multiple view renders
  • Loading branch information
fadrian06 authored Jun 3, 2024
2 parents c8a1c88 + a68fc32 commit 39ac87c
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 4 deletions.
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>

0 comments on commit 39ac87c

Please sign in to comment.