packages/sql-fixture/tests/Unit/Platform/Sqlite/Schema/PragmaColumnTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Platform\Sqlite\Schema;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9use SqlFixture\Platform\Sqlite\Schema\PragmaColumn as Subject;
10
11#[CoversClass(Subject::class)]
12#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Platform\Sqlite\Schema\PragmaSchema::class)]
13#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\ColumnDefinition::class)]
14#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\TableSchema::class)]
15final class PragmaColumnTest extends TestCase
16{
17    public function testParseReadsDimensionsAndNullability(): void
18    {
19        $column = (new Subject())->parse(['cid' => 1, 'name' => 'amount', 'type' => 'DECIMAL(8,2)', 'notnull' => 1, 'dflt_value' => '12.5', 'pk' => 0]);
20        self::assertSame('amount', $column->name);
21        self::assertSame(8, $column->precision);
22        self::assertSame(2, $column->scale);
23        self::assertFalse($column->nullable);
24        self::assertSame(12.5, $column->default);
25    }
26    #[\PHPUnit\Framework\Attributes\DataProvider('providerPragmaColumns')]
27    public function testParseRetainsAllColumnMetadata(string $type, int $notNull, int $primaryKey, ?string $default, \SqlFixture\Schema\ColumnDefinition $expected): void
28    {
29        $column = (new Subject())->parse(['cid' => 0, 'name' => 'value', 'type' => $type, 'notnull' => $notNull, 'dflt_value' => $default, 'pk' => $primaryKey]);
30        self::assertEquals($expected, $column);
31    }
32
33    /**
34     * @return list<array{string, int, int, ?string, \SqlFixture\Schema\ColumnDefinition}>
35     */
36    public static function providerPragmaColumns(): array
37    {
38        return [
39            ['', 0, 0, null, new \SqlFixture\Schema\ColumnDefinition('value', 'BLOB')],
40            ['integer', 0, 1, null, new \SqlFixture\Schema\ColumnDefinition('value', 'INTEGER', nullable: false)],
41            ['integer', 0, 2, null, new \SqlFixture\Schema\ColumnDefinition('value', 'INTEGER', nullable: false)],
42            ['varchar ( 12 )', 0, 0, "'ready'", new \SqlFixture\Schema\ColumnDefinition('value', 'VARCHAR', length: 12, default: 'ready')],
43            ['decimal ( 8 , 2 )', 1, 0, '-12.5', new \SqlFixture\Schema\ColumnDefinition('value', 'DECIMAL', precision: 8, scale: 2, nullable: false, default: -12.5)],
44            ['text', 1, 0, null, new \SqlFixture\Schema\ColumnDefinition('value', 'TEXT', nullable: false)],
45        ];
46    }
47}
48