documentSQL Fixture

README.md

SQL Fixture

Packagist Downloads PHP Version License: MIT Docs Ask DeepWiki

SQL Fixture is a FakerPHP provider that generates fixture rows from table definitions for MySQL, PostgreSQL, and SQLite. It reads the schema from CREATE TABLE statements (FixtureProvider), a live PDO connection (DatabaseFixtureProvider), or a directory of DDL files (FileFixtureProvider), and generates values that match each column's type, length, and constraints. Generated rows can be returned as arrays or hydrated into PHP objects, and related rows can be generated together from a fixture plan.

Requirements

Support Syntax

The following database versions are supported. Pass the dialect and, optionally, the version tag to FixtureProvider or FileFixtureProvider; omitting the version tag uses the default for that database. DatabaseFixtureProvider reads both from the connection.

MySQL

VersionVersion tagDefault
5.6.51mysql-5.6.51
5.7.44mysql-5.7.44
8.0.44mysql-8.0.44
8.1.0mysql-8.1.0
8.2.0mysql-8.2.0
8.3.0mysql-8.3.0
8.4.7mysql-8.4.7Yes
9.0.1mysql-9.0.1
9.1.0mysql-9.1.0

PostgreSQL

VersionVersion tagDefault
16.6pg-16.6
17.2pg-17.2Yes

SQLite

VersionVersion tagDefault
3.47.2sqlite-3.47.2Yes

Installation

composer require --dev k-kinzal/sql-fixture

Usage

use Faker\Factory;
use SqlFixture\Provider\FixtureProvider;

$faker = Factory::create();
$faker->addProvider(new FixtureProvider($faker));

$user = $faker->fixture(
    'CREATE TABLE users (
        id INT PRIMARY KEY AUTO_INCREMENT,
        name VARCHAR(255) NOT NULL,
        status ENUM("active", "inactive") NOT NULL
    )',
    ['name' => 'Alice'],
);
// For example: ['name' => 'Alice', 'status' => 'active']

To read the statements as another release does, pass the dialect and the version tag:

$provider = new FixtureProvider($faker, dialect: 'mysql', version: 'mysql-8.0.44');
$provider->getVersion(); // 'mysql-8.0.44'

DatabaseFixtureProvider detects the release of the connected server:

use SqlFixture\Provider\DatabaseFixtureProvider;

$provider = new DatabaseFixtureProvider($faker, $pdo);
$provider->getVersion(); // For example: 'mysql-8.0.44' for a MySQL 8.0 server

License

MIT License. See LICENSE for details.