packages/sql-semantics-sqlite/tests/Unit/Model/Lowering/ProductionsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Lowering;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlParser\Parser\Node;
11use SqlSemantics\Platform\Sqlite\Model\Lowering\Productions;
12use SqlSemantics\Platform\Sqlite\Platform;
13use SqlSemantics\Platform\Sqlite\Semantics;
14
15#[CoversClass(Productions::class)]
16#[Medium]
17final class ProductionsTest extends TestCase
18{
19 public function testLoadReadsAReleaseOnce(): void
20 {
21 $path = dirname(__DIR__, 4) . '/resources/productions/sqlite-3.47.2.php';
22
23 $productions = Productions::load($path);
24
25 self::assertSame($productions, Productions::load($path));
26 self::assertContains('term: INTEGER', $productions->all());
27 }
28
29 public function testLoadRefusesAMissingList(): void
30 {
31 $this->expectExceptionMessage('The production list of the grammar release is missing: /nowhere/sqlite-0.0.0.php');
32
33 Productions::load('/nowhere/sqlite-0.0.0.php');
34 }
35
36 public function testFormWrapsANodeWithItsSignature(): void
37 {
38 $productions = new Productions(['where_opt' => ['where_opt:', 'where_opt: WHERE expr']]);
39 $node = new Node('where_opt', 1, []);
40
41 $form = $productions->form($node);
42
43 self::assertSame($node, $form->node);
44 self::assertSame('where_opt: WHERE expr', $form->signature);
45 }
46
47 public function testSignatureSpellsTheProductionOfAParsedNode(): void
48 {
49 $platform = Platform::shared();
50 $profile = (new Semantics())->profile();
51 $tree = $platform->parser($profile)->parse('SELECT 1');
52
53 self::assertSame('input: cmdlist', $platform->productions($profile)->signature($tree));
54 self::assertSame('cmdlist: ecmd', $platform->productions($profile)->signature($tree->find('cmdlist')[0]));
55 }
56
57 public function testSignatureRefusesANodeTheReleaseDoesNotHave(): void
58 {
59 $productions = new Productions(['where_opt' => ['where_opt:']]);
60
61 $this->expectExceptionMessage('The grammar release has no production where_opt#1.');
62
63 $productions->signature(new Node('where_opt', 1, []));
64 }
65
66 public function testAllListsEverySignatureOfEveryRule(): void
67 {
68 $productions = new Productions(['a' => ['a:', 'a: a B'], 'b' => ['b: C']]);
69
70 self::assertSame(['a:', 'a: a B', 'b: C'], $productions->all());
71 }
72}
73