Skip to content

Commit

Permalink
TASK: Introduce TestingViewForFusionRuntime for testing instead of …
Browse files Browse the repository at this point in the history
…using a hacky anonymous class
  • Loading branch information
mhsdesign committed Apr 22, 2024
1 parent 80ce33a commit 13077e6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 58 deletions.
Expand Up @@ -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
Expand All @@ -32,68 +29,18 @@ 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(
FusionSourceCodeCollection::fromFilePath(__DIR__ . '/Fixtures/Fusion/Root.fusion'),
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;
}

/**
Expand Down
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Neos\Fusion\Tests\Functional\FusionObjects;

use Neos\Fusion\Core\Runtime;
use Neos\Fusion\Exception\RuntimeException;

/**
* TODO THIS IS HACKY AS WE CREATE A SHALLOW ABSTRACTION FOR TESTING
*
* We do that as the FusionView (rightfully) doesn't return mixed anymore - but we test arbitrary data.
* {@see https://github.com/neos/neos-development-collection/pull/4856}
*
* We could instead also rewrite all tests to use the Runtime natively.
* But that would be a lot of effort and diff for nothing.
*
* Instead we want to refactor our fusion tests to behat at some point.
*
* Thus the (temporary) hack / abstraction.
*
* @deprecated todo rewrite everything as behat test :D
*/
class TestingViewForFusionRuntime
{
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();
}
}
}
3 changes: 2 additions & 1 deletion Neos.Neos/Tests/Functional/Fusion/NodeHelperTest.php
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -107,7 +108,7 @@ public function crop()
self::assertEquals('Some -', (string)$view->render());
}

protected function buildView()
protected function buildView(): TestingViewForFusionRuntime
{
$view = parent::buildView();

Expand Down

0 comments on commit 13077e6

Please sign in to comment.