packages/sql-faker/tests/Unit/Compiler/Bison/Directive/StartDirectiveReaderTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\SqlFaker\Compiler\Bison\Directive;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\UsesClass;
9use PHPUnit\Framework\TestCase;
10use SqlFaker\Compiler\Bison\Ast\BisonStartDeclaration;
11use SqlFaker\Compiler\Bison\Directive\StartDirectiveReader;
12use SqlFaker\Compiler\Bison\Lexer\ActionScanner;
13use SqlFaker\Compiler\Bison\Lexer\BisonLexeme;
14use SqlFaker\Compiler\Bison\Lexer\BisonLexer;
15use SqlFaker\Compiler\Bison\Lexer\BisonScannerChain;
16use SqlFaker\Compiler\Bison\Lexer\BisonToken;
17use SqlFaker\Compiler\Bison\Lexer\BisonTokenStream;
18use SqlFaker\Compiler\Bison\Lexer\BisonTrivia;
19use SqlFaker\Compiler\Bison\Lexer\DirectiveScanner;
20use SqlFaker\Compiler\Bison\Lexer\IdentifierScanner;
21use SqlFaker\Compiler\Bison\Lexer\NumberScanner;
22use SqlFaker\Compiler\Bison\Lexer\QuotedLiteralScanner;
23use SqlFaker\Compiler\Bison\Lexer\SourceCursor;
24use SqlFaker\Compiler\Bison\Lexer\TypeTagScanner;
25
26#[CoversClass(StartDirectiveReader::class)]
27#[UsesClass(BisonLexeme::class)]
28#[UsesClass(BisonLexer::class)]
29#[UsesClass(BisonScannerChain::class)]
30#[UsesClass(BisonStartDeclaration::class)]
31#[UsesClass(BisonToken::class)]
32#[UsesClass(BisonTokenStream::class)]
33#[UsesClass(BisonTrivia::class)]
34#[UsesClass(IdentifierScanner::class)]
35#[UsesClass(NumberScanner::class)]
36#[UsesClass(SourceCursor::class)]
37#[UsesClass(ActionScanner::class)]
38#[UsesClass(DirectiveScanner::class)]
39#[UsesClass(QuotedLiteralScanner::class)]
40#[UsesClass(TypeTagScanner::class)]
41#[UsesClass(\SqlFaker\Compiler\Bison\Lexer\PunctuationScanner::class)]
42#[UsesClass(\SqlFaker\Compiler\GrammarParseException::class)]
43final class StartDirectiveReaderTest extends TestCase
44{
45    public function testHandlesClaimsOnlyTheStartDirective(): void
46    {
47        $reader = new StartDirectiveReader();
48
49        self::assertTrue($reader->handles('%start'));
50        self::assertFalse($reader->handles('%token'));
51    }
52
53    public function testReadTakesTheRuleName(): void
54    {
55        $declaration = (new StartDirectiveReader())->read(BisonTokenStream::over('statement'), '%start');
56
57        self::assertInstanceOf(BisonStartDeclaration::class, $declaration);
58        self::assertSame('statement', $declaration->symbol);
59    }
60
61    public function testReadDeclaresNothingWhenNoRuleNameFollows(): void
62    {
63        $stream = BisonTokenStream::over('42');
64
65        self::assertNull((new StartDirectiveReader())->read($stream, '%start'));
66    }
67
68    public function testReadLeavesATokenItDidNotClaim(): void
69    {
70        $stream = BisonTokenStream::over('42');
71
72        (new StartDirectiveReader())->read($stream, '%start');
73
74        self::assertSame(42, $stream->next()->value);
75    }
76}
77