Overview

Namespaces

  • phpmock
    • environment
    • functions
    • spy

Classes

  • Mock
  • MockBuilder

Interfaces

  • Deactivatable

Exceptions

  • MockEnabledException
  • Overview
  • Namespace
  • Class

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\Spy
Namespace: phpmock
License: WTFPL
Author: Markus Malkusch markus@malkusch.de
See: phpmock\MockBuilder
Link: Donations
Located at Mock.php
Methods summary
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::disable()
Mock::disableAll()

Suppresswarnings(phpmd)

public
# disable( )

Disable this mock.

Disable this mock.

See

Mock::enable()
Mock::disableAll()

Implementation of

phpmock\Deactivatable::disable()
public static
# disableAll( )

Disable all mocks.

Disable all mocks.

See

Mock::enable()
Mock::disable()
public string
# getNamespace( )

Returns the namespace without enclosing slashes.

Returns the namespace without enclosing slashes.

Returns

string
The namespace
public string
# getName( )

Returns the unqualified function name.

Returns the unqualified function name.

Returns

string
The name of the mocked function.
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.

See

phpmock\Mock::enable()

Link

Bug #68541
API documentation generated by ApiGen