PHP code example of marcoconsiglio / faker-php-number-helpers

1. Go to this page and download the library: Download marcoconsiglio/faker-php-number-helpers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

marcoconsiglio / faker-php-number-helpers example snippets



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
    }
}


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);
    }
}


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);
    }
}

composer 

composer