packages/deriver/tests/Unit/Deriver/Evaluator/Evidence/ForestTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Deriver\Evaluator\Evidence;
6
7use Deriver\Deriver\Evaluator\Enumeration\Choices;
8use Deriver\Deriver\Evaluator\Evidence\Forest as Subject;
9use Deriver\Deriver\Evaluator\Evidence\Provenance;
10use Deriver\Deriver\Evaluator\Term;
11use PHPUnit\Framework\Attributes\CoversNothing;
12use PHPUnit\Framework\TestCase;
13use WeakReference;
14
15#[CoversNothing]
16final class ForestTest extends TestCase
17{
18    public function testRootRetainsTheConditionsOfUnresolvedAlternatives(): void
19    {
20        $value = (new Choices())->make([
21            [Term::constant(1), ['flag' => true]],
22            [Term::constant(2), ['flag' => false]],
23        ]);
24
25        $root = (new Subject(new Provenance()))->root($value);
26
27        self::assertSame(true, $root->inputs['operand:0']->attributes['guard:flag']);
28        self::assertSame(false, $root->inputs['operand:1']->attributes['guard:flag']);
29    }
30
31    public function testRootSharesRepeatedOperands(): void
32    {
33        $value = Term::constant(3);
34        $root = (new Subject(new Provenance()))->root(Term::array([$value,$value]));
35        self::assertSame($root->inputs['operand:0'], $root->inputs['operand:1']);
36    }
37
38    public function testIndependentTraversalsReuseSharedSubgraphsWithinOneAnalysis(): void
39    {
40        $proofs = new Provenance();
41        $value = Term::array([Term::constant(3)]);
42        $first = (new Subject($proofs))->root($value);
43        $second = (new Subject($proofs))->root(Term::array([$value, $value]));
44        self::assertSame($first, $second->inputs['operand:0']);
45        self::assertSame($first, $second->inputs['operand:1']);
46        self::assertNotSame($first, (new Subject(new Provenance()))->root($value));
47    }
48
49    public function testCachingDoesNotReplaceAttachedProofsOrRetainValues(): void
50    {
51        $proofs = new Provenance();
52        $value = Term::constant(3);
53        $plain = (new Subject($proofs))->root($value);
54        $attached = new \Deriver\Deriver\Evaluator\Evidence\Node(\Deriver\Deriver\Evaluator\Evidence\EvidenceKind::SourceDefinition, attributes: ['definition' => 'other']);
55        $derived = $proofs->attach($value, $attached);
56        self::assertSame($attached, (new Subject($proofs))->root($derived));
57        self::assertSame($plain, (new Subject($proofs))->root($value));
58        $reference = WeakReference::create($value);
59        unset($value);
60        self::assertNull($reference->get());
61    }
62
63}
64