PHP Класс 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
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$_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

Открытые методы

Метод Описание
__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

Защищенные методы

Метод Описание
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

Описание методов

__called() публичный статический Метод

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 __perform_response ( $response, $args )

_arguments_match() публичный статический Метод

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() публичный статический Метод

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

build_test_double() защищенный статический Метод

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
Результат string The name of the mocker class

include_hamcrest() статический публичный Метод

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() статический публичный Метод

Aternative name for mock_instance
static public mock ( $class )

mock_class() статический публичный Метод

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
Результат string - The class that acts as a Phockito mock of the passed class

mock_instance() статический публичный Метод

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
Результат Object - A mock of that class

reset() статический публичный Метод

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 spy ( )

spy_class() статический публичный Метод

static public spy_class ( $class )

spy_instance() статический публичный Метод

static public spy_instance ( $class )

verify() статический публичный Метод

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 "+"
Результат Phockito_VerifyBuilder

when() статический публичный Метод

When builder. Starts stubbing the method called to build the argument passed to when
static public when ( $arg = null ) : Phockito_WhenBuilder
Результат Phockito_WhenBuilder

Описание свойств

$_call_list публичное статическое свойство

@var array
public static array $_call_list
Результат array

$_defaults публичное статическое свойство

Array of defaults for a given class and method
public static array $_defaults
Результат array

$_instanceid_counter публичное статическое свойство

Each mock instance needs a unique string ID, which we build by incrementing this counter @var int
public static int $_instanceid_counter
Результат integer

$_is_interface публичное статическое свойство

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
Результат array

$_responses публичное статическое свойство

.n], each item is an array of ('args' => the method args, 'responses' => stubbed responses)
public static array $_responses
Результат array

$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
public static $ignore_finals

$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 static $type_registrar