marcoconsiglio/faker-php-number-helpers 问题修复 & 功能扩展

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

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

marcoconsiglio/faker-php-number-helpers

最新稳定版本:v4.2.1

Composer 安装命令:

composer require marcoconsiglio/faker-php-number-helpers

包简介

Adds a helper trait that makes it easier to generate random numbers using FakerPHP.

README 文档

README

GitHub License GitHub Release Static Badge
Static Badge Static Badge Static Badge

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.

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固