packages/sql-fixture/tests/Integration/Sqlite/SqliteReleaseRoundTripTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Integration\Sqlite;
6
7use Faker\Factory;
8use PDO;
9use PHPUnit\Framework\Attributes\CoversNothing;
10use PHPUnit\Framework\Attributes\DataProvider;
11use PHPUnit\Framework\Attributes\Medium;
12use PHPUnit\Framework\TestCase;
13use SqlFixture\Provider\DatabaseFixtureProvider;
14
15#[CoversNothing]
16#[Medium]
17final class SqliteReleaseRoundTripTest extends TestCase
18{
19    /**
20     * @return iterable<string, array{int}>
21     */
22    public static function providerSeeds(): iterable
23    {
24        yield 'seed 1' => [1];
25        yield 'seed 2' => [2];
26        yield 'seed 3' => [3];
27    }
28
29    #[DataProvider('providerSeeds')]
30    public function testRowGeneratedFromTheReportedSchemaIsAccepted(int $seed): void
31    {
32        $pdo = new PDO('sqlite::memory:', options: [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
33        $faker = Factory::create();
34        $faker->seed($seed);
35        $provider = new DatabaseFixtureProvider($faker, $pdo);
36        self::assertSame('sqlite-3.47.2', $provider->getVersion());
37
38        $pdo->exec(<<<'SQL'
39            CREATE TABLE fixture_rows (
40                id INTEGER PRIMARY KEY AUTOINCREMENT,
41                col_int INT NOT NULL,
42                col_tinyint TINYINT NOT NULL,
43                col_smallint SMALLINT NOT NULL,
44                col_mediumint MEDIUMINT NOT NULL,
45                col_bigint BIGINT NOT NULL,
46                col_text TEXT NOT NULL,
47                col_varchar VARCHAR(100) NOT NULL,
48                col_char CHAR(10) NOT NULL,
49                col_real REAL NOT NULL,
50                col_float FLOAT NOT NULL,
51                col_double DOUBLE NOT NULL,
52                col_decimal DECIMAL(10, 2) NOT NULL,
53                col_blob BLOB NOT NULL,
54                col_boolean BOOLEAN NOT NULL,
55                col_date DATE NOT NULL,
56                col_time TIME NOT NULL,
57                col_datetime DATETIME NOT NULL,
58                col_nullable VARCHAR(20) DEFAULT 'none'
59            )
60            SQL);
61        $row = $provider->fixture('fixture_rows');
62        $statement = $pdo->prepare(sprintf(
63            'INSERT INTO fixture_rows (%s) VALUES (%s)',
64            implode(', ', array_keys($row)),
65            implode(', ', array_fill(0, count($row), '?')),
66        ));
67        self::assertTrue($statement->execute(array_values(array_replace($row, array_map('intval', array_filter($row, 'is_bool'))))));
68        $count = $pdo->query('SELECT COUNT(*) FROM fixture_rows');
69        self::assertNotFalse($count);
70        self::assertSame(1, (int) $count->fetchColumn());
71    }
72}
73