packages/sql-semantics-mysql/tests/Unit/Lowering/TableDefinition/Key/KeyRuleTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\TableDefinition\Key;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Lowering;
11use SqlSemantics\Platform\MySql\Lowering\TableDefinition\Key\KeyRule;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
14use SqlSemantics\Platform\MySql\Platform;
15use SqlSemantics\Platform\MySql\Statement\Expression\Comparison;
16use SqlSemantics\Platform\MySql\Statement\Table\Key\CheckConstraint;
17use SqlSemantics\Platform\MySql\Statement\Table\Key\ForeignKey;
18use SqlSemantics\Platform\MySql\Statement\Table\Key\IndexKind;
19
20#[CoversClass(KeyRule::class)]
21#[Medium]
22final class KeyRuleTest extends TestCase
23{
24 public function testElementLowersAForeignKey(): void
25 {
26 $platform = new Platform();
27 $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
28 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
29 $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT, CONSTRAINT f FOREIGN KEY (a) REFERENCES p (x))')->find('key_def')[0];
30
31 self::assertInstanceOf(ForeignKey::class, (new KeyRule($lowering))->element($node));
32 }
33
34 public function testIndexLowersAnIndexWithoutConstraint(): void
35 {
36 $platform = new Platform();
37 $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
38 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
39 $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT, FULLTEXT KEY f (a))')->find('key_def')[0];
40 $index = (new KeyRule($lowering))->index($lowering->form($node));
41
42 self::assertSame(IndexKind::FullText, $index->kind);
43 self::assertTrue($index->keyword);
44 }
45
46 public function testConstrainedLowersAPrimaryKey(): void
47 {
48 $platform = new Platform();
49 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
50 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
51 $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT, CONSTRAINT c PRIMARY KEY (a))')->find('table_constraint_def')[0];
52 $index = (new KeyRule($lowering))->constrained($lowering->form($node), null, null, null, 4, 6);
53
54 self::assertSame(IndexKind::Primary, $index->kind);
55 }
56
57 public function testConstraintLowersTheConstraintClause(): void
58 {
59 $platform = new Platform();
60 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
61 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
62 $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT, CONSTRAINT c CHECK (a > 0))')->find('opt_constraint_name')[0];
63
64 self::assertSame('c', (new KeyRule($lowering))->constraint($node)?->name?->column->value);
65 }
66
67 public function testCheckLowersTheCondition(): void
68 {
69 $platform = new Platform();
70 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
71 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
72 $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT, CHECK (a > 0))')->find('check_constraint')[0];
73
74 self::assertInstanceOf(Comparison::class, (new KeyRule($lowering))->check($node));
75 }
76
77 public function testOptionalCheckLowersALegacyColumnCheck(): void
78 {
79 $platform = new Platform();
80 $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
81 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
82 $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT CHECK (a > 0))')->find('opt_check_constraint')[0];
83
84 self::assertInstanceOf(CheckConstraint::class, (new KeyRule($lowering))->optionalCheck($node));
85 }
86
87 public function testEnforcedTellsWhetherNotIsWritten(): void
88 {
89 $platform = new Platform();
90 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
91 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
92 $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT, CHECK (a > 0) NOT ENFORCED)')->find('constraint_enforcement')[0];
93
94 self::assertFalse((new KeyRule($lowering))->enforced($node));
95 }
96
97 public function testEnforcementLowersAnAbsentClause(): void
98 {
99 $platform = new Platform();
100 $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
101 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
102 $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT, CHECK (a > 0))')->find('opt_constraint_enforcement')[0];
103
104 self::assertNull((new KeyRule($lowering))->enforcement($node));
105 }
106}
107