packages/sql-semantics-postgres/tests/Unit/Rules/Utility/MaintenanceFactsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Utility;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\UndeclaredTable;
18use SqlSemantics\Platform\PostgreSql\Rules\Utility\MaintenanceFacts;
19use SqlSemantics\Platform\PostgreSql\Semantics;
20use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
21use SqlSemantics\Platform\PostgreSql\Statement\Utility\Maintenance\Vacuum;
22
23#[CoversClass(MaintenanceFacts::class)]
24#[Medium]
25final class MaintenanceFactsTest extends TestCase
26{
27 public function testTargetsReportsAColumnTheDeclaredTableLacks(): void
28 {
29 $table = new Table(new QualifiedName(new Name('t')), new LanguageProfile(GrammarRelease::PostgreSql172), [new Column(new Name('a'), Builtin::Int4)]);
30 self::assertSame(
31 ['column "b" of relation "t" does not exist'],
32 array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), (new Semantics())->inspect('ANALYZE t (a, b)', [$table])->facts->diagnostics),
33 );
34 }
35
36 public function testTargetsClaimsNothingAboutAnUndeclaredTable(): void
37 {
38 $operation = (new Semantics())->inspect('VACUUM ANALYZE t (a), u');
39 self::assertInstanceOf(Vacuum::class, $operation->statement);
40 self::assertSame([], $operation->facts->diagnostics);
41 self::assertInstanceOf(UndeclaredTable::class, $operation->facts->relation($operation->statement->targets[1])->table);
42 }
43
44 public function testHasComparesUnderTheContext(): void
45 {
46 $table = new Table(new QualifiedName(new Name('t')), new LanguageProfile(GrammarRelease::PostgreSql172), [new Column(new Name('A'), Builtin::Int4)]);
47 self::assertSame(
48 ['column "a" of relation "t" does not exist'],
49 array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), (new Semantics())->inspect('ANALYZE t ("A", a)', [$table])->facts->diagnostics),
50 );
51 }
52}
53