@@ -22,12 +22,38 @@ function($filename) {
2222 return array_values ($ files );
2323 }
2424
25+ /**
26+ * @return bool
27+ */
28+ public static function hasNativeASTSupport (int $ astVersion ) {
29+ try {
30+ $ x = \ast \parse_code ('' , $ astVersion );
31+ return true ;
32+ } catch (\LogicException $ e ) {
33+ return false ;
34+ }
35+ }
36+
37+ /**
38+ * @return string[]|int[] [string $filePath, int $astVersion]
39+ */
2540 public function astValidFileExampleProvider () {
2641 $ tests = [];
2742 $ sourceDir = dirname (dirname (realpath (__DIR__ ))) . '/test_files/src ' ;
2843 $ files = $ this ->_scanSourceDirForPHP ($ sourceDir );
44+ $ supports40 = self ::hasNativeASTSupport (40 );
45+ $ supports50 = self ::hasNativeASTSupport (50 );
46+ if (!($ supports40 || $ supports50 )) {
47+ throw new RuntimeException ("Neither AST version 40 nor 50 are natively supported " );
48+ }
2949 foreach ($ files as $ file ) {
30- $ tests [] = [$ sourceDir . '/ ' . $ file ];
50+ $ path = $ sourceDir . '/ ' . $ file ;
51+ if ($ supports40 ) {
52+ $ tests [] = [$ path , 40 ];
53+ }
54+ if ($ supports50 ) {
55+ $ tests [] = [$ path , 50 ];
56+ }
3157 }
3258 return $ tests ;
3359 }
@@ -52,15 +78,15 @@ private static function normalizeOriginalAST($node) {
5278 }
5379
5480 /** @dataProvider astValidFileExampleProvider */
55- public function testFallbackFromParser (string $ fileName ) {
81+ public function testFallbackFromParser (string $ fileName, int $ astVersion ) {
5682 $ contents = file_get_contents ($ fileName );
5783 if ($ contents === false ) {
5884 $ this ->fail ("Failed to read $ fileName " );
5985 }
60- $ ast = \ast \parse_code ($ contents , ASTConverter:: AST_VERSION );
86+ $ ast = \ast \parse_code ($ contents , $ astVersion );
6187 self ::normalizeOriginalAST ($ ast );
6288 $ this ->assertInstanceOf ('\ast\Node ' , $ ast , 'Examples must be syntactically valid PHP parseable by php-ast ' );
63- $ fallback_ast = \ASTConverter \ASTConverter::ast_parse_code_fallback ($ contents , ASTConverter:: AST_VERSION );
89+ $ fallback_ast = \ASTConverter \ASTConverter::ast_parse_code_fallback ($ contents , $ astVersion );
6490 $ this ->assertInstanceOf ('\ast\Node ' , $ fallback_ast , 'The fallback must also return a tree of php-ast nodes ' );
6591 $ fallbackASTRepr = var_export ($ fallback_ast , true );
6692 $ originalASTRepr = var_export ($ ast , true );
0 commit comments