\ShmockPHPUnitSpec

A spec is used during the build phase of mock creation to configure the expectations on a single invocation of a method on a mock.

 // return_value() and twice() are defined on PHPUnitSpec
 $calc->add(1,2)->return_value(3)->twice();

Summary

Methods
Properties
Constants
__construct()
times()
once()
twice()
any()
never()
at_least_once()
will()
return_value_map()
return_true()
return_false()
return_null()
return_value()
return_this()
throw_exception()
return_consecutively()
return_shmock()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

__construct()

__construct(\PHPUnit_Framework_TestCase $test, \Shmock\Instance $shmock, string $method, array $with, bool $order_matters, int $call_index)

This class is not directly instantiated, but instead returned every time you invoke a method on a \Shmock\Instance or \Shmock\StaticClass.

Specs in Shmock make the assumption that because you are mocking the method that it will be invoked at least once. This is a default setting on the mock and can be changed by specifying a number of times to invoke the method, as done above with the twice() method. Callers intending to invoke methods more than once with different arguments are directed to the return_value_map facility or to specify $mock->order_matters();.

A few options on specs can be in conflict with each other. For example, you may elect to return a value to the caller via return_value($v), throw an exception via throw_exception($e) or to perform a custom action via will(function () {}). In cases of a conflict, last expectation set wins.

PHPUnit specs also accept standar \PHPUnit_Framework_Constraint instances as arguments, so wildcard arguments or other helpers from PHPUnit are available for use:

 $calc->add($this->greaterThan(1), $this->lessThan(2));

Parameters

\PHPUnit_Framework_TestCase $test
\Shmock\Instance $shmock
string $method
array $with
bool $order_matters
int $call_index

times()

times(int $times) : \Shmock\PHPUnitSpec

Specify that the method will be invoked $times times.

 // expect notify will be called 5 times
 $shmock->notify()->times(5);

Parameters

int $times

the number of times to expect the given call

Returns

\Shmock\PHPUnitSpec

once()

once() : \Shmock\PHPUnitSpec

Specify that the method will be invoked once.

This is a shorthand for times(1)

Returns

\Shmock\PHPUnitSpec

twice()

twice() : \Shmock\PHPUnitSpec

Specify that the method will be invoked twice.

This is a shorthand for times(2)

Returns

\Shmock\PHPUnitSpec

any()

any() : \Shmock\PHPUnitSpec

Specifies that the number of invocations of this method is not to be verified by Shmock.

 $shmock->notify()->any();

This is a shorthand for times(null)

Returns

\Shmock\PHPUnitSpec

never()

never() : \Shmock\PHPUnitSpec

Specifies that this method is never to be invoked.

This is an alias for times(0)

Returns

\Shmock\PHPUnitSpec

at_least_once()

at_least_once() : \Shmock\PHPUnitSpec

Specifies that the method is to be invoked at least once but possibly more.

This directive is only respected if no other calls to times() have been recorded.

 $shmock->notify()->at_least_once();

Returns

\Shmock\PHPUnitSpec

will()

will(callable $will_closure) : \Shmock\PHPUnitSpec

Specifies that the given closure will be executed on invocation.

The first argument to the closure is an instance of \PHPUnit_Framework_MockObject_Invocation.

 // custom action with a closure

 $shmock->notify()->will(function ($invocation) {
   $this->assertTrue(count($invocation->parameters) > 2);
 });

Parameters

callable $will_closure

Returns

\Shmock\PHPUnitSpec

return_value_map()

return_value_map(mixed[][] $map_of_args_to_values) : \Shmock\PHPUnitSpec

An order-agnostic set of return values given a set of inputs.

Parameters

mixed[][] $map_of_args_to_values

an array of arrays of arguments with the final value of the array being the return value.

Returns

\Shmock\PHPUnitSpec

For example, if you were simulating addition:

$shmock_calculator->add()->return_value_map([
    [1, 2, 3], // 1 + 2 = 3
    [10, 15, 25],
    [11, 11, 22]
]);

return_true()

return_true() : \Shmock\PHPUnitSpec

Specifies that the method will return true.

This is a shorthand for return_value(true)

Returns

\Shmock\PHPUnitSpec

return_false()

return_false() : \Shmock\PHPUnitSpec

Specifies that the method will return false.

This is a shorthand for return_value(false)

Returns

\Shmock\PHPUnitSpec

return_null()

return_null() : \Shmock\PHPUnitSpec

Specifies that the method will return null.

This is a shorthand for return_value(null)

Returns

\Shmock\PHPUnitSpec

return_value()

return_value(mixed|null $value) : \Shmock\PHPUnitSpec

Specifies that the method will return the given value on invocation.

 $shmock->notify()->return_value("notification!");

Parameters

mixed|null $value

The value to return on invocation

Returns

\Shmock\PHPUnitSpec

return_this()

return_this() : \Shmock\PHPUnitSpec

Specifies that the method will return the invocation target. This is useful for mocking other objects that have fluent interfaces.

 $latte->add_foam()->return_this();
 $latte->caffeine_free()->return_this();

Returns

\Shmock\PHPUnitSpec

throw_exception()

throw_exception(\Exception|void $e) : \Shmock\PHPUnitSpec

Throws an exception on invocation.

Parameters

\Exception|void $e

the exception to throw. If not specified, Shmock will provide an instance of the base \Exception.

Returns

\Shmock\PHPUnitSpec

return_consecutively()

return_consecutively(mixed[] $array_of_values, boolean|void $keep_returning_last_value) : \Shmock\PHPUnitSpec

Specifies that each subsequent invocation of this method will take the subsequent value from the array as the return value.

The sequence of values to return is not affected by ordering constraints on the mock (ie, order_matters()).

 $shmock->notify()->return_consecutively(["called!", "called again!", "called a third time!"]);

 $mock = $shmock->replay(); // replay is automatically called at the end of \Shmock\Shmock::create()

 $mock->notify(); // called!
 $mock->notify(); // called again!
 $mock->notify(); // called a third time!

Parameters

mixed[] $array_of_values

the sequence of values to return.

boolean|void $keep_returning_last_value

whether to continue returning the last element in the sequence or to fail the count expectation after every sequence element has been used. Defaults to false.

Returns

\Shmock\PHPUnitSpec

return_shmock()

return_shmock(string $class, callable $shmock_closure) : \Shmock\PHPUnitSpec

Specifies that the return value from this function will be a new mock object, which is built and replayed as soon as the invocation has occurred.

The signature of return_shmock() is similar to \Shmock\Shmock::create() except that the test case argument is omitted

 $user = \Shmock\Shmock::create($this, 'User', function ($user) {
   $user->supervisor()->return_shmock('Supervisor', function ($supervisor) {
     $supervisor->send_angry_email("I need you to work this weekend");
   });
 });

Parameters

string $class

the name of the class to mock.

callable $shmock_closure

a closure that will act as the class's build phase.

Returns

\Shmock\PHPUnitSpec