packages/deriver/tests/Unit/Deriver/Evaluator/Memory/MutationsTest.php
1<?php
2
3declare (strict_types=1);
4
5namespace Tests\Unit\Deriver\Evaluator\Memory;
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\Enumeration\Choices;
12use Deriver\Deriver\Evaluator\Evaluator;
13use Deriver\Deriver\Evaluator\EvaluatorConfig;
14use Deriver\Deriver\Evaluator\Index\DefinitionIndex;
15use Deriver\Deriver\Evaluator\Request\Budget;
16use Deriver\Deriver\Evaluator\Scalar\Identity;
17use Deriver\Deriver\Evaluator\Scope\Context;
18use Deriver\Deriver\Evaluator\Scope\Frame;
19use Deriver\Deriver\Evaluator\TargetProfile;
20use Deriver\Deriver\Evaluator\Term;
21use Deriver\Deriver\Lowerer\Declaration\Index;
22use Deriver\Deriver\MicroEvaluatorConversion;
23use Deriver\Deriver\Parser\Node\File;
24use Deriver\Deriver\Parser\Parser;
25use Deriver\Deriver\Parser\ParserConfig;
26use Deriver\Deriver\Providers\ProviderExpansion;
27use Deriver\Deriver\Resolver\Resolver;
28use Deriver\Deriver\Sources\Path;
29use Deriver\Deriver\Sources\Source;
30use Deriver\Deriver\Sources\Sources;
31use PHPUnit\Framework\Attributes\CoversNothing;
32use PHPUnit\Framework\TestCase;
33
34#[CoversNothing]
35final class MutationsTest extends TestCase
36{
37 public function testValueDistinguishesPostfixExpressionAndStoredValue(): void
38 {
39 $source = 'function target(){$x=4;$old=$x++;return [$old,$x];}';
40 $configuration = new DeriverConfig();
41 $budget = new Budget();
42 $contributions = new Contributions();
43 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
44 $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
45 $contributions1 = $providers->contributions;
46 $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);
47 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
48 $snapshotId = 'test';
49 $input = $providers->input;
50 $parser = new Parser(new ParserConfig($target->version));
51 $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));
52 $program = new Index($snapshotId, $trees);
53 $engine = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
54 $symbol = 'target';
55 $graph = $engine->context->index->graph($symbol);
56 self::assertNotNull($graph);
57 $alternatives = (new Choices())->alternatives($engine->returns(new Frame($graph, 'source:' . $symbol), 64));
58 $values = array_reduce(iterator_to_array($alternatives, false), static function (array $values, array $alternative): array {
59 [$value, $_] = $alternative;
60 $values[(new Identity())->key($value)] = $value;
61 return $values;
62 }, []);
63 self::assertCount(1, $values);
64 $resolvedValue = array_values($values)[0];
65 self::assertInstanceOf(Term::class, $resolvedValue);
66 self::assertSame([4, 5], $resolvedValue->native());
67 }
68 public function testIncrementSharesPhpStringCarrySemantics(): void
69 {
70 $source = 'function target(){$x="a9";$old=$x++;return [$old,$x];}';
71 $configuration = new DeriverConfig();
72 $budget = new Budget();
73 $contributions = new Contributions();
74 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
75 $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
76 $contributions1 = $providers->contributions;
77 $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);
78 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
79 $snapshotId = 'test';
80 $input = $providers->input;
81 $parser = new Parser(new ParserConfig($target->version));
82 $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));
83 $program = new Index($snapshotId, $trees);
84 $engine = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
85 $symbol = 'target';
86 $graph = $engine->context->index->graph($symbol);
87 self::assertNotNull($graph);
88 $alternatives = (new Choices())->alternatives($engine->returns(new Frame($graph, 'source:' . $symbol), 64));
89 $values = array_reduce(iterator_to_array($alternatives, false), static function (array $values, array $alternative): array {
90 [$value, $_] = $alternative;
91 $values[(new Identity())->key($value)] = $value;
92 return $values;
93 }, []);
94 self::assertCount(1, $values);
95 $resolvedValue = array_values($values)[0];
96 self::assertInstanceOf(Term::class, $resolvedValue);
97 self::assertSame(['a9', 'b0'], $resolvedValue->native());
98 }
99 public function testIncrementKeepsDiagnosticResultsAndTheirSource(): void
100 {
101 $source = 'function target(){$x=true;return ++$x;}';
102 $configuration = new DeriverConfig();
103 $budget = new Budget();
104 $contributions = new Contributions();
105 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
106 $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
107 $contributions1 = $providers->contributions;
108 $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);
109 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
110 $snapshotId = 'test';
111 $input = $providers->input;
112 $parser = new Parser(new ParserConfig($target->version));
113 $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));
114 $program = new Index($snapshotId, $trees);
115 $engine = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
116 $symbol = 'target';
117 $graph = $engine->context->index->graph($symbol);
118 self::assertNotNull($graph);
119 $alternatives = (new Choices())->alternatives($engine->returns(new Frame($graph, 'source:' . $symbol), 64));
120 $values = array_reduce(iterator_to_array($alternatives, false), static function (array $values, array $alternative): array {
121 [$value1, $_] = $alternative;
122 $values[(new Identity())->key($value1)] = $value1;
123 return $values;
124 }, []);
125 self::assertCount(1, $values);
126 $resolvedValue = array_values($values)[0];
127 self::assertInstanceOf(Term::class, $resolvedValue);
128 $value = $resolvedValue;
129 self::assertTrue($value->native());
130 self::assertSame('PHP_WARNING', $value->attributes['reason']);
131 self::assertSame('candidate.php', $value->attributes['source']);
132 }
133 public function testApplyPreservesCompoundAndPostIncrementValues(): void
134 {
135 $source = new Source('subject.php', '<?php function subject() { $value = 2; $value *= 3; $before = $value++; return [$before, $value]; }');
136 $tree = (new Resolver())->resolve((new Parser(new ParserConfig('8.3')))->parse($source));
137 $index = new DefinitionIndex(new Index('test', [$tree]));
138 $profile = new TargetProfile('8.3');
139 $engine = new Evaluator(new Context($index, new EvaluatorConfig($profile), new Evaluators($profile), new Budget()));
140 $graph = $index->graph('subject');
141 self::assertNotNull($graph);
142 $value = $engine->returns(new Frame($graph, 'subject'), 64);
143 $alternatives = iterator_to_array((new Choices())->alternatives($value), false);
144 self::assertCount(1, $alternatives);
145 self::assertSame([6, 7], $alternatives[0][0]->native());
146 }
147}
148