packages/sql-semantics-postgres/tests/Unit/Rules/Invocation/XmlChecksTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Invocation;
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\Statement\Identifier\Name;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
15use SqlSemantics\Platform\PostgreSql\Platform;
16use SqlSemantics\Platform\PostgreSql\Rules\Invocation\XmlChecks;
17use SqlSemantics\Platform\PostgreSql\Statement\Expression\ColumnReference;
18use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Problem\XmlProblem;
19use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Problem\XmlProblemKind;
20use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Xml\XmlAttribute;
21use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
22use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
23use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
24use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Parameterized;
25
26#[CoversClass(XmlChecks::class)]
27#[Small]
28final class XmlChecksTest extends TestCase
29{
30    public function testAttributesReportsUnnamedValuesAndRepeatedNames(): void
31    {
32        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
33        (new XmlChecks())->attributes($derivation, [new XmlAttribute(new Constant(new IntegerConstant('1'))), new XmlAttribute(new ColumnReference([new Name('a')])), new XmlAttribute(new Constant(new IntegerConstant('1')), new Name('a'))]);
34        self::assertEquals([new XmlProblem(XmlProblemKind::UnnamedAttribute), new XmlProblem(XmlProblemKind::RepeatedAttribute, 'a')], $derivation->facts()->diagnostics);
35    }
36
37    public function testElementsReportsUnnamedValues(): void
38    {
39        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
40        (new XmlChecks())->elements($derivation, [new XmlAttribute(new ColumnReference([new Name('a')])), new XmlAttribute(new Constant(new IntegerConstant('1')))]);
41        self::assertEquals([new XmlProblem(XmlProblemKind::UnnamedElement)], $derivation->facts()->diagnostics);
42    }
43
44    public function testSerializedAcceptsCharacterTypes(): void
45    {
46        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
47        $type = new Known(new Parameterized(Builtin::Varchar, 10));
48        self::assertSame($type, (new XmlChecks())->serialized($derivation, $type));
49        self::assertSame([], $derivation->facts()->diagnostics);
50    }
51}
52