packages/sql-fixture/tests/Unit/Provider/DdlFileTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Provider;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9use SqlFixture\Provider\DdlFile as Subject;
10
11#[CoversClass(Subject::class)]
12#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Platform\Sqlite\SqliteSchemaParser::class)]
13#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\ColumnDefinition::class)]
14#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\SchemaParseException::class)]
15#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\SchemaParserInterface::class)]
16#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\TableSchema::class)]
17#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Platform\Sqlite\Schema\ColumnParser::class)]
18#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Platform\Sqlite\Schema\DefaultExpression::class)]
19#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Platform\Sqlite\Schema\DefinitionList::class)]
20#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Platform\Sqlite\Schema\TableSyntax::class)]
21#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Platform\Sqlite\Schema\TypeDeclaration::class)]
22#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\DefinitionSegments::class)]
23#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\TypeShape::class)]
24#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\Exception\InvalidSqlException::class)]
25#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\Exception\ExpectedCreateTableException::class)]
26#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Schema\Exception\MissingColumnDefinitionsException::class)]
27final class DdlFileTest extends TestCase
28{
29    public function testLoadSchemaFileReadsDdlAndIgnoresNonSchemaSql(): void
30    {
31        $file = tempnam(sys_get_temp_dir(), 'sql-fixture-ddl-');
32        self::assertNotFalse($file);
33        try {
34            file_put_contents($file, 'CREATE TABLE users (id INT PRIMARY KEY)');
35            $schema = (new Subject())->loadSchemaFile($file, new \SqlFixture\Platform\Sqlite\SqliteSchemaParser());
36            self::assertNotNull($schema);
37            self::assertSame('users', $schema->tableName);
38            file_put_contents($file, 'SELECT 1');
39            self::assertNull((new Subject())->loadSchemaFile($file, new \SqlFixture\Platform\Sqlite\SqliteSchemaParser()));
40        } finally {
41            unlink($file);
42        }
43    }
44}
45