packages/sql-semantics-postgres/tests/Unit/Rules/Manipulation/SourcesTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Manipulation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Manipulation\Sources::class)]
12#[Medium]
13final class SourcesTest extends TestCase
14{
15 public function testStatementAcceptsTheQueriesOfSelectStmt(): void
16 {
17 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics('pg-17.2');
18 $query = $semantics->analyze('SELECT 1 UNION SELECT 2');
19 $statement = $query->statement;
20 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\SetOperation::class, $statement);
21 self::assertTrue((new \SqlSemantics\Platform\PostgreSql\Rules\Manipulation\Sources())->statement($statement));
22 }
23
24 public function testStatementRefusesAModification(): void
25 {
26 $profile = new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
27 $t = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')), $profile, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('b'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Text)]);
28 $u = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('u')), $profile, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int8, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('c'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Bool, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull)]);
29 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics('pg-17.2');
30 $query = $semantics->analyze('DELETE FROM t', [$t, $u]);
31 $statement = $query->statement;
32 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Delete::class, $statement);
33 self::assertFalse((new \SqlSemantics\Platform\PostgreSql\Rules\Manipulation\Sources())->statement($statement));
34 }
35
36 public function testInsertableRefusesValuesInParentheses(): void
37 {
38 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics('pg-17.2');
39 $query = $semantics->analyze('VALUES (1) ORDER BY 1');
40 $statement = $query->statement;
41 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\QueryExpression::class, $statement);
42 $other = $semantics->analyze('((VALUES (1)))')->statement;
43 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\ParenthesizedQuery::class, $other);
44 self::assertSame([true, false], [(new \SqlSemantics\Platform\PostgreSql\Rules\Manipulation\Sources())->insertable($statement), (new \SqlSemantics\Platform\PostgreSql\Rules\Manipulation\Sources())->insertable($other)]);
45 }
46}
47