packages/sql-semantics-sqlite/tests/Unit/Statement/Schema/Refusal/WrongRelationKindTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Schema\Refusal;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Declaration\RelationKind;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Declaration\Table;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
14use SqlSemantics\Platform\Sqlite\Semantics;
15use SqlSemantics\Platform\Sqlite\Statement\Schema\Refusal\KindRefusal;
16use SqlSemantics\Platform\Sqlite\Statement\Schema\Refusal\WrongRelationKind;
17
18#[CoversClass(WrongRelationKind::class)]
19#[Medium]
20final class WrongRelationKindTest extends TestCase
21{
22    public function testMessageNamesTheRelationItsKindAndTheRequest(): void
23    {
24        $semantics = new Semantics();
25        $view = $semantics->analyze('CREATE VIEW v AS SELECT 1')->declarations()[0];
26        $table = $semantics->analyze('CREATE TABLE t (a)')->declarations()[0];
27        $profile = $semantics->context()->profile;
28
29        self::assertSame('Relation v is a view: CREATE INDEX indexes only a table.', (new WrongRelationKind($view, KindRefusal::CreateIndex))->message());
30        self::assertSame('Relation t is a table: An INSTEAD OF trigger watches only a view.', (new WrongRelationKind($table, KindRefusal::InsteadOfTrigger))->message());
31        self::assertSame('Relation m is a materialized view: An upsert writes only to a table.', (new WrongRelationKind(new Table(new QualifiedName(new Name('m'), new Name('main')), $profile, [], [], true, RelationKind::MaterializedView), KindRefusal::Upsert))->message());
32    }
33
34    public function testMessageIsNeverAboutARelationOfTheKindTheRequestNeeds(): void
35    {
36        $table = (new Semantics())->analyze('CREATE TABLE t (a)')->declarations()[0];
37        $this->expectExceptionMessage('A relation of the kind a request needs is not refused.');
38
39        new WrongRelationKind($table, KindRefusal::DropTable);
40    }
41}
42