From f06986bda62d34dcebb8d69e24767cf8bacdc782 Mon Sep 17 00:00:00 2001 From: Claus Due Date: Thu, 8 Aug 2024 12:50:31 +0200 Subject: [PATCH] [BUGFIX] Fix v:try in compiled context without f:else or with else argument --- Classes/ViewHelpers/TryViewHelper.php | 10 +++++++--- Tests/Unit/ViewHelpers/TryViewHelperTest.php | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Classes/ViewHelpers/TryViewHelper.php b/Classes/ViewHelpers/TryViewHelper.php index 9258cb512..be6edbba5 100644 --- a/Classes/ViewHelpers/TryViewHelper.php +++ b/Classes/ViewHelpers/TryViewHelper.php @@ -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; } diff --git a/Tests/Unit/ViewHelpers/TryViewHelperTest.php b/Tests/Unit/ViewHelpers/TryViewHelperTest.php index b5003ef81..3ce96d185 100644 --- a/Tests/Unit/ViewHelpers/TryViewHelperTest.php +++ b/Tests/Unit/ViewHelpers/TryViewHelperTest.php @@ -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(); @@ -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); + } }