packages/deriver/tests/Unit/Deriver/Lowerer/Declaration/CommentsTest.php

1<?php
2
3declare (strict_types=1);
4
5namespace Tests\Unit\Deriver\Lowerer\Declaration;
6
7use Deriver\Deriver\Evaluator\TargetProfile;
8use Deriver\Deriver\Lowerer\Declaration\Comments;
9use Deriver\Deriver\Lowerer\Declaration\Index;
10use Deriver\Deriver\Parser\Node\File;
11use Deriver\Deriver\Parser\Parser;
12use Deriver\Deriver\Parser\ParserConfig;
13use Deriver\Deriver\Resolver\Resolver;
14use Deriver\Deriver\Sources\Path;
15use Deriver\Deriver\Sources\Source;
16use Deriver\Deriver\Sources\Sources;
17use PHPUnit\Framework\Attributes\CoversNothing;
18use PHPUnit\Framework\Attributes\Small;
19use PHPUnit\Framework\TestCase;
20
21#[CoversNothing]
22#[Small]
23final class CommentsTest extends TestCase
24{
25    public function testWithinKeepsNestedStatementLocationsWithoutCompilingBodies(): void
26    {
27        $source = '<?php function f(){if(true){/** @var int $n */ $n=1;}}';
28        $snapshotId = 'test';
29        $input = new Sources([new Source('fixture.php', $source)]);
30        $profile = new TargetProfile();
31        $parser = new Parser(new ParserConfig($profile->version));
32        $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(Path::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input, false));
33        $index = new Index($snapshotId, $trees);
34        $comments = (new Comments())->within($index, 'f');
35        self::assertCount(1, $comments);
36        self::assertSame('/** @var int $n */', $comments[0]->text);
37        self::assertSame(0, $index->graphCount());
38        self::assertSame([], (new Comments())->within($index, 'missing'));
39    }
40}
41