packages/deriver/tests/Unit/Deriver/Evaluator/Invocation/Path/CallersTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Deriver\Evaluator\Invocation\Path;
6
7use Deriver\Deriver\Evaluator\Call\Evaluators;
8use Deriver\Deriver\Evaluator\Evaluator;
9use Deriver\Deriver\Evaluator\EvaluatorConfig;
10use Deriver\Deriver\Evaluator\Index\DefinitionIndex;
11use Deriver\Deriver\Evaluator\Invocation\Path\Callers;
12use Deriver\Deriver\Evaluator\Request\Budget;
13use Deriver\Deriver\Evaluator\Scope\Context;
14use Deriver\Deriver\Lowerer\Declaration\Index;
15use Deriver\Deriver\Parser\Parser;
16use Deriver\Deriver\Parser\ParserConfig;
17use Deriver\Deriver\Resolver\Resolver;
18use Deriver\Deriver\Sources\Source;
19use PHPUnit\Framework\Attributes\CoversNothing;
20use PHPUnit\Framework\TestCase;
21
22#[CoversNothing]
23final class CallersTest extends TestCase
24{
25    public function testSitesFiltersBeforeLoweringUnrelatedCallers(): void
26    {
27        $source = 'function foo($x){return $x;}function bar(){foo(7);}function other(){foo(9);}';
28        $tree = (new Resolver())->resolve((new Parser(new ParserConfig('8.3')))->parse(new Source('fixture.php', '<?php ' . $source)));
29        $index = new DefinitionIndex(new Index('fixture', [$tree]));
30        $settings = new EvaluatorConfig();
31        $engine = new Evaluator(new Context($index, $settings, new Evaluators($settings->target), new Budget()));
32        $callers = new Callers($index);
33        $sites = $callers->sites('foo', 'bar');
34        self::assertCount(1, $sites);
35        self::assertSame('bar', $sites[0][0]->body->symbol);
36        self::assertSame(1, $index->program->graphCount());
37        self::assertSame($sites, $callers->sites('foo', 'bar'));
38    }
39
40    public function testWithinKeepsDynamicTargetsForDispatch(): void
41    {
42        $source = 'function foo($x){return $x;}function bar(){$f="foo";$f(7);other(9);}';
43        $tree = (new Resolver())->resolve((new Parser(new ParserConfig('8.3')))->parse(new Source('fixture.php', '<?php ' . $source)));
44        $index = new DefinitionIndex(new Index('fixture', [$tree]));
45        $settings = new EvaluatorConfig();
46        $engine = new Evaluator(new Context($index, $settings, new Evaluators($settings->target), new Budget()));
47        $graph = $index->graph('bar');
48        self::assertNotNull($graph);
49        self::assertCount(1, (new Callers($index))->within($graph, 'foo'));
50    }
51
52}
53