Skip to content

Commit

Permalink
[TASK] Add test for recursive partial
Browse files Browse the repository at this point in the history
  • Loading branch information
s2b committed Oct 16, 2023
1 parent 7fa460b commit 252fc56
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 73 deletions.
133 changes: 133 additions & 0 deletions tests/Functional/Cases/Rendering/RecursiveRenderingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

declare(strict_types=1);

/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/

namespace TYPO3Fluid\Fluid\Tests\Functional\Cases\Rendering;

use TYPO3Fluid\Fluid\Tests\Functional\AbstractFunctionalTestCase;
use TYPO3Fluid\Fluid\View\TemplateView;

final class RecursiveRenderingTest extends AbstractFunctionalTestCase
{
/**
* @test
*/
public function recursiveSectionRenderingClonesVariableStorageAndRestoresAfterLoop(): void
{
$source = file_get_contents(__DIR__ . '/../../Fixtures/Templates/RecursiveSectionRendering.html');
$variables = [
'settings' => [
'test' => '<strong>Bla</strong>'
],
'items' => [
[
'id' => 1,
'items' => [
[
'id' => 2,
'items' => [
[
'id' => 3,
'items' => []
]
]
]
]
],
[
'id' => 4
]
]
];
$expectations = [
'Item: 1.',
'Item: 2.',
'Item: 3.',
'Item: 4.'
];

$view = new TemplateView();
$view->getRenderingContext()->setCache(self::$cache);
$view->assignMultiple($variables);
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource($source);
$output = $view->render();
foreach ($expectations as $expectedValue) {
self::assertStringContainsString($expectedValue, $output);
}

// A second run to verify cached template parsing
$view = new TemplateView();
$view->getRenderingContext()->setCache(self::$cache);
$view->assignMultiple($variables);
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource($source);
$output = $view->render();
foreach ($expectations as $expectedValue) {
self::assertStringContainsString($expectedValue, $output);
}
}

/**
* @test
*/
public function recursivePartialRenderingClonesVariableStorageAndRestoresAfterLoop(): void
{
$partialPath = __DIR__ . '/../../Fixtures/Partials/';
$source = file_get_contents(__DIR__ . '/../../Fixtures/Templates/RecursivePartialRendering.html');
$variables = [
'settings' => [
'test' => '<strong>Bla</strong>'
],
'items' => [
[
'id' => 1,
'items' => [
[
'id' => 2,
'items' => [
[
'id' => 3,
'items' => []
]
]
]
]
],
[
'id' => 4
]
]
];
$expectations = [
'Item: 1.',
'Item: 2.',
'Item: 3.',
'Item: 4.'
];

$view = new TemplateView();
$view->getRenderingContext()->setCache(self::$cache);
$view->assignMultiple($variables);
$view->getRenderingContext()->getTemplatePaths()->setPartialRootPaths([$partialPath]);
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource($source);
$output = $view->render();
foreach ($expectations as $expectedValue) {
self::assertStringContainsString($expectedValue, $output);
}

// A second run to verify cached template parsing
$view = new TemplateView();
$view->getRenderingContext()->setCache(self::$cache);
$view->assignMultiple($variables);
$view->getRenderingContext()->getTemplatePaths()->setPartialRootPaths([$partialPath]);
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource($source);
$output = $view->render();
foreach ($expectations as $expectedValue) {
self::assertStringContainsString($expectedValue, $output);
}
}
}
73 changes: 0 additions & 73 deletions tests/Functional/Cases/Rendering/RecursiveSectionRenderingTest.php

This file was deleted.

8 changes: 8 additions & 0 deletions tests/Functional/Fixtures/Partials/RecursivePartial.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<f:spaceless>
<f:for each="{items}" as="item">
Item: {item.id}.
<f:if condition="{item.items}">
<f:render partial="RecursivePartial" arguments="{items: item.items}" />
</f:if>
</f:for>
</f:spaceless>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<f:render partial="RecursivePartial" arguments="{items: items}" />

0 comments on commit 252fc56

Please sign in to comment.