From 13077e6d974aba046c6b84cbdb6f32f25267d1f7 Mon Sep 17 00:00:00 2001 From: mhsdesign <85400359+mhsdesign@users.noreply.github.com> Date: Mon, 22 Apr 2024 10:07:24 +0200 Subject: [PATCH] TASK: Introduce `TestingViewForFusionRuntime` for testing instead of using a hacky anonymous class --- .../AbstractFusionObjectTest.php | 61 ++--------------- .../TestingViewForFusionRuntime.php | 67 +++++++++++++++++++ .../Functional/Fusion/NodeHelperTest.php | 3 +- 3 files changed, 73 insertions(+), 58 deletions(-) create mode 100644 Neos.Fusion/Tests/Functional/FusionObjects/TestingViewForFusionRuntime.php diff --git a/Neos.Fusion/Tests/Functional/FusionObjects/AbstractFusionObjectTest.php b/Neos.Fusion/Tests/Functional/FusionObjects/AbstractFusionObjectTest.php index fabf9962426..452b61d6e6e 100644 --- a/Neos.Fusion/Tests/Functional/FusionObjects/AbstractFusionObjectTest.php +++ b/Neos.Fusion/Tests/Functional/FusionObjects/AbstractFusionObjectTest.php @@ -16,10 +16,7 @@ use Neos\Flow\Tests\FunctionalTestCase; use Neos\Fusion\Core\FusionGlobals; use Neos\Fusion\Core\FusionSourceCodeCollection; -use Neos\Fusion\Core\Runtime; use Neos\Fusion\Core\RuntimeFactory; -use Neos\Fusion\Exception\RuntimeException; -use Psr\Http\Message\ServerRequestFactoryInterface; /** * Testcase for the Fusion View @@ -32,22 +29,8 @@ abstract class AbstractFusionObjectTest extends FunctionalTestCase */ protected $request; - /** - * TODO THIS IS HACKY AS WE CREATE AN OWN VIEW - * - * We do that as the FusionView (rightfully) doesn't return mixed anymore. - * - * We could instead also rewrite all tests to use the Runtime instead. - * - * But that would be a lot of effort for nothing. - * - * Instead we want to refactor our tests to behat at some point. - * - * Thus the hack. - */ - protected function buildView() + protected function buildView(): TestingViewForFusionRuntime { - /** @var ServerRequestFactoryInterface $httpRequestFactory */ $this->request = ActionRequest::fromHttpRequest(new ServerRequest('GET', 'http://localhost/')); $runtime = $this->objectManager->get(RuntimeFactory::class)->createFromSourceCode( @@ -55,45 +38,9 @@ protected function buildView() FusionGlobals::fromArray(['request' => $this->request]) ); - $runtime->pushContext('fixtureDirectory', __DIR__ . '/Fixtures/'); - - // todo rewrite everything as behat test :D - return new class($runtime) { - private string $fusionPath; - public function __construct( - private readonly Runtime $runtime - ) { - } - public function setFusionPath(string $fusionPath) - { - $this->fusionPath = $fusionPath; - } - public function assign($key, $value) - { - $this->runtime->pushContext($key, $value); - } - public function setOption($key, $value) - { - match ($key) { - 'enableContentCache' => $this->runtime->setEnableContentCache($value), - 'debugMode' => $this->runtime->setDebugMode($value) - }; - } - public function assignMultiple(array $values) - { - foreach ($values as $key => $value) { - $this->runtime->pushContext($key, $value); - } - } - public function render() - { - try { - return $this->runtime->render($this->fusionPath); - } catch (RuntimeException $e) { - throw $e->getWrappedException(); - } - } - }; + $view = new TestingViewForFusionRuntime($runtime); + $view->assign('fixtureDirectory', __DIR__ . '/Fixtures/'); + return $view; } /** diff --git a/Neos.Fusion/Tests/Functional/FusionObjects/TestingViewForFusionRuntime.php b/Neos.Fusion/Tests/Functional/FusionObjects/TestingViewForFusionRuntime.php new file mode 100644 index 00000000000..aa03688ce27 --- /dev/null +++ b/Neos.Fusion/Tests/Functional/FusionObjects/TestingViewForFusionRuntime.php @@ -0,0 +1,67 @@ +fusionPath = $fusionPath; + } + + public function assign($key, $value) + { + $this->runtime->pushContext($key, $value); + } + + public function setOption($key, $value) + { + match ($key) { + 'enableContentCache' => $this->runtime->setEnableContentCache($value), + 'debugMode' => $this->runtime->setDebugMode($value) + }; + } + + public function assignMultiple(array $values) + { + foreach ($values as $key => $value) { + $this->runtime->pushContext($key, $value); + } + } + + public function render() + { + try { + return $this->runtime->render($this->fusionPath); + } catch (RuntimeException $e) { + throw $e->getWrappedException(); + } + } +} diff --git a/Neos.Neos/Tests/Functional/Fusion/NodeHelperTest.php b/Neos.Neos/Tests/Functional/Fusion/NodeHelperTest.php index 9952d6a776b..fc7b35fd719 100644 --- a/Neos.Neos/Tests/Functional/Fusion/NodeHelperTest.php +++ b/Neos.Neos/Tests/Functional/Fusion/NodeHelperTest.php @@ -30,6 +30,7 @@ use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId; use Neos\ContentRepository\TestSuite\Unit\NodeSubjectProvider; use Neos\Fusion\Tests\Functional\FusionObjects\AbstractFusionObjectTest; +use Neos\Fusion\Tests\Functional\FusionObjects\TestingViewForFusionRuntime; use PHPUnit\Framework\MockObject\MockObject; /** @@ -107,7 +108,7 @@ public function crop() self::assertEquals('Some -', (string)$view->render()); } - protected function buildView() + protected function buildView(): TestingViewForFusionRuntime { $view = parent::buildView();