\ShmockShmock

The Shmock\Shmock class is the entry point to the fluent Shmock interface. You may use this class directly to create mocks. Alternatively, use the Shmockers trait to include shorthand versions of create() and create_class() in your test cases.

Summary

Methods
Properties
Constants
create()
create_class()
add_policy()
clear_policies()
$policies
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Properties

$policies

$policies : \Shmock\Policy[]

Type

\Shmock\Policy[] — Do not modify this directly, use {add_policy()} and {clear_policies()}

Methods

create()

create(\PHPUnit_Framework_TestCase $test_case, string $class, callable $closure) : mixed

Create an instance of a mock object. Shmock uses a build / replay model for building mock objects.

The third argument to the create method is a callable that acts as the mock's build phase. The resulting object from the create method is the object in the replay phase.

Sample usage:

// build a mock of MyCalculator, expecting a call to add
// with arguments [1,2] and return the value 3, exactly once.

$mock = \Shmock\Shmock::create($this, 'MyCalculator', function ($calc) {
  $calc->add(1,2)->return_value(3);
});

In the example above, the invocation target of the method add(1,2) is an of \Shmock\Instance. This instance will allow you to mock any instance method on the class MyCalculator, so it might allow add or subtract, but not openFileStream() or sbutract. The result of the method is an instance of \Shmock\PHPUnitSpec, which contains many of the familiar expectation-setting methods for mock frameworks.

You may easily design your own build / replay lifecycle to meet your needs by using the Instance and StaticClass classes directly.

$shmock = new \Shmock\Instance($this, 'MyCalculator');
$shmock->add(1,2)->return_value(3);
$mock = $shmock->replay();

Parameters

\PHPUnit_Framework_TestCase $test_case
string $class

the class being mocked

callable $closure

the build phase of the mock

Returns

mixed —

An instance of a subclass of $class. PHPUnit mocks require that all mocks be subclasses of the target class in order to replace target methods. For this reason, mocking will fail if the class is final.

create_class()

create_class(\PHPUnit_Framework_TestCase $test_case, string $class, callable $closure) : string

Create a mock class. Mock classes go through the build / replay lifecycle like mock instances do.

Parameters

\PHPUnit_Framework_TestCase $test_case
string $class

the class to be mocked

callable $closure

the closure to apply to the class mock in its build phase.

Returns

string —

a subclass of $class that has mock expectations set on it.

add_policy()

add_policy(\Shmock\Policy $policy) : void

Add a policy to Shmock that ensures qualities about mock objects as they are created. Policies allow you to highly customize the behavior of Shmock.

Parameters

\Shmock\Policy $policy

clear_policies()

clear_policies() : void

Clears any set policies.