annotate vendor/nikic/php-parser/test/PhpParser/Parser/MultipleTest.php @ 2:92f882872392

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