packages/sql-fixture/tests/Unit/Platform/MySql/MySqlSchemaFetcherTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Platform\MySql;
6
7use PDO;
8use PHPUnit\Framework\Attributes\CoversClass;
9use PHPUnit\Framework\TestCase;
10use SqlFixture\Platform\MySql\MySqlSchemaFetcher as Subject;
11
12#[CoversClass(Subject::class)]
13#[\PHPUnit\Framework\Attributes\Medium]
14#[CoversClass(\SqlFixture\Platform\MySql\Schema\CreateTableQuery::class)]
15#[CoversClass(\SqlFixture\Platform\MySql\Schema\IdentifierQuoter::class)]
16#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Platform\MySql\MySqlSchemaParser::class)]
17#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\ColumnDefinition::class)]
18#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\SchemaFetcherInterface::class)]
19#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\SchemaParseException::class)]
20#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\SchemaParserInterface::class)]
21#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\TableSchema::class)]
22#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Platform\MySql\Schema\ColumnParser::class)]
23#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Platform\MySql\Schema\DefaultExpression::class)]
24#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Platform\MySql\Schema\DefinitionIntegrity::class)]
25#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Platform\MySql\Schema\TableDefinition::class)]
26#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Platform\MySql\Schema\TypeParameters::class)]
27#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\TypeShape::class)]
28#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Platform\MySql\Schema\TableDefinitionInput::class)]
29#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\Exception\InvalidSqlException::class)]
30#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\Exception\ExpectedCreateTableException::class)]
31#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\Exception\MissingColumnDefinitionsException::class)]
32final class MySqlSchemaFetcherTest extends TestCase
33{
34    public function testFetchSchemaParsesLiveDatabaseDdl(): void
35    {
36        $pdo = new PDO(
37            (string) getenv('SQL_FIXTURE_MYSQL_DSN'),
38            (string) getenv('SQL_FIXTURE_MYSQL_USER'),
39            (string) getenv('SQL_FIXTURE_MYSQL_PASSWORD'),
40            [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false],
41        );
42        $pdo->exec('CREATE TEMPORARY TABLE users (id INT PRIMARY KEY, name VARCHAR(30) NOT NULL)');
43        $schema = (new Subject())->fetchSchema($pdo, 'users');
44        self::assertSame('users', $schema->tableName);
45        self::assertSame(['id'], $schema->primaryKeys);
46        self::assertSame(30, $schema->columns['name']->length);
47    }
48}
49