packages/sql-semantics-sqlite/tests/Unit/Rules/Definition/KeyTermsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Definition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
12use SqlSemantics\Platform\Sqlite\Rules\Definition\KeyTerms;
13use SqlSemantics\Platform\Sqlite\Statement\Expression\Collate;
14use SqlSemantics\Platform\Sqlite\Statement\Expression\ColumnUse;
15use SqlSemantics\Platform\Sqlite\Statement\Expression\DoubleQuotedWord;
16use SqlSemantics\Platform\Sqlite\Statement\Expression\Grouped;
17use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
18use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\TextLiteral;
19use SqlSemantics\Platform\Sqlite\Statement\Schema\LiteralColumn;
20
21#[CoversClass(KeyTerms::class)]
22#[Small]
23final class KeyTermsTest extends TestCase
24{
25 public function testCoreLooksThroughParenthesesAndCollations(): void
26 {
27 $column = new ColumnUse(new Name('a'));
28
29 self::assertSame($column, (new KeyTerms())->core(new Grouped(new Collate($column, new Name('nocase')))));
30 self::assertSame($column, (new KeyTerms())->core($column));
31 }
32
33 public function testColumnReadsAPlainNameAStringAndADoubleQuotedWord(): void
34 {
35 $terms = new KeyTerms();
36
37 self::assertSame('a', $terms->column(new ColumnUse(new Name('a')))?->value);
38 self::assertSame('a', $terms->column(new Grouped(new ColumnUse(new Name('a'))))?->value);
39 self::assertSame('b', $terms->column(new LiteralColumn(new TextLiteral('b')))?->value);
40 self::assertNull($terms->column(new TextLiteral('b')));
41 self::assertSame('c', $terms->column(new DoubleQuotedWord(new Name('c')))?->value);
42 }
43
44 public function testColumnIsNullForAQualifiedReferenceAndForAnExpression(): void
45 {
46 $terms = new KeyTerms();
47
48 self::assertNull($terms->column(new ColumnUse(new Name('a'), new QualifiedName(new Name('t')))));
49 self::assertNull($terms->column(new IntegerLiteral('1')));
50 }
51
52 public function testReferenceAcceptsEveryColumnReferenceAndNoOtherExpression(): void
53 {
54 $terms = new KeyTerms();
55
56 self::assertTrue($terms->reference(new ColumnUse(new Name('a'), new QualifiedName(new Name('t')))));
57 self::assertTrue($terms->reference(new Collate(new ColumnUse(new Name('a')), new Name('nocase'))));
58 self::assertTrue($terms->reference(new LiteralColumn(new TextLiteral('a'))));
59 self::assertFalse($terms->reference(new TextLiteral('a')));
60 self::assertFalse($terms->reference(new IntegerLiteral('1')));
61 }
62
63 public function testBeneathStopsAtTheCollationLimit(): void
64 {
65 $terms = new KeyTerms();
66 $literal = new TextLiteral('a');
67 $once = new Collate(new Grouped($literal), new Name('nocase'));
68 $twice = new Collate($once, new Name('binary'));
69
70 self::assertSame($literal, $terms->beneath(new Grouped($twice), null));
71 self::assertSame($literal, $terms->beneath(new Grouped($once), 1));
72 self::assertSame($once, $terms->beneath(new Grouped($twice), 1));
73 self::assertSame($once, $terms->beneath($once, 0));
74 }
75
76 public function testStringTellsALiteralUnderTheAdmittedWrappers(): void
77 {
78 $terms = new KeyTerms();
79 $once = new Collate(new Grouped(new TextLiteral('a')), new Name('nocase'));
80 $twice = new Collate($once, new Name('binary'));
81
82 self::assertTrue($terms->string($twice, null));
83 self::assertTrue($terms->string($once, 1));
84 self::assertFalse($terms->string($twice, 1));
85 self::assertFalse($terms->string(new Grouped(new ColumnUse(new Name('a'))), null));
86 }
87}
88