Chris@13
|
1 <?php declare(strict_types=1);
|
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@13
|
12 class MultipleTest extends ParserTest
|
Chris@13
|
13 {
|
Chris@0
|
14 // This provider is for the generic parser tests, just pick an arbitrary order here
|
Chris@0
|
15 protected function getParser(Lexer $lexer) {
|
Chris@0
|
16 return new Multiple([new Php5($lexer), new Php7($lexer)]);
|
Chris@0
|
17 }
|
Chris@0
|
18
|
Chris@0
|
19 private function getPrefer7() {
|
Chris@0
|
20 $lexer = new Lexer(['usedAttributes' => []]);
|
Chris@0
|
21 return new Multiple([new Php7($lexer), new Php5($lexer)]);
|
Chris@0
|
22 }
|
Chris@0
|
23
|
Chris@0
|
24 private function getPrefer5() {
|
Chris@0
|
25 $lexer = new Lexer(['usedAttributes' => []]);
|
Chris@0
|
26 return new Multiple([new Php5($lexer), new Php7($lexer)]);
|
Chris@0
|
27 }
|
Chris@0
|
28
|
Chris@0
|
29 /** @dataProvider provideTestParse */
|
Chris@0
|
30 public function testParse($code, Multiple $parser, $expected) {
|
Chris@0
|
31 $this->assertEquals($expected, $parser->parse($code));
|
Chris@0
|
32 }
|
Chris@0
|
33
|
Chris@0
|
34 public function provideTestParse() {
|
Chris@0
|
35 return [
|
Chris@0
|
36 [
|
Chris@0
|
37 // PHP 7 only code
|
Chris@0
|
38 '<?php class Test { function function() {} }',
|
Chris@0
|
39 $this->getPrefer5(),
|
Chris@0
|
40 [
|
Chris@0
|
41 new Stmt\Class_('Test', ['stmts' => [
|
Chris@0
|
42 new Stmt\ClassMethod('function')
|
Chris@0
|
43 ]]),
|
Chris@0
|
44 ]
|
Chris@0
|
45 ],
|
Chris@0
|
46 [
|
Chris@0
|
47 // PHP 5 only code
|
Chris@0
|
48 '<?php global $$a->b;',
|
Chris@0
|
49 $this->getPrefer7(),
|
Chris@0
|
50 [
|
Chris@0
|
51 new Stmt\Global_([
|
Chris@0
|
52 new Expr\Variable(new Expr\PropertyFetch(new Expr\Variable('a'), 'b'))
|
Chris@0
|
53 ])
|
Chris@0
|
54 ]
|
Chris@0
|
55 ],
|
Chris@0
|
56 [
|
Chris@0
|
57 // Different meaning (PHP 5)
|
Chris@0
|
58 '<?php $$a[0];',
|
Chris@0
|
59 $this->getPrefer5(),
|
Chris@0
|
60 [
|
Chris@13
|
61 new Stmt\Expression(new Expr\Variable(
|
Chris@0
|
62 new Expr\ArrayDimFetch(new Expr\Variable('a'), LNumber::fromString('0'))
|
Chris@13
|
63 ))
|
Chris@0
|
64 ]
|
Chris@0
|
65 ],
|
Chris@0
|
66 [
|
Chris@0
|
67 // Different meaning (PHP 7)
|
Chris@0
|
68 '<?php $$a[0];',
|
Chris@0
|
69 $this->getPrefer7(),
|
Chris@0
|
70 [
|
Chris@13
|
71 new Stmt\Expression(new Expr\ArrayDimFetch(
|
Chris@0
|
72 new Expr\Variable(new Expr\Variable('a')), LNumber::fromString('0')
|
Chris@13
|
73 ))
|
Chris@0
|
74 ]
|
Chris@0
|
75 ],
|
Chris@0
|
76 ];
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 public function testThrownError() {
|
Chris@13
|
80 $this->expectException(Error::class);
|
Chris@13
|
81 $this->expectExceptionMessage('FAIL A');
|
Chris@0
|
82
|
Chris@13
|
83 $parserA = $this->getMockBuilder(\PhpParser\Parser::class)->getMock();
|
Chris@0
|
84 $parserA->expects($this->at(0))
|
Chris@17
|
85 ->method('parse')->willThrowException(new Error('FAIL A'));
|
Chris@0
|
86
|
Chris@13
|
87 $parserB = $this->getMockBuilder(\PhpParser\Parser::class)->getMock();
|
Chris@0
|
88 $parserB->expects($this->at(0))
|
Chris@17
|
89 ->method('parse')->willThrowException(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@13
|
94 }
|