packages/deriver/tests/Unit/Deriver/Evaluator/Execution/Demand/DemandStatusTest.php
1<?php
2
3declare (strict_types=1);
4
5namespace Tests\Unit\Deriver\Evaluator\Execution\Demand;
6
7use Deriver\Deriver\Contributions;
8use Deriver\Deriver\DeriverConfig;
9use Deriver\Deriver\Evaluator\Call\Evaluators;
10use Deriver\Deriver\Evaluator\Call\Registrations;
11use Deriver\Deriver\Evaluator\EvaluatorConfig;
12use Deriver\Deriver\Evaluator\Evidence\Frontier;
13use Deriver\Deriver\Evaluator\Execution\Context;
14use Deriver\Deriver\Evaluator\Execution\Demand\DemandStatus;
15use Deriver\Deriver\Evaluator\Execution\EvaluatorConfig as ExecutionEvaluatorConfig;
16use Deriver\Deriver\Evaluator\Execution\State;
17use Deriver\Deriver\Evaluator\Execution\Summary\Invocation;
18use Deriver\Deriver\Evaluator\Request\Budget;
19use Deriver\Deriver\Evaluator\Request\ReturnQuery;
20use Deriver\Deriver\Evaluator\TargetProfile;
21use Deriver\Deriver\Evaluator\Term;
22use Deriver\Deriver\Execution\QueryExecution;
23use Deriver\Deriver\Lowerer\Declaration\Index;
24use Deriver\Deriver\MicroEvaluatorConversion;
25use Deriver\Deriver\Parser\Node\File;
26use Deriver\Deriver\Parser\Parser;
27use Deriver\Deriver\Parser\ParserConfig;
28use Deriver\Deriver\Providers\ProviderExpansion;
29use Deriver\Deriver\Resolver\Resolver;
30use Deriver\Deriver\SnapshotIdentity;
31use Deriver\Deriver\Sources\Path;
32use Deriver\Deriver\Sources\Source;
33use Deriver\Deriver\Sources\Sources;
34use PHPUnit\Framework\Attributes\CoversNothing;
35use PHPUnit\Framework\TestCase;
36
37#[CoversNothing]
38final class DemandStatusTest extends TestCase
39{
40 public function testAStableDependentBecomesPendingWhenItsInputChanges(): void
41 {
42 $source = '<?php function target(){return 1;}';
43 $budget = new Budget();
44 $configuration = new DeriverConfig();
45 $contributions = new Contributions();
46 $input = new Sources([new Source('fixture.php', $source)]);
47 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
48 $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
49 $contributions1 = $providers->contributions;
50 $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);
51 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
52 $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
53 $snapshotId = $identity->id;
54 $input1 = $providers->input;
55 $parser = new Parser(new ParserConfig($target->version));
56 $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($input1, false));
57 $program1 = new Index($snapshotId, $trees);
58 $execution = new QueryExecution($program1, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program1->diagnostics())));
59 $snapshotId1 = 'test';
60 $input2 = new Sources([new Source('fixture.php', $source)]);
61 $profile = $execution->configuration->target;
62 $parser1 = new Parser(new ParserConfig($profile->version));
63 $trees1 = array_map(static fn (Source $source2): File => (new Resolver())->resolve($parser1->parse(new Source(Path::normalize($source2->path), $source2->contents, $source2->declarationsOnly))), iterator_to_array($input2, false));
64 $program = new Index($snapshotId1, $trees1);
65 $context = new Context($program, new ReturnQuery('target', budget: $budget), $execution->configuration, $execution->evaluators);
66 $symbol = 'target';
67 $body = $context->program->callable($symbol);
68 self::assertNotNull($body);
69 $state = new State();
70 $invocation = new Invocation();
71 $key = $invocation->key($body, $state, []);
72 $parent = $context->summaries->register($key, $body, $state);
73 $context->summaries->stack[] = $key->id();
74 $child = $context->summaries->register($invocation->key($body, $state, ['call']), $body, $state);
75 $parent->status = DemandStatus::Stable;
76 $context->summaries->invalidate($child);
77 self::assertSame(DemandStatus::Pending, $parent->status);
78 self::assertArrayHasKey($key->id(), $child->dependents);
79 }
80}
81