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
파일 보기 프로젝트 열기: hafriedlander/phockito 1 사용 예제들

공개 프로퍼티들

프로퍼티 타입 설명
$_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