定制 dknx01/data-fixtures-phpunit 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dknx01/data-fixtures-phpunit

最新稳定版本:0.1.3

Composer 安装命令:

composer require dknx01/data-fixtures-phpunit

包简介

Symfony Bundle to use Doctrine data fixtures in PHPUnit tests.

README 文档

README

This is a Symfony Bundle to use Symfony DoctrineFixturesBundle in PHPUnit test directly.

Requirements

Install

  1. Create a config file (see Minimal Configuration)
  2. 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.

Configuration

Ensure you have the following file:

# config/packages/data_fixtures_php_unit.yaml
data_fixtures_php_unit:
  faker:
    locale: 'de_DE' # optional - defaults to en_EN
    providers:
      - 'App\Tests\Faker\Bundeslaender' # optional - defaults to empty array

locale and providers are optional and only needed if you want to change the default behavior.

Minimal Configuration

The minimal setup would look like:

# config/packages/data_fixtures_php_unit.yaml
data_fixtures_php_unit:
  faker:

Fixtures registration

You should have all your Fixture classes registered as a services - at leat inside the test container.

All Fixtures that are in the namespace App\Tests\Fixtures\ and inside the folder '%kernel.project_dir%/tests/Fixtures' are automatically registered.

Example registration:

when@test:
  services:
    _defaults:
      autowire: true
      autoconfigure: true
      public: true
    App\Tests\Fixtures\:
      resource: '%kernel.project_dir%/tests/Fixtures'

Writing a fixture

Each fixture must implement the Doctrine\Common\DataFixtures\FixtureInterface;. Example:

<?php

namespace App\Tests\Fixtures;

use App\Entity\User;
use Dknx01\DataFixturesPhpUnit\Contract\FakerAware;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Persistence\ObjectManager;
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. You can use the way Doctrine data fixtures is suggesting, or you can use an attribute.

#[DependFixture(BarFixture::class)]
class FooFixture implements FixtureInterface, FakerAware
{
    // your code
}

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.

Limitations

  • A fixture class can only be used once for a test, regardless of whether the DataFixture is defined on a class basis or a method basis
    • This is invalid and will only execute on fixture, mostly the latest defined one
    #[DataFixture(new BarFixture('first'))]
    #[DataFixture(BarFixture::class)]
    public function testFoo(): void
    {
    // code
    }

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固