dknx01/data-fixtures
Composer 安装命令:
composer require dknx01/data-fixtures
包简介
A PHP package for data fixtures with plain PDO connection.
关键字:
README 文档
README
A PHP package for data fixtures with plain PDO connection.
Requirements
- ext-pdo for PDO connections
- (optional, but recommended) Faker
Install
- run
composer require --dev dknx01/data-fixtures-phpunit
Usage
If you want to use data fixtures in you tests you can do it in multiple ways. You can write a method to fill data in the database. or you can use a fixture and reuse it in multiple tests.
With an existing PDO connection
If you already have a PDO connection you can use it directly.
new Configuration(pdo: $pdo, databaseName: 'foo');
Create a new PDO connection
new Configuration(dsn: 'mysql:host=localhost;port=3307;dbname=testdb', user: 'foo', password: 'bar', options: []);
Configuration:
The Configuration object has the following parameters:
| Parameter | Description |
|---|---|
| pdo | An existing PDO connection that will be used |
| dsn | The dsn of the PDO connection |
| user | Databases user, if needed |
| password | Databases users password, if needed |
| options | PDO options that will be used for the connection |
Load and execute fixtures
Your class should use the DataFixtureTrait.
/// your code $this->loadFixtures(); $this->executeFixtures($configuration); /// your code
If you do not want to use the trait, you can load and execute the fixtures this way:
// your code $fixtures = new FixtureCollection(); $fixtures->add(new SimpleFixture()); $handler = new FixtureHandler($configuration); $handler->handle($fixtures); // your code
loadFixtures(int $stackPosition = 1)
This option should be used to define at which stack position the fixtures should be loaded from. Mostly it will be "1" which means directly from the method/class the "loadFixtures" method is called from. If you have more inherited classes, you can change the number.
Writing a fixture
Each fixture must implement the Dknx01\DataFixtures\Contract\FixtureInterface.
Example:
<?php namespace App\Tests\Fixtures; use App\Entity\User; use Dknx01\DataFixtures\Contract\FakerAware; use Dknx01\DataFixtures\Contract\FixtureInterface; use Faker\Generator; readonly class UserFixture implements FixtureInterface, FakerAware { private Generator $faker; public function __construct(private string $email = '') { } public function setFaker(Generator $faker): void { $this->faker = $faker; } public function load(ObjectManager $manager): void { $user = new User(); $user->setEmail(!empty($this->email) ? $this->email : $this->faker->unique()->safeEmail()); $user->setPassword('foo'); $user->setRoles(['ROLE_USER']); $manager->persist($user); $manager->flush(); } } #[DataFixture(new UserFixture('test@fooo.nlkdjlfs'))] class ApiTest extends ApplicationTestCase { // your code } #[DataFixture(UserFixture::class)] class ApiTest extends ApplicationTestCase { // your code }
As you can see the fixture can have constructor arguments for individual data in different tests.
Data Fixture on method level
Data fixtures can be used on class level (see above) and on method level.
#[DataFixture(UserFixture::class)] #[DataFixture(new BlaFixture( name: 'Test123', fileName: 'Test123' ))] public function testFoo(): void { // cor code }
Dependent Fixtures
Fixtures can depend on other fixtures.
#[DependFixture(BarFixture::class)] class FooFixture implements FixtureInterface, FakerAware { // your code }
Ordered Fixtures
Fixtures can have an order at which position they should load in the fixture loading process.
If a position is added multiple times, the early will move one step ahead of the latest ones.
Example:
// FIXTURE 1 #[\Dknx01\DataFixtures\Attributes\OrderedFixture(1)] // FIXTURE 2 #[\Dknx01\DataFixtures\Attributes\OrderedFixture(1)] // Result: [ 0 => 'FIXTURE 1', 1 => 'FIXTURE 2' ]
This attribute will overwrite the DependFixture attribute!
#[\Dknx01\DataFixtures\Attributes\OrderedFixture(0)] class FooFixture implements FixtureInterface, FakerAware { // your code }
In this example the FooFixture is loaded at position 0 which means at the beginning of all fixtures.
Faker
As you can see it is possible to use PHPFaker inside a fixture class.
If you implement the FakerAware interface a Faker instance is automatically injected into the data fixture.
Examples
You can see some examples in the directory examples or you can run the script
php examples/Examples.php
dknx01/data-fixtures 适用场景与选型建议
dknx01/data-fixtures 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「persistence」 「Fixture」 「tests」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dknx01/data-fixtures 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dknx01/data-fixtures 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dknx01/data-fixtures 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Behat redis context for testing
This bundle provides tools to build persistence-agnostic storage layer.
Fixtures replacement with a straightforward definition syntax
Propel2 is an open-source Object-Relational Mapping (ORM) for PHP 5.5 and up.
A cli tool which generates unit tests.
Kinikit - PHP Application development framework MVC component
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 31
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-11