packages/sql-semantics-postgres/tests/Unit/Rules/Expression/ColumnNamingTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Expression;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\PostgreSql\Rules\Expression\ColumnNaming;
12use SqlSemantics\Platform\PostgreSql\Statement\Expression\Cast;
13use SqlSemantics\Platform\PostgreSql\Statement\Expression\CastSpelling;
14use SqlSemantics\Platform\PostgreSql\Statement\Expression\ColumnReference;
15use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\CaseBranch;
16use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\CaseExpression;
17use SqlSemantics\Platform\PostgreSql\Statement\Literal\BooleanLiteral;
18use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
19use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\KeywordDesignation;
20use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\TypeKeyword;
21use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
22
23#[CoversClass(ColumnNaming::class)]
24#[Small]
25final class ColumnNamingTest extends TestCase
26{
27 public function testNameOfACastOfACaseIsTheType(): void
28 {
29 $case = new CaseExpression(null, [new CaseBranch(new BooleanLiteral(true), new NullLiteral())]);
30 self::assertSame('int4', (new ColumnNaming())->name(new Cast($case, new TypeName(new KeywordDesignation(TypeKeyword::Integer)), CastSpelling::Function))?->value);
31 }
32
33 public function testFigureAnswersTheStrength(): void
34 {
35 self::assertEquals([[new Name('a'), 2], [null, 0]], [(new ColumnNaming())->figure(new ColumnReference([new Name('a')])), (new ColumnNaming())->figure(new NullLiteral())]);
36 }
37}
38