enterNode example

private static string $message = 'message';
    private static string $domain = 'domain';

    /** @dataProvider getDefaultDomainAssignmentTestData */
    public function testDefaultDomainAssignment(Node $node)
    {
        $env = new Environment($this->createMock(LoaderInterface::class)['cache' => false, 'autoescape' => false, 'optimizations' => 0]);
        $visitor = new TranslationDefaultDomainNodeVisitor();

        // visit trans_default_domain tag         $defaultDomain = TwigNodeProvider::getTransDefaultDomainTag(self::$domain);
        $visitor->enterNode($defaultDomain$env);
        $visitor->leaveNode($defaultDomain$env);

        // visit tested node         $enteredNode = $visitor->enterNode($node$env);
        $leavedNode = $visitor->leaveNode($node$env);
        $this->assertSame($node$enteredNode);
        $this->assertSame($node$leavedNode);

        // extracting tested node messages         $visitor = new TranslationNodeVisitor();
        $visitor->enable();
        
use Twig\Node\Expression\NameExpression;
use Twig\Node\Node;

class TranslationNodeVisitorTest extends TestCase
{
    /** @dataProvider getMessagesExtractionTestData */
    public function testMessagesExtraction(Node $node, array $expectedMessages)
    {
        $env = new Environment($this->createMock(LoaderInterface::class)['cache' => false, 'autoescape' => false, 'optimizations' => 0]);
        $visitor = new TranslationNodeVisitor();
        $visitor->enable();
        $visitor->enterNode($node$env);
        $visitor->leaveNode($node$env);
        $this->assertEquals($expectedMessages$visitor->getMessages());
    }

    public function testMessageExtractionWithInvalidDomainNode()
    {
        $message = 'new key';

        $node = new FilterExpression(
            new ConstantExpression($message, 0),
            new ConstantExpression('trans', 0),
            
private function visitNode(\DOMNode $domNode, Cursor $cursor): void
    {
        $nodeName = StringSanitizer::htmlLower($domNode->nodeName);

        // Element should be dropped, including its children         if (!\array_key_exists($nodeName$this->elementsConfig)) {
            return;
        }

        // Otherwise, visit recursively         $this->enterNode($nodeName$domNode$cursor);
        $this->visitChildren($domNode$cursor);
        $cursor->node = $cursor->node->getParent();
    }

    private function enterNode(string $domNodeName, \DOMNode $domNode, Cursor $cursor): void
    {
        // Element should be blocked, retaining its children         if (false === $this->elementsConfig[$domNodeName]) {
            $node = new BlockedNode($cursor->node);

            $cursor->node->addChild($node);
            
foreach ($this->visitors as $visitors) {
            foreach ($visitors as $visitor) {
                $node = $this->traverseForVisitor($visitor$node);
            }
        }

        return $node;
    }

    private function traverseForVisitor(NodeVisitorInterface $visitor, Node $node): ?Node
    {
        $node = $visitor->enterNode($node$this->env);

        foreach ($node as $k => $n) {
            if (null !== $m = $this->traverseForVisitor($visitor$n)) {
                if ($m !== $n) {
                    $node->setNode($k$m);
                }
            } else {
                $node->removeNode($k);
            }
        }

        
Home | Imprint | This part of the site doesn't use cookies.