packages/sql-semantics-mysql/tests/Unit/Rules/Query/TailFactsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Query;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
11use SqlSemantics\Platform\MySql\Model\Resolution\VisibleRelation;
12use SqlSemantics\Platform\MySql\Model\Statement\Fact\QueryFact;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
15use SqlSemantics\Platform\MySql\Model\Statement\Shape\RowShape;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Dependent;
17use SqlSemantics\Platform\MySql\Rules\Query\TailFacts;
18use SqlSemantics\Platform\MySql\Semantics;
19use SqlSemantics\Platform\MySql\Statement\Query\Clause\RowLimit;
20use SqlSemantics\Platform\MySql\Statement\Query\Locking\LockingClause;
21use SqlSemantics\Platform\MySql\Statement\Query\Locking\LockStrength;
22use SqlSemantics\Platform\MySql\Statement\Query\Problem\Misuse;
23use SqlSemantics\Platform\MySql\Statement\Query\Problem\MisuseRule;
24use SqlSemantics\Platform\MySql\Statement\Query\Select;
25use SqlSemantics\Platform\MySql\Statement\Relation\Dual;
26
27#[CoversClass(TailFacts::class)]
28#[Medium]
29final class TailFactsTest extends TestCase
30{
31    public function testDeriveReportsAnUnknownLockedTable(): void
32    {
33        $semantics = new Semantics();
34        $derivation = new Derivation($semantics->context());
35        $fact = new QueryFact([], $semantics->context()->columnNames);
36        (new TailFacts())->derive($derivation, $derivation->environment(), $fact, null, [new LockingClause(LockStrength::Update, [new QualifiedName(new Name('t'))])], [new VisibleRelation(new Dual(), new RowShape([]), new Name('u'))]);
37
38        self::assertCount(1, $derivation->facts()->diagnostics);
39        self::assertInstanceOf(Misuse::class, $derivation->facts()->diagnostics[0]);
40        self::assertSame(MisuseRule::UnknownLockedTable, $derivation->facts()->diagnostics[0]->rule);
41    }
42
43    public function testLimitDerivesTheOperands(): void
44    {
45        $operation = (new Semantics())->analyze('SELECT a FROM t LIMIT ?, ?');
46
47        self::assertInstanceOf(Select::class, $operation->statement);
48        self::assertInstanceOf(RowLimit::class, $operation->statement->limit);
49        self::assertInstanceOf(Dependent::class, $operation->facts->scalar($operation->statement->limit->count)->type);
50    }
51}
52