$preserve_original_methods
$preserve_original_methods : bool
Whether or not to preserve the original methods on the mock object or stub them out.
Default is to preserve original methods.
This class is only used when in the context of mocked instance and the shmock_class function is used.
Usage of this class is identical to Instance, except that methods that are mocked on a StaticClass must be static.
__construct(\PHPUnit_Framework_TestCase $test_case, string $class)
The Instance is active during the build phase of a mock of an instance. This object acts as a receiver for methods you wish to mock by implementing __call.
\PHPUnit_Framework_TestCase | $test_case | |
string | $class | the class being mocked |
disable_original_constructor() : \Shmock\Instance
Prevent the original constructor from being called when the replay phase begins. This can be important if the constructor of the class being mocked takes complex arguments or performs work that cannot be intercepted.
dont_preserve_original_methods() : \Shmock\Instance
When this is called, Shmock will disable any of the original implementations of methods on the mocked class. This can be useful when no expectations are set on a particular method but the original implementation cannot be called in testing.
order_matters() : \Shmock\Instance
When this is called, Shmock will begin keeping track of the order of calls made on this mock. This is implemented by using the PHPUnit at() feature and keeping an internal counter to track order.
$shmock->order_matters(); $shmock->notify('first notification'); $shmock->notify('second notification');
In this example, the string "first notification" is expected to be sent to notify first during replay. If any other string, including "second notification" is received, it will fail the expectation.
Shmock does not expose the at() feature directly.
order_doesnt_matter() : \Shmock\Instance
Disables order checking. Note that order is already disabled by default, so this does not need to be invoked unless order_matters was previously invoked
shmock_class(callable $closure) : void
When mocking an object instance, it may be desirable to mock static methods as well. Because Shmock has strict rules that mock instances may only mock instance methods, to mock a static method requires dropping into the mock class context.
This is made simple by the shmock_class() method on Instance.
// User is an ActiveRecord-style class with finders and data members // (this is just an example, this is probably not a good way to organize this code) class User { private $id; private $userName; public function updateUserName($userName) { /// persist to db $this->userName = $userName; $handle = static::dbHandle(); $handle->update(['userName' => $this->userName]); $this->fireEvent('/users/username'); } public function fireEvent($eventType) { Notifications::fire($this->id, $eventType); } public static function dbHandle() { return new DbHandle('schema.users'); } } // In a test we want to ensure that save() will fire notifications // and correctly persist to the database. $mock = $this->shmock('User', function ($user) { // ensure that the user will fire the event $user->fireEvent('/users/username')->once(); // use shmock_class to mock the static method dbHandle() $user->shmock_class(function ($user_class) { $user_class->dbHandle()->return_value(new FakeDBHandle()); }); });
callable | $closure |
__call(string $method, array $with) : \Shmock\PHPUnitSpec
Shmock intercepts all non-shmock methods here.
Shmock will fail the test if any of the following are true:
Additionally, any expectations set by Shmock policies may trigger an exception when replay() is invoked.
string | $method | the method on the target class |
array | $with | the arguments to the mocked method |
a spec that can add additional constraints to the invocation.