jefferson-lima/fixture-factory
Composer 安装命令:
composer require jefferson-lima/fixture-factory
包简介
README 文档
README
Generates fixture objects to be used in tests.
Installation
composer require jefferson-lima/fixture-factory
Usage
Fixture Factory uses the metadata of your class properties to generate values for them. The value generated for a
property is derived from its @var tag. Thus, first you must annotate your properties with their types:
MyClass {
/** @var string */
public $foo;
public $bar;
}
The properties without a @var tag, or with and invalid one, will be ignored.
Then you can use the FixtureFactory to create a fixture for your class:
$myFixture = FixtureFactory::createFixture(MyClass::class);
Fixture Factory uses Faker to generate values.
Supported types
Currently, the supported types are string, int, bool, float. Objects are supported, as long
as a concrete class is provided. Abstract classes or interfaces are not supported as it's not possible
to determine the concrete implementation that should be used.
Nested objects will be generated recursively.
Overriding attributes
It's possible to override attributes by passing an array as a second argument to the createFixture
method:
$myFixture = FixtureFactory::createFixture(MyClass::class, ['foo' => 'bar']);
Circular references
Care must be taken with circular references, they must be avoided or overridden, otherwise an exception will be thrown. One example of circular reference is shown below:
class Foo {
/** @var Bar */
private $bar;
}
class Bar {
/** @var Foo */
private $foo;
}
A circular reference can be broken by overriding one attribute:
$myFixture = FixtureFactory::createFixture(MyClass::class, ['bar' => null]);
Doctrine annotations can also be used to resolve circular references, for example:
class Foo {
/**
* @var Bar
* @OneToOne(targetEntity="Bar", inversedBy="foo")
*/
private $bar;
}
class Bar {
/**
* @var Foo
* @OneToOne(targetEntity="Foo", mappedBy="bar")
*/
private $foo;
}
In this case, it's possible to identify that foo points to bar, and that bar points back
at it.
Symfony constraints
It's also possible to use Symfony validation constraints to narrow down the values generated for your properties. These are the currently supported constraints listed by order of precedence:
Strings:
Some constraints can't be applied simultaneously. In this case, the constraint with higher precedence is applied. For example:
/**
* @Length(min=255)
* @Email
*/
private $email;
In this case, the @Email constraint is applied, and the @Length will be ignored.
Doctrine Associations
The following Doctrine associations are supported:
For many side of the associations an ArrayCollection with two elements is created.
License
This project is licensed under the MIT License - see the LICENSE.md file for details
jefferson-lima/fixture-factory 适用场景与选型建议
jefferson-lima/fixture-factory 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 06 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jefferson-lima/fixture-factory 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jefferson-lima/fixture-factory 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-06-24