Mercurial > hg > isophonics-drupal-site
diff vendor/nikic/php-parser/test/PhpParser/NodeTraverserTest.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | 5fb285c0d0e3 |
children |
line wrap: on
line diff
--- a/vendor/nikic/php-parser/test/PhpParser/NodeTraverserTest.php Tue Jul 10 15:07:59 2018 +0100 +++ b/vendor/nikic/php-parser/test/PhpParser/NodeTraverserTest.php Thu Feb 28 13:21:36 2019 +0000 @@ -4,10 +4,8 @@ use PhpParser\Node\Expr; use PhpParser\Node\Scalar\String_; -use PhpParser\NodeVisitor; -use PHPUnit\Framework\TestCase; -class NodeTraverserTest extends TestCase +class NodeTraverserTest extends \PHPUnit\Framework\TestCase { public function testNonModifying() { $str1Node = new String_('Foo'); @@ -43,32 +41,32 @@ // replace empty statements with string1 node $visitor1->expects($this->at(0))->method('beforeTraverse')->with([]) - ->will($this->returnValue([$str1Node])); + ->willReturn([$str1Node]); $visitor2->expects($this->at(0))->method('beforeTraverse')->with([$str1Node]); // replace string1 node with print node $visitor1->expects($this->at(1))->method('enterNode')->with($str1Node) - ->will($this->returnValue($printNode)); + ->willReturn($printNode); $visitor2->expects($this->at(1))->method('enterNode')->with($printNode); // replace string1 node with string2 node $visitor1->expects($this->at(2))->method('enterNode')->with($str1Node) - ->will($this->returnValue($str2Node)); + ->willReturn($str2Node); $visitor2->expects($this->at(2))->method('enterNode')->with($str2Node); // replace string2 node with string1 node again $visitor1->expects($this->at(3))->method('leaveNode')->with($str2Node) - ->will($this->returnValue($str1Node)); + ->willReturn($str1Node); $visitor2->expects($this->at(3))->method('leaveNode')->with($str1Node); // replace print node with string1 node again $visitor1->expects($this->at(4))->method('leaveNode')->with($printNode) - ->will($this->returnValue($str1Node)); + ->willReturn($str1Node); $visitor2->expects($this->at(4))->method('leaveNode')->with($str1Node); // replace string1 node with empty statements again $visitor1->expects($this->at(5))->method('afterTraverse')->with([$str1Node]) - ->will($this->returnValue([])); + ->willReturn([]); $visitor2->expects($this->at(5))->method('afterTraverse')->with([]); $traverser = new NodeTraverser; @@ -87,7 +85,7 @@ // remove the string1 node, leave the string2 node $visitor->expects($this->at(2))->method('leaveNode')->with($str1Node) - ->will($this->returnValue(NodeTraverser::REMOVE_NODE)); + ->willReturn(NodeTraverser::REMOVE_NODE); $traverser = new NodeTraverser; $traverser->addVisitor($visitor); @@ -106,7 +104,7 @@ // replace strMiddle with strR1 and strR2 by merge $visitor->expects($this->at(4))->method('leaveNode')->with($strMiddle) - ->will($this->returnValue([$strR1, $strR2])); + ->willReturn([$strR1, $strR2]); $traverser = new NodeTraverser; $traverser->addVisitor($visitor); @@ -117,11 +115,9 @@ ); } - /** - * @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]]]; @@ -141,7 +137,7 @@ $visitor2 = $this->getMockBuilder(NodeVisitor::class)->getMock(); $visitor1->expects($this->at(1))->method('enterNode')->with($printNode) - ->will($this->returnValue(NodeTraverser::DONT_TRAVERSE_CHILDREN)); + ->willReturn(NodeTraverser::DONT_TRAVERSE_CHILDREN); $visitor2->expects($this->at(1))->method('enterNode')->with($printNode); $visitor1->expects($this->at(2))->method('leaveNode')->with($printNode); @@ -152,7 +148,7 @@ $visitor1->expects($this->at(4))->method('enterNode')->with($mulNode); $visitor2->expects($this->at(4))->method('enterNode')->with($mulNode) - ->will($this->returnValue(NodeTraverser::DONT_TRAVERSE_CHILDREN)); + ->willReturn(NodeTraverser::DONT_TRAVERSE_CHILDREN); $visitor1->expects($this->at(5))->method('leaveNode')->with($mulNode); $visitor2->expects($this->at(5))->method('leaveNode')->with($mulNode); @@ -167,6 +163,42 @@ $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) + ->willReturn(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) + ->willReturn(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'); @@ -178,7 +210,7 @@ // From enterNode() with array parent $visitor = $this->getMockBuilder(NodeVisitor::class)->getMock(); $visitor->expects($this->at(1))->method('enterNode')->with($mulNode) - ->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL)); + ->willReturn(NodeTraverser::STOP_TRAVERSAL); $visitor->expects($this->at(2))->method('afterTraverse'); $traverser = new NodeTraverser; $traverser->addVisitor($visitor); @@ -187,7 +219,7 @@ // From enterNode with Node parent $visitor = $this->getMockBuilder(NodeVisitor::class)->getMock(); $visitor->expects($this->at(2))->method('enterNode')->with($varNode1) - ->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL)); + ->willReturn(NodeTraverser::STOP_TRAVERSAL); $visitor->expects($this->at(3))->method('afterTraverse'); $traverser = new NodeTraverser; $traverser->addVisitor($visitor); @@ -196,7 +228,7 @@ // From leaveNode with Node parent $visitor = $this->getMockBuilder(NodeVisitor::class)->getMock(); $visitor->expects($this->at(3))->method('leaveNode')->with($varNode1) - ->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL)); + ->willReturn(NodeTraverser::STOP_TRAVERSAL); $visitor->expects($this->at(4))->method('afterTraverse'); $traverser = new NodeTraverser; $traverser->addVisitor($visitor); @@ -205,7 +237,7 @@ // From leaveNode with array parent $visitor = $this->getMockBuilder(NodeVisitor::class)->getMock(); $visitor->expects($this->at(6))->method('leaveNode')->with($mulNode) - ->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL)); + ->willReturn(NodeTraverser::STOP_TRAVERSAL); $visitor->expects($this->at(7))->method('afterTraverse'); $traverser = new NodeTraverser; $traverser->addVisitor($visitor); @@ -214,9 +246,9 @@ // Check that pending array modifications are still carried out $visitor = $this->getMockBuilder(NodeVisitor::class)->getMock(); $visitor->expects($this->at(6))->method('leaveNode')->with($mulNode) - ->will($this->returnValue(NodeTraverser::REMOVE_NODE)); + ->willReturn(NodeTraverser::REMOVE_NODE); $visitor->expects($this->at(7))->method('enterNode')->with($printNode) - ->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL)); + ->willReturn(NodeTraverser::STOP_TRAVERSAL); $visitor->expects($this->at(8))->method('afterTraverse'); $traverser = new NodeTraverser; $traverser->addVisitor($visitor);