* Dariusz RumiƄski * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Tokenizer\Analyzer; use PhpCsFixer\Tests\TestCase; use PhpCsFixer\Tokenizer\Analyzer\AlternativeSyntaxAnalyzer; use PhpCsFixer\Tokenizer\Tokens; /** * @internal * * @covers \PhpCsFixer\Tokenizer\Analyzer\AlternativeSyntaxAnalyzer */ final class AlternativeSyntaxAnalyzerTest extends TestCase { /** * @param list $expectedPositives * * @dataProvider provideBelongsToAlternativeSyntaxCases */ public function testBelongsToAlternativeSyntax(array $expectedPositives, string $source): void { $tokens = Tokens::fromCode($source); for ($index = $tokens->count() - 1; $index >= 0; --$index) { self::assertSame( \in_array($index, $expectedPositives, true), (new AlternativeSyntaxAnalyzer())->belongsToAlternativeSyntax($tokens, $index), '@ index: '.$index ); } } public static function provideBelongsToAlternativeSyntaxCases(): iterable { yield 'declare' => [ [7], ' [ [20], ' [ [17], ' [ [6, 17, 25], ' [ [6], ' [ [5], ' [ [7, 15, 51], 'findAlternativeSyntaxBlockEnd( Tokens::fromCode($code), $startIndex ) ); } /** * @return iterable */ public static function provideItFindsTheEndOfAnAlternativeSyntaxBlockCases(): iterable { yield ['
PHP; yield [$nestedWithHtml, 1, 11]; yield [$nestedWithHtml, 11, 38]; yield [$nestedWithHtml, 17, 27]; yield [$nestedWithHtml, 27, 33]; } /** * @dataProvider provideItThrowsOnInvalidAlternativeSyntaxBlockStartIndexCases */ public function testItThrowsOnInvalidAlternativeSyntaxBlockStartIndex(string $code, int $startIndex, string $expectedMessage): void { $tokens = Tokens::fromCode($code); $analyzer = new AlternativeSyntaxAnalyzer(); $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage($expectedMessage); $analyzer->findAlternativeSyntaxBlockEnd($tokens, $startIndex); } /** * @return iterable */ public static function provideItThrowsOnInvalidAlternativeSyntaxBlockStartIndexCases(): iterable { yield ['