\ShmockInstanceClass

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.

Summary

Methods
Properties
Constants
__construct()
disable_original_constructor()
set_constructor_arguments()
dont_preserve_original_methods()
order_matters()
order_doesnt_matter()
replay()
shmock_class()
__call()
No public properties found
No constants found
No protected methods found
$preserve_original_methods
$disable_original_constructor
$order_matters
$constructor_arguments
N/A
No private methods found
No private properties found
N/A

Properties

$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.

Type

bool

$disable_original_constructor

$disable_original_constructor : bool

Whether or not to disable the constructor belonging to the class being mocked. By default Shmock will create a mock object that invokes the constructor.

Type

bool

$order_matters

$order_matters : bool

Type

bool — Indicates whether the order of invocations should be tracked.

$constructor_arguments

$constructor_arguments : array

Arguments that will be passed to the original constructor.

Type

array

Methods

__construct()

__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.

Parameters

\PHPUnit_Framework_TestCase $test_case
string $class

the class being mocked

disable_original_constructor()

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.

Returns

\Shmock\Instance

set_constructor_arguments()

set_constructor_arguments() : void

Any arguments passed in here will be included in the constructor call for the mocked class.

dont_preserve_original_methods()

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.

Returns

\Shmock\Instance

order_matters()

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.

Returns

\Shmock\Instance

order_doesnt_matter()

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

Returns

\Shmock\Instance

replay()

replay() : mixed

When this is invoked, Shmock concludes this instance's build phase, runs any policies that may have been registered, and creates a mock object in the replay phase.

Returns

mixed —

an instance of a subclass of the mocked class.

shmock_class()

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());
     });
  });

Parameters

callable $closure

__call()

__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:

  1. The class being mocked doesn't exist.
  2. The method being mocked doesn't exist AND there is no __call handler on the class.
  3. The method is private.
  4. The method is static. (or non-static if using a StaticClass )

Additionally, any expectations set by Shmock policies may trigger an exception when replay() is invoked.

Parameters

string $method

the method on the target class

array $with

the arguments to the mocked method

Returns

\Shmock\PHPUnitSpec

a spec that can add additional constraints to the invocation.