PHP Class phpmock\Mock

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());
See also: MockBuilder
Author: Markus Malkusch ([email protected])
Inheritance: implements phpmock\Deactivatable
显示文件 Open project: php-mock/php-mock Class Usage Examples

Public Methods

Method Description
__construct ( string $namespace, string $name, callable $function ) Set the namespace, function name and the mock function.
call ( array $arguments ) : mixed Calls the mocked function.
define ( ) Defines the mocked function in the given namespace.
disable ( ) Disable this mock.
disableAll ( ) Disable all mocks.
enable ( ) Enables this mock.
getFQFN ( ) : string Returns the fully qualified function name.
getName ( ) : string Returns the unqualified function name.
getNamespace ( ) : string Returns the namespace without enclosing slashes.

Method Details

__construct() public method

Set the namespace, function name and the mock function.
public __construct ( string $namespace, string $name, callable $function )
$namespace string The namespace for the mock function.
$name string The function name of the mocked function.
$function callable The mock function.

call() public method

This method is called from the namespaced function.
public call ( array $arguments ) : mixed
$arguments array the call arguments.
return mixed

define() public method

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.
See also: enable()
public define ( )

disable() public method

Disable this mock.
See also: Mock::enable()
See also: Mock::disableAll()
public disable ( )

disableAll() public static method

Disable all mocks.
See also: Mock::enable()
See also: Mock::disable()
public static disableAll ( )

enable() public method

Enables this mock.
See also: Mock::disable()
See also: Mock::disableAll()
public enable ( )

getFQFN() public method

Returns the fully qualified function name.
public getFQFN ( ) : string
return string The function name with its namespace.

getName() public method

Returns the unqualified function name.
public getName ( ) : string
return string The name of the mocked function.

getNamespace() public method

Returns the namespace without enclosing slashes.
public getNamespace ( ) : string
return string The namespace