packages/ztd-query-core/tests/Unit/Sql/Profile/SqlCommentProfileTest.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\LexicalDelimiters;
12use ZtdQuery\Sql\Profile\SqlCommentProfile;
13
14#[CoversClass(SqlCommentProfile::class)]
15#[UsesClass(LexicalDelimiters::class)]
16final class SqlCommentProfileTest extends TestCase
17{
18 public function testStartsLineCommentSeesEveryPrefixTheDialectDeclares(): void
19 {
20 $profile = FakeSqlLexerProfiles::comments(lineCommentPrefixes: ['--', '#']);
21
22 self::assertSame(
23 [true, true, false],
24 [
25 $profile->startsLineComment('-- a', 0),
26 $profile->startsLineComment('x # a', 2),
27 $profile->startsLineComment('// a', 0),
28 ],
29 );
30 }
31
32 public function testStartsLineCommentTakesAPrefixThatNeedsWhitespaceOnlyWhereWhitespaceFollows(): void
33 {
34 $profile = FakeSqlLexerProfiles::comments(lineCommentPrefixes: [], whitespaceDelimitedLineCommentPrefixes: ['--']);
35
36 self::assertSame(
37 [true, true, false],
38 [
39 $profile->startsLineComment('-- a', 0),
40 $profile->startsLineComment('--', 0),
41 $profile->startsLineComment('--a', 0),
42 ],
43 );
44 }
45
46 public function testBlockCommentAtAnswersTheDelimitersThatOpenAndCloseOne(): void
47 {
48 $profile = FakeSqlLexerProfiles::comments(blockCommentPairs: ['{-' => '-}']);
49
50 self::assertSame(['{-', '-}'], $profile->blockCommentAt('{- a -}', 0));
51 }
52
53 public function testBlockCommentAtAnswersNothingWhereNoCommentOpens(): void
54 {
55 self::assertNull(FakeSqlLexerProfiles::comments()->blockCommentAt('SELECT', 0));
56 }
57
58 public function testSupportsNestedBlockCommentsSaysWhatTheDialectDeclared(): void
59 {
60 self::assertTrue(FakeSqlLexerProfiles::comments(nestedBlockComments: true)->supportsNestedBlockComments());
61 }
62}
63