packages/sql-semantics-postgres/tests/Unit/Statement/Operator/AlterOperatorTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Operator;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\PostgreSql\Semantics;
12use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
13use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
14use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Attribute;
15use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\AttributeChange;
16use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Known\OperatorAttribute;
17use SqlSemantics\Platform\PostgreSql\Statement\Operator\AlterOperator;
18use SqlSemantics\Platform\PostgreSql\Statement\Routine\Problem\RoutineProblem;
19use SqlSemantics\Platform\PostgreSql\Statement\Routine\Problem\RoutineProblemKind;
20use SqlSemantics\Platform\PostgreSql\Statement\Routine\Signature\OperatorArity;
21use SqlSemantics\Platform\PostgreSql\Statement\Routine\Signature\OperatorSignature;
22use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
23use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
24
25#[CoversClass(AlterOperator::class)]
26#[Medium]
27final class AlterOperatorTest extends TestCase
28{
29 public function testDeriveStatementReportsAPostfixOperator(): void
30 {
31 $operation = (new Semantics())->inspect('ALTER OPERATOR ! (int4, NONE) SET (restrict = NONE)');
32 self::assertEquals([new RoutineProblem(RoutineProblemKind::PostfixOperator)], $operation->facts->diagnostics);
33 }
34
35 public function testRenderWritesTheAttributes(): void
36 {
37 $operation = (new Semantics())->inspect('ALTER OPERATOR = (int4, int4) SET (restrict = NONE, join = eqjoinsel, hashes)');
38 self::assertSame('ALTER OPERATOR = (int4, int4) SET (restrict = NONE, join = eqjoinsel, hashes)', $operation->toString());
39 }
40
41 public function testRejectsNoAttribute(): void
42 {
43 $this->expectExceptionMessage('ALTER OPERATOR ... SET changes at least one attribute.');
44 new AlterOperator(new OperatorSignature(new OperatorName(new Name('=')), OperatorArity::Binary, [new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))), new TypeName(new NamedDesignation(new DottedName([new Name('int4')])))]), []);
45 }
46
47 public function testRejectsAnAttributeOfAnotherCommand(): void
48 {
49 $this->expectExceptionMessage('ALTER OPERATOR reads the attributes it can change.');
50 new AlterOperator(new OperatorSignature(new OperatorName(new Name('=')), OperatorArity::Binary, [new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))), new TypeName(new NamedDesignation(new DottedName([new Name('int4')])))]), [new AttributeChange(new Attribute(new Name('sort1'), OperatorAttribute::Sort1))]);
51 }
52
53 public function testDeriveStatementReportsTheAttributesTheCommandRefuses(): void
54 {
55 $operation = (new Semantics('pg-16.6'))->inspect('ALTER OPERATOR = (int4, int4) SET (commutator = =, leftarg = int4, sort1 = <)');
56 self::assertSame([
57 'operator attribute "commutator" cannot be changed',
58 'operator attribute "leftarg" cannot be changed',
59 'operator attribute "sort1" not recognized',
60 ], array_map(static fn ($problem): string => $problem->message(), $operation->facts->diagnostics));
61 }
62}
63