marcoconsiglio/faker-php-number-helpers
Composer 安装命令:
composer require marcoconsiglio/faker-php-number-helpers
包简介
Adds a helper trait that makes it easier to generate random numbers using FakerPHP.
README 文档
README
faker-php-number-helpers
Adds a helper trait that makes it easier to generate random numbers using FakerPHP.
Index
Installation
Use it as a production dependency
composer require marcoconsiglio/faker-php-number-helpers
or a development dependency
composer require --dev marcoconsiglio/faker-php-number-helpers
Usage
Add the trait to a TestCase class if you use this library in your tests project written in PHPUnit, otherwise add it wherever you need it.
<?php namespace MyCompany\Project\Tests\Unit; use MarcoConsiglio\FakerPhpNumberHelpers\WithFakerHelpers; use PHPUnit\Framework\TestCase; class MyUnitTestCase extends TestCase { use WithFakerHelpers; protected function setUp(): void { parent::setUp(); // Set up faker before using the methods in the trait // with your needed locale. $this->setUpFaker("en_GB"); } public function test_example(): void { // Arrange $int = $this->randomInteger(); // 45465 $float = $this->randomFloat(); // 89354.454687684 } }
In some tests, you'll need a PHPUnit data provider, which is a static function. In this case you can call statically setUpFaker() method in order to set up a static Faker\Generator when PHPUnit call the data provider method, which is done prior to call the setUp() method.
<?php namespace MyCompany\Project\Tests\Unit; use MarcoConsiglio\FakerPhpNumberHelpers\WithFakerHelpers; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class MyUnitTestCase extends TestCase { use WithFakerHelpers; /** * This won't work as this method is called after * myDataProvider(). */ protected function setUp(): void { parent::setUp(); $this->setUpFaker(); // Too late! } public static function myDataProvider(): array { self::setUpFaker(); // Early call! return [ [self::randomInteger()] ]; } #[DataProvider("myDataProvider")] public function test_something(int $number): void { // Act & Assert $this->assertIsInt($number); } }
Constants
Several constants provide the limit for random generation.
Integers
| Constant | Value |
|---|---|
IntRange::MIN |
PHP_INT_MIN + 1 |
IntRange::MAX |
PHP_INT_MAX |
Floats
| Constant | Value |
|---|---|
FloatRange::MIN |
-PHP_FLOAT_MAX |
FloatRange::MAX |
PHP_FLOAT_MAX |
FloatRange::MICRO |
PHP_FLOAT_MIN |
FloatRange::MAX_FRACTION |
4,503,599,627,370,495.0 |
FloatRange::MIN_FRACTION |
-4,503,599,627,370,495.0 |
Available methods
Integer generation
| Method | Minimum | Maximum | Excluded |
|---|---|---|---|
randomInteger() |
PHP_INT_MIN |
PHP_INT_MAX |
|
positiveRandomInteger() |
0 | PHP_INT_MAX |
|
negativeRandomInteger() |
PHP_INT_MIN + 1 |
-1 | 0 |
nonZeroRandomInteger() |
PHP_INT_MIN + 1 |
PHP_INT_MAX |
0 |
positiveNonZeroRandomInteger() |
1 | PHP_INT_MAX |
0 |
negativeNonZeroRandomInteger() |
PHP_INT_MIN + 1 |
-1 | 0 |
Float generation
| Method | Minimum | Maximum | Excluded |
|---|---|---|---|
randomFloat() |
-PHP_FLOAT_MAX |
PHP_FLOAT_MAX |
|
positiveRandomFloat() |
0 | PHP_FLOAT_MAX |
|
negativeRandomFloat() |
-PHP_FLOAT_MAX |
0 | 0 |
nonZeroRandomFloat() |
-PHP_FLOAT_MAX |
PHP_FLOAT_MAX |
0 |
positiveNonZeroRandomFloat() |
PHP_FLOAT_MIN |
PHP_FLOAT_MAX |
0 |
negativeNonZeroRandomFloat() |
-PHP_FLOAT_MAX |
-PHP_FLOAT_MIN |
0 |
randomFraction() |
FloatRange::MIN_FRACTION |
FloatRange::MAX_FRACTION |
integer floats |
positiveRandomFraction() |
PHP_FLOAT_MIN |
FloatRange::MAX_FRACTION |
integer floats |
negativeRandomFraction() |
FloatRange::MIN_FRACTION |
-PHP_FLOAT_MIN |
integer floats |
Next float number
Some times you need to have the previous/next representable float adjacent to a specific number.
To solve this problem you can use the NextFloat class, which is a convenient wrapper of the nsfisis/php-next-after library.
<?php namespace MyCompany\Project; use MarcoConsiglio\FakerPhpNumberHelpers\NextFloat; class MyClass { /** * Return the next representable `float` near 3.5. */ public function nextFloat(): float { return NextFloat::after(3.5); } }
API Documentation
See more in the API Documentation at ./docs/html/index.html.
marcoconsiglio/faker-php-number-helpers 适用场景与选型建议
marcoconsiglio/faker-php-number-helpers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 211 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 marcoconsiglio/faker-php-number-helpers 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 marcoconsiglio/faker-php-number-helpers 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 211
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 30
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-20