Skip to content

Commit

Permalink
Fixes partial output from optional tags
Browse files Browse the repository at this point in the history
The optional tag would still output any contents from before the exception was thrown. This is now fixed.
  • Loading branch information
victorwelling committed Nov 22, 2018
1 parent 6c23378 commit 6268bb0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/Twig/Node/OptionalNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ public function compile(Compiler $compiler)
$compiler
->write("try {\n")
->indent()
->write("ob_start();\n\n")
->subcompile($this->getNode('body'))
->raw("\n")
->write("echo ob_get_clean();\n")
->outdent()
->write("} catch ({$runtimeErrorClass} \$exception) {\n")
->indent()
->write("ob_end_clean();\n\n")
->write("if (\$exception->getPrevious() === null) {\n")
->indent()
->write("throw \$exception;\n")
Expand Down
34 changes: 29 additions & 5 deletions tests/Integration/ErrorSuppression/ErrorSuppressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ final class ErrorSuppressionTest extends IntegrationTestCase
/** @var string */
protected $templateDirectory = __DIR__ . '/Templates';

protected function setUp()
public function testTemplateShouldRenderIfNoExceptionIsThrown()
{
parent::setUp();
$output = $this->renderTemplate('page.twig');

$this->assertContains('<h1>page_title</h1>', $output);
$this->assertContains('<!-- before -->', $output);
$this->assertContains('<p>item</p>', $output);
$this->assertContains('<!-- after -->', $output);
$this->assertContains('<p>page_footer</p>', $output);
}

/**
Expand All @@ -22,6 +28,13 @@ public function testIncludedTemplateShouldThrowAnException()
{
$this->expectExceptionMessage('item_exception');

$this->request
->method('getAttribute')
->will($this->returnValueMap([
['throw_logic_exception', 'n', 'n'],
['throw_runtime_exception', 'n', 'y'],
]));

$this->renderTemplate('item.twig');
}

Expand All @@ -30,10 +43,19 @@ public function testIncludedTemplateShouldThrowAnException()
*/
public function testOptionalBlocksShouldDiscardTheirContentsOnRuntimeExceptions()
{
$this->request
->method('getAttribute')
->will($this->returnValueMap([
['throw_logic_exception', 'n', 'n'],
['throw_runtime_exception', 'n', 'y'],
]));

$output = $this->renderTemplate('page.twig');

$this->assertContains('<h1>page_title</h1>', $output);
$this->assertNotContains('<!-- hidden -->', $output);
$this->assertNotContains('<!-- before -->', $output);
$this->assertNotContains('<p>item</p>', $output);
$this->assertNotContains('<!-- after -->', $output);
$this->assertContains('<p>page_footer</p>', $output);
}

Expand All @@ -46,8 +68,10 @@ public function testOptionalBlocksShouldNotSuppressLogicExceptions()

$this->request
->method('getAttribute')
->with('throw_logic_exception')
->willReturn('y');
->will($this->returnValueMap([
['throw_logic_exception', 'n', 'y'],
['throw_runtime_exception', 'n', 'n'],
]));

$this->renderTemplate('page.twig');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ public function present(ServerRequestInterface $request, PresentationModel $pres
return $presentationModel->withVariables([
'throw_logic_exception' => true,
]);
} elseif ($request->getAttribute('throw_runtime_exception', 'n') === 'y') {
throw new RuntimeException('item_exception');
}

throw new RuntimeException('item_exception');
return $presentationModel;
}
}
1 change: 1 addition & 0 deletions tests/Integration/ErrorSuppression/Templates/item.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
{% if throw_logic_exception %}
{{ unknown_variable }}
{% endif %}
<p>item</p>
3 changes: 2 additions & 1 deletion tests/Integration/ErrorSuppression/Templates/page.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
<body>
<h1>page_title</h1>
{% optional %}
<!-- before -->
{% include 'item.twig' %}
<!-- hidden -->
<!-- after -->
{% endoptional %}
<p>page_footer</p>
</body>
Expand Down

0 comments on commit 6268bb0

Please sign in to comment.