packages/ztd-query-core/tests/Unit/Sql/Profile/SqlSymbolProfileTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Sql\Profile;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\UsesClass;
9use PHPUnit\Framework\TestCase;
10use Tests\Fake\FakeSqlLexerProfiles;
11use ZtdQuery\Sql\LexicalPattern;
12use ZtdQuery\Sql\Profile\SqlSymbolProfile;
13
14#[CoversClass(SqlSymbolProfile::class)]
15#[UsesClass(LexicalPattern::class)]
16final class SqlSymbolProfileTest extends TestCase
17{
18    public function testNumberLengthAtMeasuresAsFarAsTheDialectSpellsANumber(): void
19    {
20        $profile = FakeSqlLexerProfiles::symbols(numericLiteralPattern: '/^[0-9]+/');
21
22        self::assertSame([3, 0], [$profile->numberLengthAt('123x', 0), $profile->numberLengthAt('x', 0)]);
23    }
24
25    public function testIsIdentifierStartAnswersWhatMayOpenAName(): void
26    {
27        $profile = FakeSqlLexerProfiles::symbols();
28
29        self::assertSame([true, false], [$profile->isIdentifierStart('a'), $profile->isIdentifierStart('1')]);
30    }
31
32    public function testIsIdentifierPartAnswersWhatMayCarryAName(): void
33    {
34        $profile = FakeSqlLexerProfiles::symbols();
35
36        self::assertSame([true, false], [$profile->isIdentifierPart('1'), $profile->isIdentifierPart('-')]);
37    }
38
39    public function testIsBracketOpeningAnswersFalseWhereTheDialectBracketsNothing(): void
40    {
41        self::assertFalse(FakeSqlLexerProfiles::symbols()->isBracketOpening('['));
42    }
43
44    public function testIsBracketClosingAnswersWhatTheDialectClosesABracketWith(): void
45    {
46        $profile = FakeSqlLexerProfiles::symbols(bracketPair: ['[', ']']);
47
48        self::assertSame([true, false], [$profile->isBracketClosing(']'), $profile->isBracketClosing('[')]);
49    }
50
51    public function testIsNestingOpeningAnswersWhatTheDialectNestsWith(): void
52    {
53        self::assertTrue(FakeSqlLexerProfiles::symbols()->isNestingOpening('('));
54    }
55
56    public function testIsNestingClosingAnswersWhatTheDialectClosesANestingWith(): void
57    {
58        self::assertTrue(FakeSqlLexerProfiles::symbols()->isNestingClosing(')'));
59    }
60
61    public function testIsStatementDelimiterAnswersWhatEndsAStatement(): void
62    {
63        self::assertTrue(FakeSqlLexerProfiles::symbols()->isStatementDelimiter(';'));
64    }
65
66    public function testListDelimiterAnswersWhatSeparatesListItems(): void
67    {
68        self::assertSame(',', FakeSqlLexerProfiles::symbols()->listDelimiter());
69    }
70}
71