packages/sql-semantics-sqlite/tests/Unit/Model/Resolution/EnvironmentTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Resolution;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Contract\AnalysisContext;
11use SqlSemantics\Platform\Sqlite\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\Sqlite\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\Sqlite\Model\Resolution\CommonBinding;
14use SqlSemantics\Platform\Sqlite\Model\Resolution\Environment;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Comparison;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
17use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\Field;
18use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\OutputSlot;
19use SqlSemantics\Platform\Sqlite\Model\Statement\Shape\RowShape;
20use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
21use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
22use SqlSemantics\Platform\Sqlite\Statement\Query\Star;
23use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
24
25#[CoversClass(Environment::class)]
26#[Small]
27final class EnvironmentTest extends TestCase
28{
29    public function testCommonTableFindsTheNearestBindingWithLaterOnesShadowingEarlierOnes(): void
30    {
31        $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')], [], true, Comparison::AsciiInsensitive);
32        $outerBinding = new CommonBinding(new Name('c'), new Star(), new RowShape([]));
33        $earlier = new CommonBinding(new Name('c'), new Star(), new RowShape([]));
34        $later = new CommonBinding(new Name('C'), new Star(), new RowShape([]));
35        $outer = new Environment($context, null, [], [$outerBinding]);
36        $inner = new Environment($context, $outer, [], [$earlier, $later]);
37
38        self::assertSame($later, $inner->commonTable(new Name('c')));
39        self::assertSame($outerBinding, $outer->commonTable(new Name('c')));
40        self::assertSame($outerBinding, (new Environment($context, $outer))->commonTable(new Name('c')));
41        self::assertNull($inner->commonTable(new Name('d')));
42    }
43
44    public function testCommonTableComparesNamesAsTheContextSays(): void
45    {
46        $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')]);
47        $binding = new CommonBinding(new Name('c'), new Star(), new RowShape([]));
48        $environment = new Environment($context, null, [], [$binding]);
49
50        self::assertSame($binding, $environment->commonTable(new Name('c')));
51        self::assertNull($environment->commonTable(new Name('C')));
52    }
53
54    public function testAliasedFindsEveryOutputFieldWithTheAlias(): void
55    {
56        $context = new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), [new Name('main')], [], true, Comparison::Sensitive, Comparison::AsciiInsensitive);
57        $first = new Field(0, new OutputSlot(new Name('x'), new Known(Storage::Integer), Nullability::NotNull));
58        $second = new Field(1, new OutputSlot(new Name('X'), new Known(Storage::Text), Nullability::Nullable));
59        $unnamed = new Field(2, new OutputSlot(null, new Known(Storage::Text), Nullability::Nullable));
60        $environment = new Environment($context, null, [], [], [$first, $second, $unnamed]);
61
62        self::assertSame([$first, $second], $environment->aliased(new Name('x')));
63        self::assertSame([], $environment->aliased(new Name('y')));
64    }
65}
66