Просмотр исходного кода

DX: add more tests to CommentsAnalyzer (#7041)

Kuba Werłos 1 год назад
Родитель
Сommit
80d66319d6
1 измененных файлов с 39 добавлено и 16 удалено
  1. 39 16
      tests/Tokenizer/Analyzer/CommentsAnalyzerTest.php

+ 39 - 16
tests/Tokenizer/Analyzer/CommentsAnalyzerTest.php

@@ -264,6 +264,7 @@ $bar;',
             ['<?php /* @var User $bar */ list($bar) = a();'],
             ['<?php /* Before anonymous function */ $fn = fn($x) => $x + 1;'],
             ['<?php /* Before anonymous function */ fn($x) => $x + 1;'],
+            ['<?php /* @var int $x */ [$x] = [2];'],
         ];
     }
 
@@ -291,25 +292,10 @@ $bar;',
 
                 static::bar();',
             ],
+            ['<?php /* @var int $a */ [$b] = [2];'],
         ];
     }
 
-    public function testPhpdocCandidate71(): void
-    {
-        $tokens = Tokens::fromCode('<?php /* @var int $x */ [$x] = [2];');
-        $analyzer = new CommentsAnalyzer();
-
-        self::assertTrue($analyzer->isHeaderComment($tokens, 1));
-    }
-
-    public function testNotPhpdocCandidate71(): void
-    {
-        $tokens = Tokens::fromCode('<?php /* @var int $a */ [$b] = [2];');
-        $analyzer = new CommentsAnalyzer();
-
-        self::assertFalse($analyzer->isBeforeStructuralElement($tokens, 1));
-    }
-
     /**
      * @dataProvider providePhpdocCandidatePhp80Cases
      *
@@ -384,4 +370,41 @@ Class MyAnnotation3 {}'],
             '<?php /* Before enum */ enum Foo {}',
         ];
     }
+
+    /**
+     * @dataProvider provideNotPhpdocCandidatePhp811Cases
+     *
+     * @requires PHP 8.1
+     */
+    public function testNotPhpdocCandidatePhp81(string $code): void
+    {
+        $tokens = Tokens::fromCode($code);
+        $index = $tokens->getNextTokenOfKind(0, [[T_COMMENT], [T_DOC_COMMENT]]);
+        $analyzer = new CommentsAnalyzer();
+
+        self::assertFalse($analyzer->isBeforeStructuralElement($tokens, $index));
+    }
+
+    public static function provideNotPhpdocCandidatePhp811Cases(): iterable
+    {
+        yield 'enum and switch' => [
+            '<?php
+            enum E {}
+            switch ($x) {
+                /* */
+                case 1: return 2;
+            }
+            ',
+        ];
+
+        yield 'switch and enum' => [
+            '<?php
+            switch ($x) {
+                /* */
+                case 1: return 2;
+            }
+            enum E {}
+            ',
+        ];
+    }
 }