Skip to content

Commit

Permalink
Merge pull request #20 from netlogix/bugfix/xhrCache
Browse files Browse the repository at this point in the history
fix: add xhrCache to InlineSettingArray
  • Loading branch information
tweis authored Jun 19, 2024
2 parents bfe8639 + e72fad9 commit eb7561f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
26 changes: 21 additions & 5 deletions Classes/ViewHelpers/XhrCacheViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@
namespace Netlogix\Nxstyleguide\ViewHelpers;

use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;

class XhrCacheViewHelper extends AbstractTagBasedViewHelper
{
protected $tagName = 'script';

public function __construct(
protected ?PageRenderer $pageRenderer = null
) {
parent::__construct();
$this->pageRenderer ??= GeneralUtility::makeInstance(PageRenderer::class);
}

public function initializeArguments(): void
{
$this->registerArgument('url', 'string', '', false);
Expand All @@ -36,16 +45,23 @@ public function render(): string
$data = $data->toArray();
}

$url = $this->hasArgument('url') ? \json_encode($this->arguments['url'], JSON_THROW_ON_ERROR) : 'window.location.href';
$data = \json_encode($data, Environment::getContext()->isDevelopment() ? JSON_PRETTY_PRINT : 0);

$replace = [
'"{url}"' => $this->hasArgument('url') ? \json_encode(
$this->arguments['url'],
JSON_THROW_ON_ERROR
) : 'window.location.href',
'"{data}"' => \json_encode($data, Environment::getContext()->isDevelopment() ? JSON_PRETTY_PRINT : 0),
'"{url}"' => $url,
'"{data}"' => $data,
];

$content = \str_replace(array_keys($replace), array_values($replace), $body);

$this->pageRenderer->addInlineSettingArray('xhrCache', [
[
'data' => json_decode($data, true),
'url' => json_decode($url, true),
],
]);

$this->tag->setContent($content);

return $this->tag->render();
Expand Down
20 changes: 15 additions & 5 deletions Tests/Unit/ViewHelpers/XhrCacheViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use LogicException;
use Netlogix\Nxstyleguide\ViewHelpers\XhrCacheViewHelper;
use Netlogix\Nxvarnish\Xclass\Controller\TypoScriptFrontendController;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

Expand All @@ -15,7 +17,7 @@ class XhrCacheViewHelperTest extends UnitTestCase
#[Test]
public function initializeArguments_should_registerArguments(): void
{
$subject = $this->getAccessibleMock(XhrCacheViewHelper::class, ['registerArgument']);
$subject = $this->getAccessibleMock(XhrCacheViewHelper::class, ['registerArgument'], [$this->getPageRendererMock()]);
$subject->expects($this->exactly(2))
->method('registerArgument')
->willReturnCallback(static fn ($name, $type, $description, $required) => match (true) {
Expand All @@ -30,7 +32,7 @@ public function initializeArguments_should_registerArguments(): void
#[Test]
public function should_set_window_location_href_when_no_url_given(): void
{
$subject = new XhrCacheViewHelper();
$subject = new XhrCacheViewHelper($this->getPageRendererMock());
$result = $subject->initializeArgumentsAndRender();

$this->assertStringContainsString('url: window.location.href', $result);
Expand All @@ -39,7 +41,7 @@ public function should_set_window_location_href_when_no_url_given(): void
#[Test]
public function should_set_data_null_when_no_data_given(): void
{
$subject = new XhrCacheViewHelper();
$subject = new XhrCacheViewHelper($this->getPageRendererMock());
$result = $subject->initializeArgumentsAndRender();

$this->assertStringContainsString('data: null', $result);
Expand All @@ -48,7 +50,7 @@ public function should_set_data_null_when_no_data_given(): void
#[Test]
public function should_set_data_when_data_is_given(): void
{
$subject = new XhrCacheViewHelper();
$subject = new XhrCacheViewHelper($this->getPageRendererMock());
$subject->setArguments([
'content' => 42,
]);
Expand All @@ -66,12 +68,20 @@ public function should_convert_data_to_array_if_QueryResultInterface(): void
$queryResult->method('toArray')
->willReturn([['1'], ['2']]);

$subject = new XhrCacheViewHelper();
$subject = new XhrCacheViewHelper($this->getPageRendererMock());
$subject->setArguments([
'content' => $queryResult,
]);
$result = $subject->initializeArgumentsAndRender();

$this->assertStringContainsString('data: [["1"],["2"]]', $result);
}

private function getPageRendererMock(): PageRenderer
{
return $this->getMockBuilder(PageRenderer::class)
->disableOriginalConstructor()
->addMethods([])
->getMock();
}
}

0 comments on commit eb7561f

Please sign in to comment.