Class Mock
Mocking framework for built-in PHP functions.
Mocking a build-in PHP function is achieved by using PHP's namespace fallback policy. A mock will provide the namespaced function. I.e. only unqualified functions in a non-global namespace can be mocked.
Example:
namespace foo; use phpmock\Mock; $time = new Mock( __NAMESPACE__, "time", function () { return 3; } ); $time->enable(); assert (3 == time()); $time->disable(); assert (3 != time());
- phpmock\Mock implements phpmock\Deactivatable
Direct known subclasses
phpmock\spy\SpyLicense: WTFPL
Author: Markus Malkusch markus@malkusch.de
See:
phpmock\MockBuilder Link: Donations
Located at Mock.php
public
__construct( string $namespace, string $name, callable $function )
Set the namespace, function name and the mock function.
Set the namespace, function name and the mock function.
Parameters
- $namespace
- The namespace for the mock function.
- $name
- The function name of the mocked function.
- $function
- The mock function.
public
enable( )
Enables this mock.
Enables this mock.
Throws
phpmock\MockEnabledException If the function has already an enabled mock.
See
Mock::disableAll()
Suppresswarnings(phpmd)
public
public static
public
string
getNamespace( )
Returns the namespace without enclosing slashes.
Returns the namespace without enclosing slashes.
Returns
The namespace
public
string
public
define( )
Defines the mocked function in the given namespace.
Defines the mocked function in the given namespace.
In most cases you don't have to call this method. enable() is doing this for you. But if the mock is defined after the first call in the tested class, the tested class doesn't resolve to the mock. This is documented in Bug #68541. You therefore have to define the namespaced function before the first call. Defining the function has no side effects as you still have to enable the mock. If the function was already defined this method does nothing.