Skip to content

Commit

Permalink
[BUGFIX] Fix v:try in compiled context without f:else or with else ar…
Browse files Browse the repository at this point in the history
…gument
  • Loading branch information
NamelessCoder committed Aug 8, 2024
1 parent a0d26ab commit f06986b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Classes/ViewHelpers/TryViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,13 @@ public static function renderStatic(
$content = $arguments['__then']();
} catch (\Exception $error) {
$variableProvider = $renderingContext->getVariableProvider();
$variableProvider->add('exception', $error);
$content = $arguments['__else']();
$variableProvider->remove('exception');
if (isset($arguments['__else'])) {
$variableProvider->add('exception', $error);
$content = $arguments['__else']();
$variableProvider->remove('exception');
} else {
$content = $arguments['else'] ?? null;
}
}
return $content;
}
Expand Down
18 changes: 18 additions & 0 deletions Tests/Unit/ViewHelpers/TryViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public function testRenderStaticWithException(): void
self::assertSame('else case', $output);
}

public function testRenderStaticWithExceptionAndElseArgument(): void
{
$arguments['__then'] = function() { throw new \Exception('test'); };
$arguments['else'] = 'else case';
$output = TryViewHelper::renderStatic($arguments, function() { return ''; }, $this->renderingContext);
self::assertSame('else case', $output);
}

public function testRenderWithException(): void
{
$instance = $this->getMockBuilder($this->getViewHelperClassName())->setMethods(['renderElseChild', 'renderChildren'])->getMock();
Expand All @@ -38,4 +46,14 @@ public function testRenderWithException(): void
$result = $instance->render();
$this->assertEquals('testerror', $result);
}

public function testRenderWithExceptionAndElseArgument(): void
{
$instance = $this->getMockBuilder($this->getViewHelperClassName())->setMethods(['renderChildren'])->getMock();
$instance->setRenderingContext($this->renderingContext);
$instance->setArguments(['else' => 'else']);
$instance->expects($this->once())->method('renderChildren')->willThrowException(new \RuntimeException('testerror'));
$result = $instance->render();
$this->assertEquals('else', $result);
}
}

0 comments on commit f06986b

Please sign in to comment.