Download the PHP package adriansuter/php-autoload-override without Composer
On this page you can find all versions of the php package adriansuter/php-autoload-override. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download adriansuter/php-autoload-override
More information about adriansuter/php-autoload-override
Files in adriansuter/php-autoload-override
Package php-autoload-override
Short Description Override global scoped fully qualified function calls inside your class methods in order to be able to mock them during testing.
License MIT
Homepage https://adriansuter.github.io/php-autoload-override/
Informations about the package php-autoload-override
PHP-Autoload-Override
This library allows overriding fully qualified function calls inside your class methods in order to be able to mock them during testing.
NOTE: The library can be used for other scenarios as well. But we recommend using it for testing purposes only.
Requirements
- PHP 8.2 or later
- Composer with PSR-4 (PSR-0 is not supported)
Installation
Usage with PHPUnit
Say we want to unit test the following class Probability.
The class uses \rand() from the global scope. Because we cannot control its output, we cannot
test pick() deterministically — until we override it.
Setting up the bootstrap
Open the bootstrap script
of your test suite and register the override. The recommended approach uses OverrideFactory:
Each entry in forClass() maps a function name to its real implementation, written as a
first-class callable.
OverrideFactory generates the override closure automatically: when a test sets a mock value via
MockRegistry::set(), that value is returned; otherwise the real \rand() is called. No mock
value is registered initially, so non-test code is unaffected.
For multiple classes, chain forClass() calls:
If you need the raw declarations array instead (e.g. for an AbstractIntegrationTestCase),
use build() instead of apply():
Writing the test
Set a mock value with MockRegistry::set() before calling the code under test, and reset it in
tearDown() so it does not affect other tests:
MockRegistry::reset(Probability::class) clears only the overrides for that class. Call
MockRegistry::reset() without arguments to clear all registered overrides at once.
Note that these overrides are only applied during the unit tests.
Sharing an override across multiple classes
MockRegistry::set() registers an override for one specific class. To register a fallback that
applies to every class, use setGlobal():
If a class also has a per-class override for the same function, the per-class value takes
priority. Reset only the global overrides with MockRegistry::resetGlobal().
Using Override::apply() directly
If you register overrides via Override::apply() directly rather than using OverrideFactory,
you write the closure yourself. MockRegistry::get() takes three arguments: the class name, the
function name, and a default that is returned when no mock is registered:
Be aware that the third argument — \rand($min, $max) — is evaluated on every call, even when a
mock value is set. This is harmless for \rand(), but if the real function is expensive or has
side effects that must be avoided when a mock is active, guard the call with MockRegistry::has():
Note: Using
$GLOBALSinside override closures still works and remains fully supported.MockRegistryis a cleaner alternative, not a replacement — existing code does not need to be migrated.
Learn More
License
The PHP-Autoload-Override library is licensed under the MIT license. See License File for more information.