packages/sql-semantics/tests/Unit/Core/Ast/ConstraintReaderTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Core\Ast;
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\Platform\PostgreSql\Dialect as PostgreSqlDialect;
14
15#[CoversClass(\SqlSemantics\Core\Ast\ConstraintReader::class)]
16#[CoversClass(\SqlSemantics\Core\Binding\ExpressionBinder::class)]
17#[CoversClass(\SqlSemantics\Core\Binding\ExpressionRules::class)]
18#[CoversClass(\SqlSemantics\Core\Binding\FromBinder::class)]
19#[CoversClass(\SqlSemantics\Core\Binding\LiteralBinder::class)]
20#[CoversClass(\SqlSemantics\Core\Binding\NullFacts::class)]
21#[CoversClass(\SqlSemantics\Core\Binding\ProjectionBinder::class)]
22#[CoversClass(\SqlSemantics\Core\Binding\SelectBinder::class)]
23#[CoversClass(\SqlSemantics\Core\Binding\SyntaxGuard::class)]
24#[CoversClass(\SqlSemantics\Core\Binding\SelectModifiersBinder::class)]
25#[CoversClass(\SqlSemantics\Core\Binding\TypeResolution::class)]
26#[CoversClass(Binder::class)]
27#[CoversClass(SchemaBuilder::class)]
28#[CoversClass(\SqlSemantics\Core\Ast\DialectParser::class)]
29#[CoversClass(\SqlSemantics\Core\Ast\ColumnReader::class)]
30#[CoversClass(\SqlSemantics\Core\Ast\Identifiers::class)]
31#[CoversClass(\SqlSemantics\Core\Ast\SchemaReader::class)]
32#[CoversClass(\SqlSemantics\Core\Ast\StatementList::class)]
33#[CoversClass(\SqlSemantics\Core\Ast\TokenGroups::class)]
34#[CoversClass(\SqlSemantics\Core\Ast\Tree::class)]
35#[CoversClass(\SqlSemantics\Core\Ast\TypeReader::class)]
36#[CoversClass(\SqlSemantics\Core\Binding\BoundRelation::class)]
37#[CoversClass(\SqlSemantics\Core\Binding\IdentitySequence::class)]
38#[CoversClass(\SqlSemantics\Core\Binding\Scope::class)]
39#[CoversClass(\SqlSemantics\Core\Binding\TableResolver::class)]
40#[CoversClass(\SqlSemantics\Core\Model\ColumnBinding::class)]
41#[CoversClass(\SqlSemantics\Core\Model\Expression::class)]
42#[CoversClass(\SqlSemantics\Core\Model\Join::class)]
43#[CoversClass(\SqlSemantics\Core\Model\Ordering::class)]
44#[CoversClass(\SqlSemantics\Core\Model\OutputColumn::class)]
45#[CoversClass(\SqlSemantics\Core\Model\BoundSelect::class)]
46#[CoversClass(\SqlSemantics\Core\Model\TableUse::class)]
47#[CoversClass(\SqlSemantics\Core\Schema::class)]
48#[CoversClass(\SqlSemantics\Core\Schema\ColumnDefinition::class)]
49#[CoversClass(\SqlSemantics\Core\Schema\TableConstraint::class)]
50#[CoversClass(\SqlSemantics\Core\Schema\TableDefinition::class)]
51#[CoversClass(SemanticException::class)]
52#[CoversClass(\SqlSemantics\Core\Type\TypeDescriptor::class)]
53#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Core\Policy\SyntaxRules::class)]
54#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\QueryRules::class)]
55#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\Platform::class)]
56#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\TypeRules::class)]
57#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\NameRules::class)]
58#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\SchemaRules::class)]
59#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\QueryRules::class)]
60#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\Platform::class)]
61#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\TypeRules::class)]
62#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\NameRules::class)]
63#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\SchemaRules::class)]
64#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\QueryRules::class)]
65#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\Platform::class)]
66#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\TypeRules::class)]
67#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\NameRules::class)]
68#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\SchemaRules::class)]
69#[Medium]
70final class ConstraintReaderTest extends TestCase
71{
72    public function testReadNamedForeignKey(): void
73    {
74        $table = (new SchemaBuilder(PostgreSqlDialect::PostgreSql))->build('CREATE TABLE users (id INTEGER, CONSTRAINT self_ref FOREIGN KEY (id) REFERENCES users(id) ON DELETE CASCADE DEFERRABLE)')->tables[0];
75        self::assertSame('self_ref', $table->constraints[0]->name);
76        self::assertSame(['id'], $table->constraints[0]->columns);
77        self::assertStringContainsString('DEFERRABLE', \SqlSemantics\Statement\Writer::render($table->constraints[0]->source));
78    }
79
80    public function testReferencesPreservesCompositeForeignKeyOrder(): void
81    {
82        $table = (new SchemaBuilder(PostgreSqlDialect::PostgreSql))->build('CREATE TABLE users (id INTEGER, parent_id INTEGER, FOREIGN KEY (id, parent_id) REFERENCES other.users (parent_id, id))')->tables[0];
83        self::assertSame(['other', 'users'], $table->constraints[0]->referencedTable);
84        self::assertSame(['parent_id', 'id'], $table->constraints[0]->referencedColumns);
85    }
86}
87