MultipleTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace PhpParser\Parser;
  3. use PhpParser\Error;
  4. use PhpParser\Lexer;
  5. use PhpParser\Node\Expr;
  6. use PhpParser\Node\Scalar\LNumber;
  7. use PhpParser\Node\Stmt;
  8. use PhpParser\ParserTest;
  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. }
  27. public function provideTestParse() {
  28. return [
  29. [
  30. // PHP 7 only code
  31. '<?php class Test { function function() {} }',
  32. $this->getPrefer5(),
  33. [
  34. new Stmt\Class_('Test', ['stmts' => [
  35. new Stmt\ClassMethod('function')
  36. ]]),
  37. ]
  38. ],
  39. [
  40. // PHP 5 only code
  41. '<?php global $$a->b;',
  42. $this->getPrefer7(),
  43. [
  44. new Stmt\Global_([
  45. new Expr\Variable(new Expr\PropertyFetch(new Expr\Variable('a'), 'b'))
  46. ])
  47. ]
  48. ],
  49. [
  50. // Different meaning (PHP 5)
  51. '<?php $$a[0];',
  52. $this->getPrefer5(),
  53. [
  54. new Expr\Variable(
  55. new Expr\ArrayDimFetch(new Expr\Variable('a'), LNumber::fromString('0'))
  56. )
  57. ]
  58. ],
  59. [
  60. // Different meaning (PHP 7)
  61. '<?php $$a[0];',
  62. $this->getPrefer7(),
  63. [
  64. new Expr\ArrayDimFetch(
  65. new Expr\Variable(new Expr\Variable('a')), LNumber::fromString('0')
  66. )
  67. ]
  68. ],
  69. ];
  70. }
  71. public function testThrownError() {
  72. $this->setExpectedException('PhpParser\Error', 'FAIL A');
  73. $parserA = $this->getMockBuilder('PhpParser\Parser')->getMock();
  74. $parserA->expects($this->at(0))
  75. ->method('parse')->will($this->throwException(new Error('FAIL A')));
  76. $parserB = $this->getMockBuilder('PhpParser\Parser')->getMock();
  77. $parserB->expects($this->at(0))
  78. ->method('parse')->will($this->throwException(new Error('FAIL B')));
  79. $parser = new Multiple([$parserA, $parserB]);
  80. $parser->parse('dummy');
  81. }
  82. }