packages/sql-semantics-postgres/tests/Unit/Rules/Routine/ChangeChecksTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Routine;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\PostgreSql\Rules\Routine\ChangeChecks;
13use SqlSemantics\Platform\PostgreSql\Semantics;
14use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Attribute;
15use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Known\OperatorChangeAttribute;
16use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Known\TypeChangeAttribute;
17
18#[CoversClass(ChangeChecks::class)]
19#[Medium]
20final class ChangeChecksTest extends TestCase
21{
22 public function testDeriveReportsEachRefusedAttribute(): void
23 {
24 $operation = (new Semantics('pg-17.2'))->inspect('ALTER OPERATOR = (int4, int4) SET (restrict, negator = NONE, commutator = 1, hashes, sort1 = <)');
25 self::assertSame([
26 'negator requires a parameter',
27 'argument of commutator must be a name',
28 'operator attribute "sort1" not recognized',
29 ], array_map(static fn ($problem): string => $problem->message(), $operation->facts->diagnostics));
30 }
31
32 public function testProblemAcceptsARemovedFunction(): void
33 {
34 self::assertNull((new ChangeChecks())->problem(new Attribute(new Name('receive'), TypeChangeAttribute::Receive), true, GrammarRelease::PostgreSql172));
35 }
36
37 public function testChangeableRefusesTheAttributesAddedInPostgreSql17Before(): void
38 {
39 $checks = new ChangeChecks();
40 self::assertSame([false, true, false], [
41 $checks->changeable(OperatorChangeAttribute::Negator, GrammarRelease::PostgreSql166),
42 $checks->changeable(OperatorChangeAttribute::Negator, GrammarRelease::PostgreSql172),
43 $checks->changeable(TypeChangeAttribute::Element, GrammarRelease::PostgreSql172),
44 ]);
45 }
46}
47