packages/ztd-query-postgres/tests/Unit/Schema/Reflection/Key/ForeignKeysTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Schema\Reflection\Key;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9
10#[CoversClass(\ZtdQuery\Platform\Postgres\Schema\Reflection\Key\ForeignKeys::class)]
11#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Reflection\Key\ForeignKeyRow::class)]
12final class ForeignKeysTest extends TestCase
13{
14 public function testDefinitionsGroupsCompositeKeysInCatalogOrder(): void
15 {
16 $first = ['constraint_name' => 'fk_user', 'column_name' => 'user_id', 'foreign_table_name' => 'users', 'foreign_column_name' => 'id', 'update_rule' => 'CASCADE', 'delete_rule' => 'RESTRICT'];
17 $second = array_replace($first, ['column_name' => 'tenant', 'foreign_column_name' => 'tenant_id']);
18 $statement = self::createStub(\ZtdQuery\Connection\StatementInterface::class);
19 $statement->method('fetchAll')->willReturn([$first, ['constraint_name' => 'invalid'], $second]);
20 self::assertSame(['CONSTRAINT "fk_user" FOREIGN KEY ("user_id", "tenant") REFERENCES "users" ("id", "tenant_id") ON UPDATE CASCADE ON DELETE RESTRICT'], (new \ZtdQuery\Platform\Postgres\Schema\Reflection\Key\ForeignKeys())->definitions($statement));
21 self::assertSame([], (new \ZtdQuery\Platform\Postgres\Schema\Reflection\Key\ForeignKeys())->definitions(false));
22 }
23 public function testRenderPreservesReferentialActions(): void
24 {
25 self::assertSame('CONSTRAINT "fk" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON UPDATE NO ACTION ON DELETE CASCADE', (new \ZtdQuery\Platform\Postgres\Schema\Reflection\Key\ForeignKeys())->render('fk', ['columns' => ['"user_id"'], 'table' => 'users', 'referencedColumns' => ['"id"'], 'onUpdate' => 'NO ACTION', 'onDelete' => 'CASCADE']));
26 }
27}
28