packages/sql-semantics/tests/Unit/Core/Analysis/SourceCommentsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Core\Analysis;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlParser\Lexer\Token;
11use SqlParser\Parser\Node;
12use SqlSemantics\Core\Analysis\SourceComments;
13
14#[CoversClass(SourceComments::class)]
15#[Small]
16final class SourceCommentsTest extends TestCase
17{
18    public function testBeforeAnswersTheCommentsOfTheTokenTheyPrecede(): void
19    {
20        $select = new Token(1, 'SELECT', 'SELECT', 0);
21        $one = new Token(2, 'NUM', '1', 15, ' /* value */ ');
22        $comments = new SourceComments(['-- lead'], [spl_object_id($one) => ['/* value */']], ['-- end']);
23        self::assertSame(['/* value */'], $comments->before($one));
24        self::assertSame([], $comments->before($select));
25        self::assertSame(['-- lead'], $comments->leading);
26        self::assertSame(['-- end'], $comments->trailing);
27    }
28
29    public function testFirstFindsTheFirstTokenBelowANodeAndNullForAnEmptyNode(): void
30    {
31        $empty = new Node('opt_clause', 0, []);
32        $one = new Token(2, 'NUM', '1', 7);
33        $tree = new Node('root', 0, [$empty, new Node('expr', 0, [new Node('term', 0, [$one])]), new Token(3, ';', ';', 8)]);
34        $comments = new SourceComments([], [], []);
35        self::assertNull($comments->first($empty));
36        self::assertSame($one, $comments->first($tree));
37    }
38}
39