PHP Class lithium\test\Mocker

To enable the autoloading of mocks you simply need to make a simple method call. {{{ use lithium\core\Environment; use lithium\test\Mocker; if (!Environment::is('production')) { Mocker::register(); } }}} You can also enable autoloading inside the setup of a unit test class. This method can be called redundantly. {{{ use lithium\test\Mocker; class MockerTest extends \lithium\test\Unit { public function setUp() { Mocker::register(); } } }}} Using Mocker is the fun magical part, it's autoloaded so simply call the class you want to mock with the '\Mock' at the end. The autoloader will detect you want to autoload it, and create it for you. Now you can filter any method. {{{ use lithium\console\dispatcher\Mock as DispatcherMock; $dispatcher = new DispatcherMock(); $dispatcher->applyFilter('config', function($self, $params, $chain) { return array(); }); $results = $dispatcher->config(); }}} {{{ use lithium\analysis\parser\Mock as ParserMock; $code = 'echo "foobar";'; ParserMock::applyFilter('config', function($self, $params, $chain) { return array(); }); $tokens = ParserMock::tokenize($code, array('wrap' => true)); }}}
Mostrar archivo Open project: unionofrad/lithium Class Usage Examples

Protected Properties

Property Type Description
$_blackList array Some of these methods are are too custom inside the Mock or Delegate, while others should simply not be filtered.
$_functionCallbacks array The key is the fully namespaced function name, and the value is the closure to be called.
$_functionResults array Results of function calls for later assertion in MockerChain.
$_methodFilters Stores the closures that represent the method filters. They are indexed by called class.
$_mockDelegateIngredients array The Delegate directly extends the class you wish to mock and makes all methods publically available to other classes but should not be accessed directly by any other classes other than Mock.
$_mockFunctionIngredients array List of code to be generated for overwriting php functions.
$_mockIngredients array The Mock class directly extends the class you wish to mock but only interacts with the Delegate directly. This class is the public interface for users.

Public Methods

Method Description
applyFilter ( string $class, mixed $method = null, Closure $filter = null ) : void Apply a closure to a method of the current static object.
callFunction ( string $name, array &$params = [] ) : mixed A method to call user defined functions.
chain ( mixed $mock ) : object Generate a chain class with the current rules of the mock.
create ( string $mockee ) : void The main entrance to create a new Mock class.
invokeMethod ( string $method, array $params = [] ) : mixed Calls a method on this object with the given parameters. Provides an OO wrapper for forward_static_call_array().
mergeResults ( array $results, array $secondary ) : array Will merge two sets of results into each other.
overwriteFunction ( string | boolean $name, closure | boolean $callback = null ) : void Will overwrite namespaced functions.
register ( ) : void Will register this class into the autoloader.

Protected Methods

Method Description
_dynamicCode ( string $type, string $key, array $tokens = [] ) : string Will generate the code you are wanting.
_filter ( string $class, string | array $method, array $params, Closure $callback, array $filters = [] ) : mixed Executes a set of filters against a method by taking a method's main implementation as a callback, and iteratively wrapping the filters around it.
_methodModifiers ( ReflectionMethod $method ) : string Will determine what method mofifiers of a method.
_methodParams ( ReflectionFunctionAbstrac\ReflectionFunctionAbstract $method ) : string Will determine what parameter prototype of a method.
_mocker ( string $mockee ) : array Will generate the mocker from the current mockee.
_namespace ( string $mockee ) : string Will generate the namespace from the current mockee.
_stringMethodParams ( ReflectionFunctionAbstrac\ReflectionFunctionAbstract $method ) : string Will return the params in a way that can be placed into compact()
_validateMockee ( string $mockee ) : boolean Will validate if mockee is a valid class we should mock.

Method Details

_dynamicCode() protected static method

This pulls from $_mockDelegateIngredients and $_mockIngredients.
protected static _dynamicCode ( string $type, string $key, array $tokens = [] ) : string
$type string The name of the array of ingredients to use
$key string The key from the array of ingredients
$tokens array Tokens, if any, that should be inserted
return string

_filter() protected static method

Executes a set of filters against a method by taking a method's main implementation as a callback, and iteratively wrapping the filters around it.
See also: lithium\util\collection\Filters
protected static _filter ( string $class, string | array $method, array $params, Closure $callback, array $filters = [] ) : mixed
$class string Fully namespaced class to apply filters.
$method string | array The name of the method being executed, or an array containing the name of the class that defined the method, and the method name.
$params array An associative array containing all the parameters passed into the method.
$callback Closure The method's implementation, wrapped in a closure.
$filters array Additional filters to apply to the method for this call only.
return mixed

