packages/deriver/tests/Unit/Deriver/Execution/StableInputsTest.php
1<?php
2
3declare (strict_types=1);
4
5namespace Tests\Unit\Deriver\Execution;
6
7use Deriver\Deriver\Contributions;
8use Deriver\Deriver\DeriverConfig;
9use Deriver\Deriver\Evaluator\Call\BoundInstruction;
10use Deriver\Deriver\Evaluator\Call\Evaluators;
11use Deriver\Deriver\Evaluator\Call\Registrations;
12use Deriver\Deriver\Evaluator\EvaluatorConfig;
13use Deriver\Deriver\Evaluator\Evidence\Frontier;
14use Deriver\Deriver\Evaluator\Execution\Context;
15use Deriver\Deriver\Evaluator\Execution\EvaluatorConfig as ExecutionEvaluatorConfig;
16use Deriver\Deriver\Evaluator\Request\Budget;
17use Deriver\Deriver\Evaluator\Request\ReturnQuery;
18use Deriver\Deriver\Evaluator\TargetProfile;
19use Deriver\Deriver\Evaluator\Term;
20use Deriver\Deriver\Execution\QueryExecution;
21use Deriver\Deriver\Execution\StableInputs;
22use Deriver\Deriver\Lowerer\Declaration\Index;
23use Deriver\Deriver\MicroEvaluatorConversion;
24use Deriver\Deriver\Parser\Node\File;
25use Deriver\Deriver\Parser\Parser;
26use Deriver\Deriver\Parser\ParserConfig;
27use Deriver\Deriver\Providers\ProviderExpansion;
28use Deriver\Deriver\Resolver\Resolver;
29use Deriver\Deriver\SnapshotIdentity;
30use Deriver\Deriver\Sources\Path;
31use Deriver\Deriver\Sources\Source;
32use Deriver\Deriver\Sources\SourceRef;
33use Deriver\Deriver\Sources\Sources;
34use PHPUnit\Framework\Attributes\CoversNothing;
35use PHPUnit\Framework\Attributes\DataProvider;
36use PHPUnit\Framework\Attributes\Small;
37use PHPUnit\Framework\TestCase;
38
39#[CoversNothing]
40#[Small]
41final class StableInputsTest extends TestCase
42{
43 public function testRecoverDoesNotTreatScriptGlobalsAsImmutableParameters(): void
44 {
45 $source = '<?php function target(){return 1;}';
46 $budget = new Budget();
47 $configuration = new DeriverConfig();
48 $contributions = new Contributions();
49 $input = new Sources([new Source('fixture.php', $source)]);
50 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
51 $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
52 $contributions1 = $providers->contributions;
53 $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);
54 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
55 $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
56 $snapshotId = $identity->id;
57 $input1 = $providers->input;
58 $parser = new Parser(new ParserConfig($target->version));
59 $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));
60 $program1 = new Index($snapshotId, $trees);
61 $execution = new QueryExecution($program1, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program1->diagnostics())));
62 $snapshotId1 = 'test';
63 $input2 = new Sources([new Source('fixture.php', $source)]);
64 $profile = $execution->configuration->target;
65 $parser1 = new Parser(new ParserConfig($profile->version));
66 $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));
67 $program = new Index($snapshotId1, $trees1);
68 $body = (new Context($program, new ReturnQuery('target', budget: $budget), $execution->configuration, $execution->evaluators))->program->callable('script:fixture.php');
69 self::assertNotNull($body);
70 self::assertSame([], (new StableInputs())->recover($body, [], 'BUDGET_EXCEEDED'));
71 }
72 public function testRecoverRejectsCatchBindingsEvenWithoutExplicitWrites(): void
73 {
74 $source = '<?php function target(PDO $pdo){try{}catch(Throwable $pdo){}}';
75 $budget = new Budget();
76 $configuration = new DeriverConfig();
77 $contributions = new Contributions();
78 $input = new Sources([new Source('fixture.php', $source)]);
79 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
80 $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
81 $contributions1 = $providers->contributions;
82 $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);
83 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
84 $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
85 $snapshotId = $identity->id;
86 $input1 = $providers->input;
87 $parser = new Parser(new ParserConfig($target->version));
88 $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));
89 $program1 = new Index($snapshotId, $trees);
90 $execution = new QueryExecution($program1, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program1->diagnostics())));
91 $snapshotId1 = 'test';
92 $input2 = new Sources([new Source('fixture.php', $source)]);
93 $profile = $execution->configuration->target;
94 $parser1 = new Parser(new ParserConfig($profile->version));
95 $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));
96 $program = new Index($snapshotId1, $trees1);
97 $body = (new Context($program, new ReturnQuery('target', budget: $budget), $execution->configuration, $execution->evaluators))->program->callable('target');
98 self::assertNotNull($body);
99 self::assertSame([], (new StableInputs())->recover($body, [], 'BUDGET_EXCEEDED'));
100 }
101 #[DataProvider('providerExtractNames')]
102 public function testSymbolEffectRecognizesDynamicAndQualifiedExtractCalls(string $name): void
103 {
104 $at = new SourceRef('test', 'a.php', 0, 1);
105 $call = new BoundInstruction('call', 'invoke', $at, 'result', ['name']);
106 $proof = new StableInputs();
107 self::assertTrue($proof->symbolEffect($call, []));
108 self::assertTrue($proof->symbolEffect($call, ['name' => new BoundInstruction('name', 'constant', $at, 'name', constant: Term::constant($name))]));
109 self::assertFalse($proof->symbolEffect($call, ['name' => new BoundInstruction('name', 'constant', $at, 'name', constant: Term::constant('heavy'))]));
110 }
111 /**
112 * @return list<array{string}>
113 */
114 public static function providerExtractNames(): array
115 {
116 return [['extract'], ['\extract'], ['App\extract'], ['EXTRACT']];
117 }
118 public function testTypeIncludesImplicitlyNullableDefaults(): void
119 {
120 $source = '<?php function target(PDO $pdo=null){}';
121 $budget = new Budget();
122 $configuration = new DeriverConfig();
123 $contributions = new Contributions();
124 $input = new Sources([new Source('fixture.php', $source)]);
125 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
126 $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
127 $contributions1 = $providers->contributions;
128 $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);
129 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
130 $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
131 $snapshotId = $identity->id;
132 $input1 = $providers->input;
133 $parser = new Parser(new ParserConfig($target->version));
134 $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));
135 $program1 = new Index($snapshotId, $trees);
136 $execution = new QueryExecution($program1, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program1->diagnostics())));
137 $snapshotId1 = 'test';
138 $input2 = new Sources([new Source('fixture.php', $source)]);
139 $profile = $execution->configuration->target;
140 $parser1 = new Parser(new ParserConfig($profile->version));
141 $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));
142 $program = new Index($snapshotId1, $trees1);
143 $body = (new Context($program, new ReturnQuery('target', budget: $budget), $execution->configuration, $execution->evaluators))->program->callable('target');
144 self::assertNotNull($body);
145 self::assertSame('PDO|null', (new StableInputs())->type($body->parameters[0]));
146 }
147}
148