packages/sql-semantics-sqlite/tests/Unit/Model/Diagnostic/CheckTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Diagnostic;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlParser\Parser\Node;
11use SqlSemantics\Platform\Sqlite\Model\Diagnostic\Check;
12use SqlSemantics\Platform\Sqlite\Model\Lowering\Form;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
15
16#[CoversClass(Check::class)]
17#[Small]
18final class CheckTest extends TestCase
19{
20 public function testInputKeepsAHoldingCondition(): void
21 {
22 $name = new QualifiedName(new Name('t'), new Name('s'), new Name('c'));
23
24 self::assertSame('c', $name->catalog?->value);
25 }
26
27 public function testInputRejectsAFailingConditionWithTheMessageOfTheConstructor(): void
28 {
29 $this->expectExceptionMessage('A catalog qualifier requires a schema qualifier.');
30
31 new QualifiedName(new Name('t'), null, new Name('c'));
32 }
33
34 public function testListOfNarrowsAListOfTheClass(): void
35 {
36 $names = [new Name('a'), new Name('b')];
37
38 $list = Check::listOf($names, Name::class, 'Names only.');
39
40 self::assertSame($names, $list);
41 }
42
43 public function testListOfRejectsAnItemOfAnotherClass(): void
44 {
45 $this->expectExceptionMessage('Names only.');
46
47 Check::listOf([new Name('a'), new QualifiedName(new Name('b'))], Name::class, 'Names only.');
48 }
49
50 public function testListOfRejectsAMapInsteadOfAList(): void
51 {
52 $this->expectExceptionMessage('Names only.');
53
54 Check::listOf(['first' => new Name('a')], Name::class, 'Names only.');
55 }
56
57 public function testListOfRejectsAListShorterThanTheMinimum(): void
58 {
59 $this->expectExceptionMessage('At least two names.');
60
61 Check::listOf([new Name('a')], Name::class, 'At least two names.', 2);
62 }
63
64 public function testListOfAcceptsAnEmptyListWithoutMinimum(): void
65 {
66 self::assertSame([], Check::listOf([], Name::class, 'Names only.'));
67 }
68
69 public function testInvariantRejectsAFailingConditionWithTheMessageOfTheCheck(): void
70 {
71 $this->expectExceptionMessage('The production has no nonterminal at position 0: where_opt:');
72
73 (new Form(new Node('where_opt', 0, []), 'where_opt:'))->node(0);
74 }
75
76 public function testInvariantKeepsAHoldingCondition(): void
77 {
78 $child = new Node('expr', 0, []);
79
80 self::assertSame($child, (new Form(new Node('where_opt', 1, [$child]), 'where_opt: expr'))->node(0));
81 }
82}
83