MultipleTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace PhpParser\Parser;
  3. use PhpParser\Error;
  4. use PhpParser\Lexer;
  5. use PhpParser\Node\Scalar\LNumber;
  6. use PhpParser\ParserTest;
  7. use PhpParser\Node\Expr;
  8. use PhpParser\Node\Stmt;
  9. require_once __DIR__ . '/../ParserTest.php';
  10. class MultipleTest extends ParserTest {
  11. // This provider is for the generic parser tests, just pick an arbitrary order here
  12. protected function getParser(Lexer $lexer) {
  13. return new Multiple([new Php5($lexer), new Php7($lexer)]);
  14. }
  15. private function getPrefer7() {
  16. $lexer = new Lexer(['usedAttributes' => []]);
  17. return new Multiple([new Php7($lexer), new Php5($lexer)]);
  18. }
  19. private function getPrefer5() {
  20. $lexer = new Lexer(['usedAttributes' => []]);
  21. return new Multiple([new Php5($lexer), new Php7($lexer)]);
  22. }
  23. /** @dataProvider provideTestParse */
  24. public function testParse($code, Multiple $parser, $expected) {
  25. $this->assertEquals($expected, $parser->parse($code));
  26. $this->assertSame([], $parser->getErrors());
  27. }
  28. public function provideTestParse() {
  29. return [
  30. [
  31. // PHP 7 only code
  32. '<?php class Test { function function() {} }',
  33. $this->getPrefer5(),
  34. [
  35. new Stmt\Class_('Test', ['stmts' => [
  36. new Stmt\ClassMethod('function')
  37. ]]),
  38. ]
  39. ],
  40. [
  41. // PHP 5 only code
  42. '<?php global $$a->b;',
  43. $this->getPrefer7(),
  44. [
  45. new Stmt\Global_([
  46. new Expr\Variable(new Expr\PropertyFetch(new Expr\Variable('a'), 'b'))
  47. ])
  48. ]
  49. ],
  50. [
  51. // Different meaning (PHP 5)
  52. '<?php $$a[0];',
  53. $this->getPrefer5(),
  54. [
  55. new Expr\Variable(
  56. new Expr\ArrayDimFetch(new Expr\Variable('a'), LNumber::fromString('0'))
  57. )
  58. ]
  59. ],
  60. [
  61. // Different meaning (PHP 7)
  62. '<?php $$a[0];',
  63. $this->getPrefer7(),
  64. [
  65. new Expr\ArrayDimFetch(
  66. new Expr\Variable(new Expr\Variable('a')), LNumber::fromString('0')
  67. )
  68. ]
  69. ],
  70. ];
  71. }
  72. public function testThrownError() {
  73. $this->setExpectedException('PhpParser\Error', 'FAIL A');
  74. $parserA = $this->getMockBuilder('PhpParser\Parser')->getMock();
  75. $parserA->expects($this->at(0))
  76. ->method('parse')->will($this->throwException(new Error('FAIL A')));
  77. $parserB = $this->getMockBuilder('PhpParser\Parser')->getMock();
  78. $parserB->expects($this->at(0))
  79. ->method('parse')->will($this->throwException(new Error('FAIL B')));
  80. $parser = new Multiple([$parserA, $parserB]);
  81. $parser->parse('dummy');
  82. }
  83. public function testGetErrors() {
  84. $errorsA = [new Error('A1'), new Error('A2')];
  85. $parserA = $this->getMockBuilder('PhpParser\Parser')->getMock();
  86. $parserA->expects($this->at(0))->method('parse');
  87. $parserA->expects($this->at(1))
  88. ->method('getErrors')->will($this->returnValue($errorsA));
  89. $errorsB = [new Error('B1'), new Error('B2')];
  90. $parserB = $this->getMockBuilder('PhpParser\Parser')->getMock();
  91. $parserB->expects($this->at(0))->method('parse');
  92. $parserB->expects($this->at(1))
  93. ->method('getErrors')->will($this->returnValue($errorsB));
  94. $parser = new Multiple([$parserA, $parserB]);
  95. $parser->parse('dummy');
  96. $this->assertSame($errorsA, $parser->getErrors());
  97. }
  98. }