packages/deriver/tests/Unit/Deriver/Evaluator/Invocation/BodiesTest.php

1<?php
2
3declare (strict_types=1);
4
5namespace Tests\Unit\Deriver\Evaluator\Invocation;
6
7use Deriver\Deriver\Contributions;
8use Deriver\Deriver\DeriverConfig;
9use Deriver\Deriver\Evaluator\Call\CallDecision;
10use Deriver\Deriver\Evaluator\Call\CallDescription;
11use Deriver\Deriver\Evaluator\Call\CallDescriptor;
12use Deriver\Deriver\Evaluator\Call\CallEvaluator;
13use Deriver\Deriver\Evaluator\Call\Evaluators;
14use Deriver\Deriver\Evaluator\Call\Plan\SemanticPlan;
15use Deriver\Deriver\Evaluator\Call\Registrations;
16use Deriver\Deriver\Evaluator\Evaluator;
17use Deriver\Deriver\Evaluator\EvaluatorConfig;
18use Deriver\Deriver\Evaluator\Index\DefinitionIndex;
19use Deriver\Deriver\Evaluator\Invocation\Bodies;
20use Deriver\Deriver\Evaluator\Request\Budget;
21use Deriver\Deriver\Evaluator\Scope\Context;
22use Deriver\Deriver\Evaluator\Scope\Frame;
23use Deriver\Deriver\Evaluator\TargetProfile;
24use Deriver\Deriver\Evaluator\Term;
25use Deriver\Deriver\Lowerer\Declaration\Index;
26use Deriver\Deriver\MicroEvaluatorConversion;
27use Deriver\Deriver\Parser\Node\File;
28use Deriver\Deriver\Parser\Parser;
29use Deriver\Deriver\Parser\ParserConfig;
30use Deriver\Deriver\Providers\ProviderExpansion;
31use Deriver\Deriver\Resolver\Resolver;
32use Deriver\Deriver\Sources\Path;
33use Deriver\Deriver\Sources\Source;
34use Deriver\Deriver\Sources\Sources;
35use Override;
36use PHPUnit\Framework\Attributes\CoversNothing;
37use PHPUnit\Framework\TestCase;
38
39#[CoversNothing]
40final class BodiesTest extends TestCase
41{
42    public function testSelectSharesOneReplacementAcrossOutputRequests(): void
43    {
44        $source = 'function work(){return missing();}function target(){return work();}';
45        $configuration = new DeriverConfig();
46        $budget = new Budget();
47        $contributions = new Contributions(evaluators: [new class (new CallDescriptor('work', '1', 'work'), new SemanticPlan([])) implements CallEvaluator {
48            /**
49             * @param CallDescriptor $metadata Stable evaluator registration
50             * @param SemanticPlan $plan Ordered API semantics
51             */
52            public function __construct(public readonly CallDescriptor $metadata, public readonly SemanticPlan $plan)
53            {
54            }
55            /**
56             * @return CallDescriptor Stable registration
57             */
58            #[Override]
59            public function descriptor(): CallDescriptor
60            {
61                return $this->metadata;
62            }
63            /**
64             * @param CallDescription $call Selected target
65             * @return CallDecision Declarative fixture semantics
66             */
67            #[Override]
68            public function evaluate(CallDescription $call): CallDecision
69            {
70                return CallDecision::handled($this->plan);
71            }
72        }]);
73        $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
74        $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
75        $contributions1 = $providers->contributions;
76        $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
77        $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
78        $snapshotId = 'test';
79        $input = $providers->input;
80        $parser = new Parser(new ParserConfig($target->version));
81        $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));
82        $program = new Index($snapshotId, $trees);
83        $engine = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
84        $symbol = 'target';
85        $graph = $engine->context->index->graph($symbol);
86        self::assertNotNull($graph);
87        $frame = new Frame($graph, 'source:' . $symbol);
88        $operation = 'invoke';
89        $instruction = array_values(array_filter($frame->graph->definitions, static fn ($instruction): bool => $instruction->operation === $operation))[0] ?? null;
90        $call = $instruction;
91        self::assertNotNull($call);
92        $selector = new Bodies($engine);
93        $first = $selector->select($frame, $call, 'work', 32);
94        self::assertTrue($first->replacement);
95        self::assertSame($first, $selector->select($frame, $call, 'work', 32));
96        self::assertSame(1, $engine->context->evaluatorApplications);
97        self::assertSame([], $engine->context->bodies);
98    }
99}
100