packages/sql-semantics-postgres/tests/Unit/Statement/Object/Attribute/AttributeTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Object\Attribute;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
14use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
18use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
19use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
20use SqlSemantics\Platform\PostgreSql\Statement\Literal\SignedNumber;
21use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
22use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
23use SqlSemantics\Platform\PostgreSql\Statement\Name\ObjectKind;
24use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Attribute;
25use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\BooleanArgument;
26use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Known\OperatorAttribute;
27use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\NameArgument;
28use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\TextArgument;
29use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
30use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
31
32#[CoversClass(Attribute::class)]
33#[Small]
34final class AttributeTest extends TestCase
35{
36 public function testValuedTellsWhetherAValueIsWritten(): void
37 {
38 self::assertSame([false, true, false], [(new Attribute(new Name('hashes'), OperatorAttribute::Hashes, new BooleanArgument()))->valued(), (new Attribute(new Name('hashes'), OperatorAttribute::Hashes, new BooleanArgument(new StringConstant('on'))))->valued(), (new Attribute(new Name('colour'), null))->valued()]);
39 }
40
41 public function testDeriveClauseDerivesTheValue(): void
42 {
43 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
44 $modifier = new Constant(new IntegerConstant('1'));
45 (new Attribute(new Name('function'), OperatorAttribute::Function, new NameArgument(ObjectKind::Function, new TypeName(new NamedDesignation(new DottedName([new Name('f')]), [$modifier])))))->deriveClause($derivation, $derivation->environment());
46 self::assertTrue($derivation->facts()->covers($modifier));
47 }
48
49 public function testRenderWritesTheNameAsALabelAndTheValue(): void
50 {
51 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
52 (new Attribute(new Name('function'), OperatorAttribute::Function, new NameArgument(ObjectKind::Function, new DottedName([new Name('int4eq')]))))->render($out);
53 self::assertSame('function = int4eq', (new Lexical())->join($out->pieces()));
54 }
55
56 public function testRejectsAMemberOfAnotherName(): void
57 {
58 $this->expectExceptionMessage('A recognized attribute is the member its name names.');
59 new Attribute(new Name('FUNCTION'), OperatorAttribute::Function, new NameArgument(ObjectKind::Function, new DottedName([new Name('f')])));
60 }
61
62 public function testRejectsAReadValueOfAnUnrecognizedAttribute(): void
63 {
64 $this->expectExceptionMessage('A read value belongs to a recognized attribute that reads it that way.');
65 new Attribute(new Name('colour'), null, new TextArgument(new StringConstant('red')));
66 }
67
68 public function testRejectsAValueReadAnotherWay(): void
69 {
70 $this->expectExceptionMessage('A read value belongs to a recognized attribute that reads it that way.');
71 new Attribute(new Name('leftarg'), OperatorAttribute::Leftarg, new NameArgument(ObjectKind::Function, new DottedName([new Name('int4')])));
72 }
73
74 public function testRejectsAWrittenValueTheCommandReads(): void
75 {
76 $this->expectExceptionMessage('A value the command reads is kept as read.');
77 new Attribute(new Name('function'), OperatorAttribute::Function, new StringConstant('int4eq'));
78 }
79
80 public function testAcceptsAWrittenValueTheCommandRejects(): void
81 {
82 $number = new SignedNumber(false, new IntegerConstant('1'));
83 self::assertSame($number, (new Attribute(new Name('function'), OperatorAttribute::Function, $number))->value);
84 }
85}
86