comparison vendor/nikic/php-parser/test/PhpParser/Parser/MultipleTest.php @ 0:4c8ae668cc8c

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