packages/sql-semantics-mysql/tests/Unit/Lowering/TableDefinition/Column/AttributeRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\TableDefinition\Column;
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\Column\AttributeRule;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
14use SqlSemantics\Platform\MySql\Platform;
15use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
16use SqlSemantics\Platform\MySql\Statement\Table\Column\DefaultLiteral;
17use SqlSemantics\Platform\MySql\Statement\Table\Column\KeywordAttribute;
18use SqlSemantics\Platform\MySql\Statement\Table\Column\Kind\ColumnKeyword;
19use SqlSemantics\Platform\MySql\Statement\Table\Column\StorageAttribute;
20
21#[CoversClass(AttributeRule::class)]
22#[Medium]
23final class AttributeRuleTest extends TestCase
24{
25    public function testAttributeLowersAKeywordAttribute(): void
26    {
27        $platform = new Platform();
28        $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
29        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
30        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT AUTO_INCREMENT)')->find('attribute')[0];
31        $attribute = (new AttributeRule($lowering))->attribute($node);
32
33        self::assertInstanceOf(KeywordAttribute::class, $attribute);
34        self::assertSame(ColumnKeyword::AutoIncrement, $attribute->keyword);
35    }
36
37    public function testValuedLowersAnAttributeWithAValue(): void
38    {
39        $platform = new Platform();
40        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
41        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
42        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT DEFAULT 3)')->find('column_attribute')[0];
43
44        self::assertInstanceOf(DefaultLiteral::class, (new AttributeRule($lowering))->valued($lowering->form($node)));
45    }
46
47    public function testMarksConfirmsTheKeywordRules(): void
48    {
49        $platform = new Platform();
50        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
51        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
52        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT NOT NULL PRIMARY KEY)')->find('column_attribute')[1];
53        (new AttributeRule($lowering))->marks($lowering->form($node));
54        (new AttributeRule($lowering))->marks($lowering->form($platform->parser($profile)->parse('CREATE TABLE t (a INT NOT NULL)')->find('column_attribute')[0]));
55
56        self::assertInstanceOf(StorageAttribute::class, (new AttributeRule($lowering))->attribute($platform->parser($profile)->parse('CREATE TABLE t (a INT STORAGE DISK)')->find('column_attribute')[0]));
57    }
58
59    public function testValueLowersALiteralDefault(): void
60    {
61        $platform = new Platform();
62        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
63        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
64        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT DEFAULT 3)')->find('now_or_signed_literal')[0];
65
66        self::assertInstanceOf(NumberLiteral::class, (new AttributeRule($lowering))->value($node));
67    }
68
69    public function testCollationLowersACollationName(): void
70    {
71        $platform = new Platform();
72        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
73        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
74        $node = $platform->parser($profile)->parse('CREATE TABLE t (a TEXT COLLATE utf8mb4_bin)')->find('collation_name')[0];
75
76        self::assertSame('utf8mb4_bin', (new AttributeRule($lowering))->collation($node)->name?->value);
77    }
78}
79