PHP Class Phockito

Mocking framework based on Mockito for Java (C) 2011 Hamish Friedlander / SilverStripe. Distributable under the same license as SilverStripe. Example usage: Create the mock $iterator = Phockito.mock('ArrayIterator); Use the mock object - doesn't do anything, functions return null $iterator->append('Test'); $iterator->asort(); Selectively verify execution Phockito::verify($iterator)->append('Test'); 1 is default - can also do 2, 3 for exact numbers, or 1+ for at least one, or 0 for never Phockito::verify($iterator, 1)->asort(); Example stubbing: Create the mock $iterator = Phockito.mock('ArrayIterator); Stub in a value Phockito::when($iterator->offsetGet(0))->return('first'); Prints "first" print_r($iterator->offsetGet(0)); Prints null, because get(999) not stubbed print_r($iterator->offsetGet(999)); Note that several functions are declared as public so that builder classes can access them. Anything starting with an "_" is for internal consumption only
Mostrar archivo Open project: hafriedlander/phockito Class Usage Examples

Public Properties

Property Type Description
$_call_list array @var array
$_defaults array Array of defaults for a given class and method
$_instanceid_counter integer Each mock instance needs a unique string ID, which we build by incrementing this counter @var int
$_is_interface array Records whether a given class is an interface, to avoid repeatedly generating reflection objects just to re-call type registrar
$_responses array .n], each item is an array of ('args' => the method args, 'responses' => stubbed responses)
$ignore_finals - If true, don't warn when doubling classes with final methods, just ignore the methods. If false, throw warnings when final methods encountered
$type_registrar - Class name of a class with a static "register_double" method that will be called with any double to inject into some other type tracking system

Public Methods

Method Description
__called ( $class, $instance, $method, $args ) Called by the mock instances when a method is called. Records the call and returns a response if one has been stubbed in
__perform_response ( $response, $args )
_arguments_match ( $mockclass, $method, $a, $b ) Checks if the two argument sets (passed as arrays) match. Simple serialized check for now, to be replaced by something that can handle anyString etc matchers later
_has_namespaces ( ) * Should we attempt to support namespaces? Is PHP >= 5.3, basically
include_hamcrest ( $include_globals = true ) Includes the Hamcrest matchers. You don't have to, but if you don't you can't to nice generic stubbing and verification
mock ( $class ) Aternative name for mock_instance
mock_class ( string $class ) : string Given a class name as a string, return a new class name as a string which acts as a mock of the passed class name. Probably not useful by itself until we start supporting static method stubbing
mock_instance ( string $class ) : Object Given a class name as a string, return a new instance which acts as a mock of that class
reset ( Phockito_Mock $mock, $method = null ) Reset a mock instance. Forget all calls and stubbed responses for a given instance
spy ( )
spy_class ( $class )
spy_instance ( $class )
verify ( Phockito_Mock $mock, string $times = 1 ) : Phockito_VerifyBuilder Verify builder. Takes a mock instance and an optional number of times to verify against. Returns a DSL object that catches the method to verify
when ( $arg = null ) : Phockito_WhenBuilder When builder. Starts stubbing the method called to build the argument passed to when

Protected Methods

Method Description
build_test_double ( boolean $partial, string $mockedClass ) : string Passed a class as a string to create the mock as, and the class as a string to mock, create the mocking class php and eval it into the current running environment

Method Details

__called() public static method

Called by the mock instances when a method is called. Records the call and returns a response if one has been stubbed in
public static __called ( $class, $instance, $method, $args )

__perform_response() public static method

public static __perform_response ( $response, $args )

_arguments_match() public static method

Checks if the two argument sets (passed as arrays) match. Simple serialized check for now, to be replaced by something that can handle anyString etc matchers later
public static _arguments_match ( $mockclass, $method, $a, $b )

_has_namespaces() public static method

* Should we attempt to support namespaces? Is PHP >= 5.3, basically
public static _has_namespaces ( )

build_test_double() protected static method

Passed a class as a string to create the mock as, and the class as a string to mock, create the mocking class php and eval it into the current running environment
protected static build_test_double ( boolean $partial, string $mockedClass ) : string
$partial boolean - Should test double be a partial or a full mock
$mockedClass string - The name of the class (or interface) to create a mock of
return string The name of the mocker class

include_hamcrest() static public method

Includes the Hamcrest matchers. You don't have to, but if you don't you can't to nice generic stubbing and verification
static public include_hamcrest ( $include_globals = true )

mock() static public method

Aternative name for mock_instance
static public mock ( $class )

mock_class() static public method

Given a class name as a string, return a new class name as a string which acts as a mock of the passed class name. Probably not useful by itself until we start supporting static method stubbing
static public mock_class ( string $class ) : string
$class string - The class to mock
return string - The class that acts as a Phockito mock of the passed class

mock_instance() static public method

Given a class name as a string, return a new instance which acts as a mock of that class
static public mock_instance ( string $class ) : Object
$class string - The class to mock
return Object - A mock of that class

reset() static public method

Reset a mock instance. Forget all calls and stubbed responses for a given instance
static public reset ( Phockito_Mock $mock, $method = null )
$mock Phockito_Mock - The mock instance to reset

spy() static public method

static public spy ( )

spy_class() static public method

static public spy_class ( $class )

spy_instance() static public method

static public spy_instance ( $class )

verify() static public method

Verify builder. Takes a mock instance and an optional number of times to verify against. Returns a DSL object that catches the method to verify
static public verify ( Phockito_Mock $mock, string $times = 1 ) : Phockito_VerifyBuilder
$mock Phockito_Mock - The mock instance to verify
$times string - The number of times the method should be called, either a number, or a number followed by "+"
return Phockito_VerifyBuilder

when() static public method

When builder. Starts stubbing the method called to build the argument passed to when
static public when ( $arg = null ) : Phockito_WhenBuilder
return Phockito_WhenBuilder

Property Details

$_call_list public_oe static_oe property

@var array
public static array $_call_list
return array

$_defaults public_oe static_oe property

Array of defaults for a given class and method
public static array $_defaults
return array

$_instanceid_counter public_oe static_oe property

Each mock instance needs a unique string ID, which we build by incrementing this counter @var int
public static int $_instanceid_counter
return integer

$_is_interface public_oe static_oe property

Records whether a given class is an interface, to avoid repeatedly generating reflection objects just to re-call type registrar
public static array $_is_interface
return array

$_responses public_oe static_oe property

.n], each item is an array of ('args' => the method args, 'responses' => stubbed responses)
public static array $_responses
return array

$ignore_finals public_oe static_oe property

- If true, don't warn when doubling classes with final methods, just ignore the methods. If false, throw warnings when final methods encountered
public static $ignore_finals

$type_registrar public_oe static_oe property

- Class name of a class with a static "register_double" method that will be called with any double to inject into some other type tracking system
public static $type_registrar