ReplaceRecursiveFilter example

/** * @internal */
class ReplaceRecursiveFilterTest extends TestCase
{
    use KernelTestBehaviour;

    private ReplaceRecursiveFilter $replaceRecursiveFilter;

    protected function setUp(): void
    {
        $this->replaceRecursiveFilter = new ReplaceRecursiveFilter();
    }

    public function testGetFilterReturnsArrayWithTwigFilter(): void
    {
        $firstTwigFilter = $this->replaceRecursiveFilter->getFilters()[0];
        static::assertInstanceOf(TwigFilter::class$firstTwigFilter);
    }

    public function testIfFilterContainsReplaceRecursive(): void
    {
        $replaceRecursiveFilter = array_filter($this->replaceRecursiveFilter->getFilters()static fn ($filter) => $filter->getName() === 'replace_recursive');

        
/** * @internal * * @covers \Shopware\Core\Framework\Adapter\Twig\Filter\ReplaceRecursiveFilter */
class ReplaceRecursiveFilterTest extends TestCase
{
    public function testReplace(): void
    {
        $env = new Environment(new ArrayLoader(['test' => '{{ {"berries": ["blueberry"]}|replace_recursive({"berries": ["strawberry", "blackberry"]})|json_encode|raw }}']));
        $env->addExtension(new ReplaceRecursiveFilter());

        static::assertSame('{"berries":["strawberry","blackberry"]}', $env->render('test'));
    }
}
private function initEnv(Script $script): Environment
    {
        $twig = new TwigEnvironment(
            new ScriptTwigLoader($script),
            $script->getTwigOptions()
        );

        $twig->addExtension(new PhpSyntaxExtension());
        $twig->addExtension($this->translationExtension);
        $twig->addExtension(new SecurityExtension([]));
        $twig->addExtension(new PcreExtension());
        $twig->addExtension(new ReplaceRecursiveFilter());

        if ($script->getTwigOptions()['debug'] ?? false) {
            $twig->addExtension(new DebugExtension());
        }

        $twig->addGlobal('shopware', new ArrayStruct([
            'version' => $this->shopwareVersion,
        ]));

        return $twig;
    }

    
$options
        );

        $twig = new TwigEnvironment(
            new ScriptTwigLoader($script),
            $script->getTwigOptions()
        );

        $twig->addExtension(new PhpSyntaxExtension());
        $twig->addExtension(new ComparisonExtension());
        $twig->addExtension(new PcreExtension());
        $twig->addExtension(new ReplaceRecursiveFilter());

        if ($this->debug) {
            $twig->addExtension(new DebugExtension());
        }

        $twig->addExtension(new SecurityExtension([]));

        $hook = new AppScriptConditionHook($scope->getContext());

        try {
            return $this->render($twig$script$hook$name$context);
        }
Home | Imprint | This part of the site doesn't use cookies.