);
}
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Invalid node structure: Contains nested arrays
- */
public function testInvalidDeepArray() {
+ $this->expectException(\LogicException::class);
+ $this->expectExceptionMessage('Invalid node structure: Contains nested arrays');
$strNode = new String_('Foo');
$stmts = [[[$strNode]]];
$this->assertEquals($stmts, $traverser->traverse($stmts));
}
+ public function testDontTraverseCurrentAndChildren() {
+ // print 'str'; -($foo * $foo);
+ $strNode = new String_('str');
+ $printNode = new Expr\Print_($strNode);
+ $varNode = new Expr\Variable('foo');
+ $mulNode = new Expr\BinaryOp\Mul($varNode, $varNode);
+ $divNode = new Expr\BinaryOp\Div($varNode, $varNode);
+ $negNode = new Expr\UnaryMinus($mulNode);
+ $stmts = [$printNode, $negNode];
+
+ $visitor1 = $this->getMockBuilder(NodeVisitor::class)->getMock();
+ $visitor2 = $this->getMockBuilder(NodeVisitor::class)->getMock();
+
+ $visitor1->expects($this->at(1))->method('enterNode')->with($printNode)
+ ->will($this->returnValue(NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN));
+ $visitor1->expects($this->at(2))->method('leaveNode')->with($printNode);
+
+ $visitor1->expects($this->at(3))->method('enterNode')->with($negNode);
+ $visitor2->expects($this->at(1))->method('enterNode')->with($negNode);
+
+ $visitor1->expects($this->at(4))->method('enterNode')->with($mulNode)
+ ->will($this->returnValue(NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN));
+ $visitor1->expects($this->at(5))->method('leaveNode')->with($mulNode)->willReturn($divNode);
+
+ $visitor1->expects($this->at(6))->method('leaveNode')->with($negNode);
+ $visitor2->expects($this->at(2))->method('leaveNode')->with($negNode);
+
+ $traverser = new NodeTraverser;
+ $traverser->addVisitor($visitor1);
+ $traverser->addVisitor($visitor2);
+
+ $resultStmts = $traverser->traverse($stmts);
+
+ $this->assertInstanceOf(Expr\BinaryOp\Div::class, $resultStmts[1]->expr);
+ }
+
public function testStopTraversal() {
$varNode1 = new Expr\Variable('a');
$varNode2 = new Expr\Variable('b');