Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions src/Twig/Parser/ContentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,9 @@ private function callFunction(
array $arguments,
): string {
Assert::keyExists($functions, $functionName, sprintf('Function %s does not exist!', $functionName));
/** @var TwigFunction $function */
$function = $functions[$functionName];
$callable = $function->getCallable();
Assert::isArray($callable, sprintf('Function with name "%s" is not callable', $functionName));
$extension = $callable[0];
$extensionMethod = $callable[1];
$callback = [$extension, $extensionMethod];
Assert::isCallable($callback);

return call_user_func_array($callback, $arguments);
$callable = $functions[$functionName]->getCallable();
Assert::isCallable($callable, sprintf('Function with name "%s" is not callable', $functionName));

return call_user_func_array($callable, $arguments);
}
}
52 changes: 52 additions & 0 deletions tests/Unit/Twig/Parser/ContentParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,56 @@ public function testParsesStringFunctions(): void

self::assertSame("Let's render! parsed\nLet's render twice! parsed2", $this->contentParser->parse($input));
}

public function testParsesWithFirstClassCallable(): void
{
$twigEnvironment = $this->createMock(Environment::class);
$twigEnvironment
->method('getFunctions')
->willReturn([
'app_render_gallery' => new TwigFunction('app_render_gallery', $this->renderGallery(...)),
])
;

$contentParser = new ContentParser($twigEnvironment, ['app_render_gallery']);

self::assertSame(
'gallery: summer',
$contentParser->parse("{{ app_render_gallery('summer') }}"),
);
}

public function testSkipsDisabledFunction(): void
{
$this->renderBlockRuntime
->expects(self::never())
->method('renderBlock')
;

$contentParser = new ContentParser($this->twigEnvironment, []);
$input = "{{ sylius_cms_render_block('intro') }}";

self::assertSame($input, $contentParser->parse($input));
}

public function testReplacesWithEmptyStringWhenFunctionNotRegisteredInTwig(): void
{
$twigEnvironment = $this->createMock(Environment::class);
$twigEnvironment
->method('getFunctions')
->willReturn([])
;

$contentParser = new ContentParser($twigEnvironment, ['sylius_cms_render_block']);

self::assertSame(
'',
$contentParser->parse("{{ sylius_cms_render_block('intro') }}"),
);
}

private function renderGallery(string $slug): string
{
return sprintf('gallery: %s', $slug);
}
}
Loading