Download the PHP package bizkit/phpunit-function-mock without Composer

On this page you can find all versions of the php package bizkit/phpunit-function-mock. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package phpunit-function-mock

Bizkit PHPUnit Function Mock

Latest Stable Version Build Status Code Coverage License

Provides a small PHPUnit extension for mocking native PHP functions from tests.

The idea is based on Symfony PHPUnit Bridge's ClockMock and DnsMock: register a function in the tested namespace and dispatch that function through a test-controlled mock. See Symfony's Clock Mocking and DNS-sensitive tests documentation for the original pattern.

Requirements

Installation

PHPUnit Configuration

Register the extension in your PHPUnit configuration:

Usage

Use #[RegisterFunctionMock] on the test class or method to register the function hook. Configure the callable in the test body with FunctionMock.

When the test suite is loaded, the extension registers a random_int() function in the test namespace and in the matching application namespace with Tests removed. For example, App\Tests\Service\TokenGeneratorTest also registers mocks for App\Service.

Multiple Functions

Pass a list when a test needs several functions:

#[RegisterFunctionMock] is repeatable, so the same class or method can declare multiple #[RegisterFunctionMock] attributes.

Class Override

Use class: when the tested code lives in a namespace that cannot be inferred from the test class name.

The function is registered in the namespace of the class passed to class:.

Manual Registration

You can also register functions without PHPUnit attributes:

Pass null to remove a single mock and restore native fallback for that function:

Cleanup

Mocks are global process state. The PHPUnit extension clears configured callables after annotated tests finish, error, or skip. If you configure mocks manually in unannotated tests, clear them yourself:

Known Limitations

This library relies on PHP's fallback to the global namespace for functions. In namespaced code, an unqualified function call such as random_int() first checks for a function in the current namespace, then falls back to \random_int().

Because of that, mocks only work for unqualified function calls:

The namespaced function must also be registered before the target namespace calls that function for the first time. PHP can cache the first function resolution in its literal cache, so if App\Service\random_int() does not exist yet and PHP resolves the first call to \random_int(), later defining App\Service\random_int() may not affect that already-compiled call site. See PHP bug #64346. This is why the attribute-based extension registers functions when PHPUnit loads the test suite, before test methods execute.

Other limitations:

Versioning

This project follows Semantic Versioning 2.0.0.

Reporting Issues

Use the project's issue tracker to report bugs or request improvements.

License

See the LICENSE file for details (MIT).


All versions of phpunit-function-mock with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
phpunit/phpunit Version ^10.5 || ^11.5 || ^12.5 || ^13
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package bizkit/phpunit-function-mock contains the following files

Loading the files please wait ...