k-kinzal / sql-fixture
FakerPHP provider that generates fixture rows from MySQL, PostgreSQL, and SQLite table definitions
Requires
- php: ^8.1
- fakerphp/faker: ^1.23
- phpmyadmin/sql-parser: ^5.11
Requires (Dev)
- brianium/paratest: ^6.11 || ^7
- friendsofphp/php-cs-fixer: ^3.92
- infection/infection: ^0.28 || ^0.29 || ^0.30 || ^0.31 || ^0.32
- k-kinzal/sql-faker: ^0.1
- k-kinzal/testcontainers-php: ^0.5.1
- nikic/php-fuzzer: ^0.0.11
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^10.5 || ^11 || ^12 || ^13
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
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
- PHP 8.1+
- fakerphp/faker ^1.23
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
| Version | Version tag | Default |
|---|---|---|
| 5.6.51 | mysql-5.6.51 |
|
| 5.7.44 | mysql-5.7.44 |
|
| 8.0.44 | mysql-8.0.44 |
|
| 8.1.0 | mysql-8.1.0 |
|
| 8.2.0 | mysql-8.2.0 |
|
| 8.3.0 | mysql-8.3.0 |
|
| 8.4.7 | mysql-8.4.7 |
Yes |
| 9.0.1 | mysql-9.0.1 |
|
| 9.1.0 | mysql-9.1.0 |
PostgreSQL
| Version | Version tag | Default |
|---|---|---|
| 16.6 | pg-16.6 |
|
| 17.2 | pg-17.2 |
Yes |
SQLite
| Version | Version tag | Default |
|---|---|---|
| 3.47.2 | sqlite-3.47.2 |
Yes |
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.