$policies
$policies : \Shmock\Policy[]
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.
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();
\PHPUnit_Framework_TestCase | $test_case | |
string | $class | the class being mocked |
callable | $closure | the build phase of the mock |
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(\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.
\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. |
a subclass of $class that has mock expectations set on it.
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.
\Shmock\Policy | $policy |