Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/Neuron/Core/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,14 @@ public function parse ($template = null, $text = null)
}
}

include $ctlbtmpltfiles[0];
try {
include $ctlbtmpltfiles[0];
} catch (\Throwable $ctlbtmplterror) {
// Close our buffer before rethrowing, or it stays open and
// swallows everything the caller outputs afterwards.
ob_end_clean ();
throw $ctlbtmplterror;
}

$val = ob_get_contents();

Expand Down Expand Up @@ -384,10 +391,15 @@ private function combine ($template, $parameters = [])
${$k} = $v;
}

if ($ctlbtmpltfiles = $this->getFilenames($template, true)) {
foreach ($ctlbtmpltfiles as $ctlbtmpltfile) {
include $ctlbtmpltfile;
try {
if ($ctlbtmpltfiles = $this->getFilenames($template, true)) {
foreach ($ctlbtmpltfiles as $ctlbtmpltfile) {
include $ctlbtmpltfile;
}
}
} catch (\Throwable $ctlbtmplterror) {
ob_end_clean();
throw $ctlbtmplterror;
}

$val = ob_get_contents();
Expand Down Expand Up @@ -418,10 +430,15 @@ private function template ($template, $parameters = [])
${$k} = $v;
}

if ($ctlbtmpltfiles = $this->getFilenames($template)) {
foreach ($ctlbtmpltfiles as $ctlbtmpltfile) {
include $ctlbtmpltfile;
try {
if ($ctlbtmpltfiles = $this->getFilenames($template)) {
foreach ($ctlbtmpltfiles as $ctlbtmpltfile) {
include $ctlbtmpltfile;
}
}
} catch (\Throwable $ctlbtmplterror) {
ob_end_clean();
throw $ctlbtmplterror;
}

$val = ob_get_contents();
Expand Down
56 changes: 56 additions & 0 deletions tests/TemplateOutputBufferTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Neuron\Tests;

use Neuron\Core\Template;
use PHPUnit\Framework\TestCase;

/**
* A template that throws must not leave its output buffer open: the leaked
* buffer would swallow everything the caller prints afterwards (and makes
* PHPUnit re-print its whole output as the failing test's output).
*/
class TemplateOutputBufferTest extends TestCase
{
protected function setUp (): void
{
Template::setPath (__DIR__ . '/templates/');
}

public static function throwingTemplates ()
{
return [
'parse' => [ 'buffer/throws.phpt' ],
'template() inside a template' => [ 'buffer/viaTemplate.phpt' ],
'combine() inside a template' => [ 'buffer/viaCombine.phpt' ],
];
}

/**
* @dataProvider throwingTemplates
*/
public function testThrowingTemplateClosesItsBuffers ($name)
{
$level = ob_get_level ();

try {
(new Template ($name))->parse ();
$this->fail ('Expected the template exception to propagate');
} catch (\RuntimeException $e) {
$this->assertSame ('template failed', $e->getMessage ());
}

$this->assertSame ($level, ob_get_level ());
}

public function testRenderingStillReturnsTheOutput ()
{
$level = ob_get_level ();

$template = new Template ('buffer/fine.phpt');
$template->set ('name', 'world');

$this->assertSame ('hello world', trim ($template->parse ()));
$this->assertSame ($level, ob_get_level ());
}
}
1 change: 1 addition & 0 deletions tests/templates/buffer/fine.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello <?php echo $name; ?>
1 change: 1 addition & 0 deletions tests/templates/buffer/throws.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
partial output<?php throw new \RuntimeException('template failed'); ?>
1 change: 1 addition & 0 deletions tests/templates/buffer/viaCombine.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
outer<?php echo $this->combine('buffer/throws.phpt'); ?>
1 change: 1 addition & 0 deletions tests/templates/buffer/viaTemplate.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
outer<?php echo $this->template('buffer/throws.phpt'); ?>
Loading