_methodModifiers() protected static method

For instance: 'public static' or 'private abstract'
protected static _methodModifiers ( ReflectionMethod $method ) : string
$method ReflectionMethod
return string

_methodParams() protected static method

For instance: 'ReflectionFunctionAbstract $method' or '$name, array $foo = null'
protected static _methodParams ( ReflectionFunctionAbstrac\ReflectionFunctionAbstract $method ) : string
$method ReflectionFunctionAbstrac\ReflectionFunctionAbstract
return string

_mocker() protected static method

Will generate the mocker from the current mockee.
protected static _mocker ( string $mockee ) : array
$mockee string The fully namespaced `\Mock` class
return array

_namespace() protected static method

Will generate the namespace from the current mockee.
protected static _namespace ( string $mockee ) : string
$mockee string The fully namespaced `\Mock` class
return string

_stringMethodParams() protected static method

Will return the params in a way that can be placed into compact()
protected static _stringMethodParams ( ReflectionFunctionAbstrac\ReflectionFunctionAbstract $method ) : string
$method ReflectionFunctionAbstrac\ReflectionFunctionAbstract
return string

_validateMockee() protected static method

Will fail if the mock already exists, or it doesn't contain \Mock in the namespace.
protected static _validateMockee ( string $mockee ) : boolean
$mockee string The fully namespaced `\Mock` class
return boolean

applyFilter() public static method

Apply a closure to a method of the current static object.
See also: lithium\core\StaticObject::_filter()
See also: lithium\util\collection\Filters
public static applyFilter ( string $class, mixed $method = null, Closure $filter = null ) : void
$class string Fully namespaced class to apply filters.
$method mixed The name of the method to apply the closure to. Can either be a single method name as a string, or an array of method names. Can also be false to remove all filters on the current object.
$filter Closure The closure that is used to filter the method(s), can also be false to remove all the current filters for the given method.
return void

callFunction() public static method

This method should only be accessed by functions created by Mocker::overwriteFunction(). If no matching stored function exists, the global function will be called instead.
public static callFunction ( string $name, array &$params = [] ) : mixed
$name string Fully namespaced function name to call.
$params array Params to be passed to the function.
return mixed

chain() public static method

Generate a chain class with the current rules of the mock.
public static chain ( mixed $mock ) : object
$mock mixed Mock object, namespaced static mock, namespaced function name.
return object MockerChain instance

create() public static method

The main entrance to create a new Mock class.
public static create ( string $mockee ) : void
$mockee string The fully namespaced `\Mock` class
return void

invokeMethod() public static method

Calls a method on this object with the given parameters. Provides an OO wrapper for forward_static_call_array().
public static invokeMethod ( string $method, array $params = [] ) : mixed
$method string Name of the method to call.
$params array Parameter list to use when calling `$method`.
return mixed Returns the result of the method call.

mergeResults() public static method

Will merge two sets of results into each other.
public static mergeResults ( array $results, array $secondary ) : array
$results array
$secondary array
return array

overwriteFunction() public static method

Will overwrite namespaced functions.
public static overwriteFunction ( string | boolean $name, closure | boolean $callback = null ) : void
$name string | boolean Fully namespaced function, or `false` to reset functions.
$callback closure | boolean Callback to be called, or `false` to reset this function.
return void

register() public static method

Will register this class into the autoloader.
public static register ( ) : void
return void

Property Details

$_blackList protected_oe static_oe property

Some of these methods are are too custom inside the Mock or Delegate, while others should simply not be filtered.
protected static array $_blackList
return array

$_functionCallbacks protected_oe static_oe property

The key is the fully namespaced function name, and the value is the closure to be called.
protected static array $_functionCallbacks
return array

$_functionResults protected_oe static_oe property

Results of function calls for later assertion in MockerChain.
protected static array $_functionResults
return array

$_methodFilters protected_oe static_oe property

Stores the closures that represent the method filters. They are indexed by called class.
protected static $_methodFilters

$_mockDelegateIngredients protected_oe static_oe property

The Delegate directly extends the class you wish to mock and makes all methods publically available to other classes but should not be accessed directly by any other classes other than Mock.
protected static array $_mockDelegateIngredients
return array

$_mockFunctionIngredients protected_oe static_oe property

List of code to be generated for overwriting php functions.
protected static array $_mockFunctionIngredients
return array

$_mockIngredients protected_oe static_oe property

The Mock class directly extends the class you wish to mock but only interacts with the Delegate directly. This class is the public interface for users.
protected static array $_mockIngredients
return array