lord/mother 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

lord/mother

Composer 安装命令:

composer require --dev lord/mother

包简介

An Object Mother for PHP, providing nested object generation with attributes and extendable generators.

README 文档

README

Mother is an Object Mother for PHP, providing nested object generation with minimal configuration, attributes and extendable generators.

It is primarily intended for testing, but can also be used during early development to return realistic data to the frontend before features are fully implemented.

Installation

Installation via Composer - typically as a dev dependency, but can also be used in production for feature flagged prototyping if desired.

composer require lord/mother --dev

Basic usage

Objects can be generated a couple of ways:

<?php

use Lord\Mother\Mother;

# Generate an instance of UserData:
$user = Mother::make(UserData::class);

# Generate with specific properties overridden:
$user = Mother::make(UserData::class, [
    'name' => 'Jane Doe',
    'address.city' => 'London',
]);

# Generate multiple instances with the builder:
$users = Mother::for(UserData::class)
    ->with('status', 'active')
    ->populateNulls()
    ->make(10);

Tip

See the example dir for a more detailed demonstration of usage.

Test example

Mother makes Unit testing easier to write and maintain by simplifying the creation of test data.

<?php

it('crates a user', function () {
    $userRepoMock = mock(
        UserRepositoryInterface::class,
        static fn ($mock) => $mock
            ->shouldReceive('create')
            ->once()
            ->with('hire@aaron.codes', 'secret')
            ->andReturnUsing(static function (string $email) {
                // Use Mother to generate a UserData instance with the given email:
                return Mother::make(UserData::class, [
                    'email' => $email,
                ]);
            }
    );

    $sut = new CreateUserCommand($userRepoMock);

    $result = $sut->execute(
        email: 'hire@aaron.codes',
        password: 'secret',
    );

    expect($result)->toBeInstanceOf(UserData::class);
    expect($result->email)->toBe('hire@aaron.codes');
});

Mocking data example

Mother can also be used to generate mock data for use during development, such as in repositories or services to return semi-realistic data before features are fully implemented.

<?php

use Lord\Mother\Mother;

class UserRepository
{
    public function findUserByEmail(string $email): UserData
    {
        return Mother::make(UserData::class, [
            'email' => $email,
        ]);
    }
}

Advanced Usage

Value generators

Creating a custom value generator is simple - just implement the Lord\Mother\Contracts\ValueGeneratorInterface.

For example, here is a generator that creates UUID strings:

<?php

use Lord\Mother\Contracts\ValueGeneratorInterface;
use Lord\Mother\Reflection\PropertyDefinition;
use Lord\Mother\Support\Options;

class UuidGenerator implements ValueGeneratorInterface
{
    public function supports(PropertyDefinition $property, Options $options): bool
    {
        return $property->type === 'string' && $property->name === 'uuid';
    }

    public function generate(PropertyDefinition $property, Options $options): mixed
    {
        return \Ramsey\Uuid\Uuid::uuid4()->toString();
    }
}

If you register this generator with Mother, as per the supports method, it will be used whenever a property named uuid of type string is encountered:

<?php

Mother::register(new UuidGenerator());

Alternatively, you can apply a generator to a specific property using the attribute:

<?php

use Lord\Mother\Attributes\MotherUsing;

class ExampleData {
    #[MotherUsing(new UuidGenerator())]
    public string $id;
}

Note

The supports method is not called when using the attribute

Object generators

Another approach is to create an object generator, and apply it to a class using the MotherUsing attribute.

The contract for an object generator is very similar to a value generator, it implements Lord\Mother\Contracts\ObjectGeneratorInterface - where the difference is that the generate method returns ?object instead of mixed.

<?php

use Lord\Mother\Contracts\ValueGeneratorInterface;
use Lord\Mother\Reflection\PropertyDefinition;
use Lord\Mother\Support\Options;

class UuidGenerator implements ValueGeneratorInterface
{
    public function supports(PropertyDefinition $property, Options $options): bool
    {
        return $property->name === '__class__' && $property->type === UuidValue::class;
    }

    public function generate(PropertyDefinition $property, Options $options): ?object
    {
        return new UuidValue(
            value: \Ramsey\Uuid\Uuid::uuid4()->toString(),
        );
    }
}
<?php

use Lord\Mother\Attributes\MotherUsing;

#[MotherUsing(new UuidGenerator())]
class UuidValue {
    public function __construct(
        public string $value,
    ) {}
}

Options

Generation behaviour can be customised:

<?php

Mother::make(
    UserData::class,
    [],
    new Options(
        maxDepth: 2,
    )
);

// Or using an array:

Mother::make(
    UserData::class,
    [],
    [
        'maxDepth' => 2,
        'populateNulls' => false,
        'respectDefaultValues' => true,
    ]
);

Dependency injection

If you want to change how Mother works, you can provide your own container and with your own classes registered. See src/Contracts for the interfaces that can be implemented.

<?php

Mother::resolveContainerUsing(fn () => $container);

Testing

A shell command /bin/mother is provided to aid with development. Here are some examples of how to use it:

# Run the entire test suite
mother test

# Just run static analysis
mother phpstan

# Run a specific test, on a specific PHP version
mother --php=8.3 pest tests/Feature/MotherTest.php

lord/mother 适用场景与选型建议

lord/mother 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 3, 最近一次更新时间为 2026 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「testing」 「mother」 「object-mother」 「data-mother」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 lord/mother 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lord/mother 我们能提供哪些服务?
定制开发 / 二次开发

基于 lord/mother 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-03