PHP code example of bizkit / phpunit-function-mock

1. Go to this page and download the library: Download bizkit/phpunit-function-mock 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/ */

    

bizkit / phpunit-function-mock example snippets




declare(strict_types=1);

namespace App\Tests\Service;

use App\Service\TokenGenerator;
use Bizkit\PHPUnitFunctionMock\Attribute\RegisterFunctionMock;
use Bizkit\PHPUnitFunctionMock\FunctionMock;
use PHPUnit\Framework\TestCase;

#[RegisterFunctionMock('random_int')]
final class TokenGeneratorTest extends TestCase
{
    public function testGenerateToken(): void
    {
        FunctionMock::mock('random_int', static fn (int $min, int $max): int => 1234);

        self::assertSame('token-1234', new TokenGenerator()->generate());
    }
}

#[RegisterFunctionMock(['file_exists', 'is_file'])]
final class FileLoaderTest extends TestCase
{
    public function testLoad(): void
    {
        FunctionMock::mockMany([
            'file_exists' => static fn (string $path): bool => true,
            'is_file' => static fn (string $path): bool => true,
        ]);

        // ...
    }
}

use App\Service\TokenGenerator;
use Bizkit\PHPUnitFunctionMock\Attribute\RegisterFunctionMock;

#[RegisterFunctionMock('random_int', class: TokenGenerator::class)]
final class TokenGeneratorTest extends TestCase
{
}

use App\Service\TokenGenerator;
use Bizkit\PHPUnitFunctionMock\FunctionMock;

FunctionMock::register(TokenGenerator::class, 'random_int');
FunctionMock::mock('random_int', static fn (int $min, int $max): int => 1234);

FunctionMock::mock('random_int', null);

protected function tearDown(): void
{
    FunctionMock::reset();
}

namespace App\Service;

random_int(1, 10); // can be mocked
\random_int(1, 10); // cannot be mocked by this library