packages/sql-semantics/tests/Unit/Core/Schema/InvariantTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Core\Schema;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Core\Binder;
11use SqlSemantics\Core\SchemaBuilder;
12use SqlSemantics\Core\SemanticException;
13use SqlSemantics\Core\Type\Nullability;
14use SqlSemantics\Facade\Schema;
15use SqlSemantics\Platform\PostgreSql\Dialect as PostgreSqlDialect;
16use SqlSemantics\Platform\Sqlite\Dialect as SqliteDialect;
17use SqlSemantics\Statement\Writer;
18
19#[CoversClass(Binder::class)]
20#[CoversClass(SchemaBuilder::class)]
21#[CoversClass(\SqlSemantics\Core\Ast\DialectParser::class)]
22#[CoversClass(\SqlSemantics\Core\Binding\ExpressionBinder::class)]
23#[CoversClass(\SqlSemantics\Core\Binding\ExpressionRules::class)]
24#[CoversClass(\SqlSemantics\Core\Binding\FromBinder::class)]
25#[CoversClass(\SqlSemantics\Core\Binding\LiteralBinder::class)]
26#[CoversClass(\SqlSemantics\Core\Binding\NullFacts::class)]
27#[CoversClass(\SqlSemantics\Core\Binding\ProjectionBinder::class)]
28#[CoversClass(\SqlSemantics\Core\Binding\SelectBinder::class)]
29#[CoversClass(\SqlSemantics\Core\Binding\SyntaxGuard::class)]
30#[CoversClass(\SqlSemantics\Core\Binding\SelectModifiersBinder::class)]
31#[CoversClass(\SqlSemantics\Core\Binding\TypeResolution::class)]
32#[CoversClass(\SqlSemantics\Core\Ast\ColumnReader::class)]
33#[CoversClass(\SqlSemantics\Core\Ast\ConstraintReader::class)]
34#[CoversClass(\SqlSemantics\Core\Ast\Identifiers::class)]
35#[CoversClass(\SqlSemantics\Core\Ast\SchemaReader::class)]
36#[CoversClass(\SqlSemantics\Core\Ast\StatementList::class)]
37#[CoversClass(\SqlSemantics\Core\Ast\TokenGroups::class)]
38#[CoversClass(\SqlSemantics\Core\Ast\Tree::class)]
39#[CoversClass(\SqlSemantics\Core\Ast\TypeReader::class)]
40#[CoversClass(\SqlSemantics\Core\Binding\BoundRelation::class)]
41#[CoversClass(\SqlSemantics\Core\Binding\IdentitySequence::class)]
42#[CoversClass(\SqlSemantics\Core\Binding\Scope::class)]
43#[CoversClass(\SqlSemantics\Core\Binding\TableResolver::class)]
44#[CoversClass(\SqlSemantics\Core\Model\ColumnBinding::class)]
45#[CoversClass(\SqlSemantics\Core\Model\Expression::class)]
46#[CoversClass(\SqlSemantics\Core\Model\Join::class)]
47#[CoversClass(\SqlSemantics\Core\Model\Ordering::class)]
48#[CoversClass(\SqlSemantics\Core\Model\OutputColumn::class)]
49#[CoversClass(\SqlSemantics\Core\Model\BoundSelect::class)]
50#[CoversClass(\SqlSemantics\Core\Model\TableUse::class)]
51#[CoversClass(\SqlSemantics\Core\Schema::class)]
52#[CoversClass(\SqlSemantics\Core\Schema\ColumnDefinition::class)]
53#[CoversClass(\SqlSemantics\Core\Schema\TableConstraint::class)]
54#[CoversClass(\SqlSemantics\Core\Schema\TableDefinition::class)]
55#[CoversClass(SemanticException::class)]
56#[CoversClass(\SqlSemantics\Core\Type\TypeDescriptor::class)]
57#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Core\Policy\SyntaxRules::class)]
58#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\QueryRules::class)]
59#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\Platform::class)]
60#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\TypeRules::class)]
61#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\NameRules::class)]
62#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\SchemaRules::class)]
63#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\QueryRules::class)]
64#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\Platform::class)]
65#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\TypeRules::class)]
66#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\NameRules::class)]
67#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\SchemaRules::class)]
68#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\QueryRules::class)]
69#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\Platform::class)]
70#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\TypeRules::class)]
71#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\NameRules::class)]
72#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\SchemaRules::class)]
73#[Medium]
74#[CoversClass(\SqlSemantics\Core\Analysis\SchemaAnalyzer::class)]
75#[CoversClass(\SqlSemantics\Core\Analysis\ValueReader::class)]
76#[CoversClass(\SqlSemantics\Core\Ast\ColumnProperties::class)]
77#[CoversClass(\SqlSemantics\Core\Ast\SchemaChanges::class)]
78#[CoversClass(\SqlSemantics\Core\Schema\ColumnGeneration::class)]
79#[CoversClass(\SqlSemantics\Core\Schema\Invariant::class)]
80#[CoversClass(Schema::class)]
81#[CoversClass(\SqlSemantics\Statement\ImmutableGraph::class)]
82#[CoversClass(Writer::class)]
83#[CoversClass(\SqlSemantics\Statement\Assertion::class)]
84final class InvariantTest extends TestCase
85{
86    public function testMembersPreservesDeclarationOrder(): void
87    {
88        $table = (new Schema(PostgreSqlDialect::PostgreSql))->analyze('CREATE TABLE t (z INT, a TEXT)')->tables[0];
89        self::assertSame(['z', 'a'], array_column($table->columns, 'name'));
90    }
91
92    public function testNamesPreservesCompositeKeyOrder(): void
93    {
94        $table = (new Schema(PostgreSqlDialect::PostgreSql))->analyze('CREATE TABLE t (z INT, a INT, PRIMARY KEY (a, z))')->tables[0];
95        self::assertSame(['a', 'z'], $table->constraints[0]->columns);
96    }
97
98    public function testElementsPreservesImmutableSemanticGraphs(): void
99    {
100        $table = (new Schema(PostgreSqlDialect::PostgreSql))->analyze('CREATE TABLE t (a INT DEFAULT 1)')->tables[0];
101        self::assertStringNotContainsString('SqlParser', serialize($table));
102        self::assertTrue((new \SqlSemantics\Statement\ImmutableGraph())->containsOnlyImmutableValues($table->source));
103    }
104
105    public function testEnsureAcceptsACompleteState(): void
106    {
107        $state = (new Schema(SqliteDialect::Sqlite))->analyze('CREATE TABLE t (id INTEGER PRIMARY KEY)');
108        self::assertSame(Nullability::NotNull, $state->tables[0]->columns[0]->nullability);
109    }
110    public function testTablePreservesAValidCompositeForeignKey(): void
111    {
112        $state = (new Schema(PostgreSqlDialect::PostgreSql))->analyze('CREATE TABLE t (a INT, b INT, FOREIGN KEY (b, a) REFERENCES parent (y, x))');
113        self::assertSame(['b', 'a'], $state->tables[0]->constraints[0]->columns);
114        self::assertSame(['y', 'x'], $state->tables[0]->constraints[0]->referencedColumns);
115    }
116}
117