classDatabaseFixtureProvider
class DatabaseFixtureProvider
extends Base
Public API: explicitly declared with
@visibility public.Faker provider that generates fixtures from database tables via PDO.
Automatically detects the database driver (MySQL, PostgreSQL, SQLite) and the release of the server, and uses the appropriate schema fetcher and type mapper.
$pdo = new \PDO('sqlite::memory:');
$pdo->exec('CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL)');
$provider = new \SqlFixture\Provider\DatabaseFixtureProvider(\Faker\Factory::create(), $pdo);
$provider->fixture('users', ['name' => 'Alice']) // => ['name' => 'Alice']$provider = new \SqlFixture\Provider\DatabaseFixtureProvider(\Faker\Factory::create(), new \PDO('sqlite::memory:'));
$provider->getVersion() // => 'sqlite-3.47.2'Methods§
public function __construct(
Generator $faker,
PDO $connection,
?TypeMapperInterface $typeMapper = null,
?HydratorInterface $hydrator = null,
?SchemaFetcherInterface $schemaFetcher = null,
string|null $version = null,
)Initializes the collaborators and declared state for this object.
Parameters
$faker | Generator | |
$connection | PDO | |
$typeMapper | ?TypeMapperInterface | |
$hydrator | ?HydratorInterface | |
$schemaFetcher | ?SchemaFetcherInterface | |
$version | string|null | Version tag such as 'mysql-8.4.7', or null to match the release the server reports |
Throws
DriverDetectionException If the connection does not report a driver name or a server versionUnsupportedDriverException If the driver of the connection is not supportedUnsupportedVersionException If the version tag is not a supported release of the driverCalls 8
- static-call
Base::__construct()line 60 - static-call PlatformFactory::detectDriver() line 62
- static-call PlatformFactory::detectVersion() line 63
- static-call PlatformFactory::createTypeMapper() line 65
- static-call PlatformFactory::createSchemaParser() line 66
- static-call PlatformFactory::createSchemaFetcher() line 68
- new DatabaseSchemaCache line 69
- new FixtureGenerator line 70
public function fixture<T of object>(
string $tableName,
array<string, mixed> $overrides = [],
class-string<T>|null $className = null,
): ($className is null ? array<string, mixed> : T)Generate a fixture from a database table.
Parameters
$tableName | string | Table name (e.g., "users" or "mydb.users") |
$overrides | array<string, mixed> | Override values |
$className | class-string<T>|null | Deserialization target class |
Returns
($className is null ? array<string, mixed> : T)Test cases 15
MySqlReleaseRoundTripTest::testRowGeneratedFromTheReportedSchemaIsAcceptedcallsPostgreSqlReleaseRoundTripTest::testRowGeneratedFromTheReportedSchemaIsAcceptedcallsSqliteIntegrationTest::allSqliteTypesFixturecallsSqliteIntegrationTest::databaseFixtureProviderWithSqlitecallsSqliteIntegrationTest::fixtureWithHydrationcallsSqliteIntegrationTest::fixtureWithOverridescallsSqliteIntegrationTest::insertAndSelectFixturecallsSqliteReleaseRoundTripTest::testRowGeneratedFromTheReportedSchemaIsAcceptedcallsDatabaseFixtureProviderTest::testClearCacheAllowsRefetchcallsDatabaseFixtureProviderTest::testFixtureGeneratesArrayFromSqliteTablecallsDatabaseFixtureProviderTest::testFixtureNormalizesQuotedTableNamescallsDatabaseFixtureProviderTest::testFixtureWithCustomSchemaFetchercallsDatabaseFixtureProviderTest::testFixtureWithCustomTypeMappercallsDatabaseFixtureProviderTest::testFixtureWithOverridescallsDatabaseFixtureProviderTest::testSchemaCacheWorksOnSecondCallcalls
Calls 2
- method-call DatabaseSchemaCache::getSchema() line 87
- method-call FixtureGenerator::generate() line 88
public function clearCache(): voidpublic function getFixtureGenerator(): FixtureGeneratorGet the underlying fixture generator.
Returns
Test cases 1
public function getDriver(): stringGet the detected driver name.
Returns
stringpublic function getVersion(): stringGet the version tag of the release of the connected server.
Returns
stringTest cases 5
MySqlReleaseRoundTripTest::testRowGeneratedFromTheReportedSchemaIsAcceptedcallsPostgreSqlReleaseRoundTripTest::testRowGeneratedFromTheReportedSchemaIsAcceptedcallsSqliteReleaseRoundTripTest::testRowGeneratedFromTheReportedSchemaIsAcceptedcallsDatabaseFixtureProviderTest::testGetVersionKeepsAnExplicitTagcallsDatabaseFixtureProviderTest::testGetVersionMatchesTheLinkedSqlitecalls
Private surface 5§
Implementation details, listed for orientation only.
private FixtureGenerator $fixtureGeneratorprivate SchemaFetcherInterface $schemaFetcherprivate string $driverprivate ServerVersion $versionprivate DatabaseSchemaCache $schemaCacheTest cases 19§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 11
DatabaseFixtureProviderTest::testClearCacheAllowsRefetchcallsDatabaseFixtureProviderTest::testFixtureGeneratesArrayFromSqliteTablecallsDatabaseFixtureProviderTest::testFixtureNormalizesQuotedTableNamescallsDatabaseFixtureProviderTest::testFixtureWithCustomSchemaFetchercallsDatabaseFixtureProviderTest::testFixtureWithCustomTypeMappercallsDatabaseFixtureProviderTest::testFixtureWithOverridescallsDatabaseFixtureProviderTest::testGetDriverReturnsSqlitecallsDatabaseFixtureProviderTest::testGetFixtureGeneratorReturnsInstancecallsDatabaseFixtureProviderTest::testGetVersionKeepsAnExplicitTagcallsDatabaseFixtureProviderTest::testGetVersionMatchesTheLinkedSqlitecallsDatabaseFixtureProviderTest::testSchemaCacheWorksOnSecondCallcalls
Other tests reaching this symbol 8
MySqlReleaseRoundTripTest::testRowGeneratedFromTheReportedSchemaIsAcceptedcallsPostgreSqlReleaseRoundTripTest::testRowGeneratedFromTheReportedSchemaIsAcceptedcallsSqliteIntegrationTest::allSqliteTypesFixturecallsSqliteIntegrationTest::databaseFixtureProviderWithSqlitecallsSqliteIntegrationTest::fixtureWithHydrationcallsSqliteIntegrationTest::fixtureWithOverridescallsSqliteIntegrationTest::insertAndSelectFixturecallsSqliteReleaseRoundTripTest::testRowGeneratedFromTheReportedSchemaIsAcceptedcalls