marcoconsiglio/faker-php-number-helpers

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

Maintainers

Package info

github.com/MarcoConsiglio/faker-php-number-helpers

pkg:composer/marcoconsiglio/faker-php-number-helpers

Statistics

Installs: 56

Dependents: 3

Suggesters: 0

Stars: 0

Open Issues: 0

v4.2.1 2026-04-04 00:31 UTC

This package is auto-updated.

Last update: 2026-04-04 00:32:20 UTC


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.