packages/ztd-query-core/tests/Unit/Sql/Reader/SqlBlockCommentReaderTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Sql\Reader;
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\LexicalPattern;
13use ZtdQuery\Sql\Profile\SqlCommentProfile;
14use ZtdQuery\Sql\Profile\SqlParameterProfile;
15use ZtdQuery\Sql\Profile\SqlQuoteProfile;
16use ZtdQuery\Sql\Profile\SqlSymbolProfile;
17use ZtdQuery\Sql\Reader\SqlBlockCommentReader;
18use ZtdQuery\Sql\SqlLexerProfile;
19
20#[CoversClass(SqlBlockCommentReader::class)]
21#[UsesClass(SqlLexerProfile::class)]
22#[UsesClass(LexicalDelimiters::class)]
23#[UsesClass(LexicalPattern::class)]
24#[UsesClass(SqlCommentProfile::class)]
25#[UsesClass(SqlParameterProfile::class)]
26#[UsesClass(SqlQuoteProfile::class)]
27#[UsesClass(SqlSymbolProfile::class)]
28final class SqlBlockCommentReaderTest extends TestCase
29{
30 public function testEndAtAnswersNothingWhereNoCommentBegins(): void
31 {
32 self::assertNull((new SqlBlockCommentReader())->endAt('SELECT 1', 0, FakeSqlLexerProfiles::standard()));
33 }
34
35 public function testEndAtAnswersJustPastTheClosingDelimiter(): void
36 {
37 self::assertSame(9, (new SqlBlockCommentReader())->endAt('/* a b */1', 0, FakeSqlLexerProfiles::standard()));
38 }
39
40 public function testEndAtClosesAtTheFirstDelimiterWhereACommentHoldsNoOther(): void
41 {
42 $profile = FakeSqlLexerProfiles::custom(nestedBlockComments: false);
43
44 self::assertSame(12, (new SqlBlockCommentReader())->endAt('/* a /* b */ */', 0, $profile));
45 }
46
47 public function testEndAtReadsTheWholeOfANestedCommentWhereOneMayHoldAnother(): void
48 {
49 self::assertSame(15, (new SqlBlockCommentReader())->endAt('/* a /* b */ */', 0, FakeSqlLexerProfiles::standard()));
50 }
51
52 public function testEndAtStopsAtTheEndOfAStatementThatNeverClosedTheComment(): void
53 {
54 self::assertSame(4, (new SqlBlockCommentReader())->endAt('/* a', 0, FakeSqlLexerProfiles::standard()));
55 }
56}
57