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

Защищенные свойства (Protected)

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

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

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

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

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

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

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

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

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

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.
См. также: 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.
Результат mixed

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

For instance: 'public static' or 'private abstract'
protected static _methodModifiers ( ReflectionMethod $method ) : string
$method ReflectionMethod
Результат string

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

For instance: 'ReflectionFunctionAbstract $method' or '$name, array $foo = null'
protected static _methodParams ( ReflectionFunctionAbstrac\ReflectionFunctionAbstract $method ) : string
$method ReflectionFunctionAbstrac\ReflectionFunctionAbstract
Результат string

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

Will generate the mocker from the current mockee.
protected static _mocker ( string $mockee ) : array
$mockee string The fully namespaced `\Mock` class
Результат array

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

Will generate the namespace from the current mockee.
protected static _namespace ( string $mockee ) : string
$mockee string The fully namespaced `\Mock` class
Результат string

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

Will return the params in a way that can be placed into compact()
protected static _stringMethodParams ( ReflectionFunctionAbstrac\ReflectionFunctionAbstract $method ) : string
$method ReflectionFunctionAbstrac\ReflectionFunctionAbstract
Результат string

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

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

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

Apply a closure to a method of the current static object.
См. также: lithium\core\StaticObject::_filter()
См. также: 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.
Результат void

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

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

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

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.
Результат object MockerChain instance

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

The main entrance to create a new Mock class.
public static create ( string $mockee ) : void
$mockee string The fully namespaced `\Mock` class
Результат void

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

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`.
Результат mixed Returns the result of the method call.

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

Will merge two sets of results into each other.
public static mergeResults ( array $results, array $secondary ) : array
$results array
$secondary array
Результат array

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

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

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

Will register this class into the autoloader.
public static register ( ) : void
Результат void

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

$_blackList защищенное статическое свойство

Some of these methods are are too custom inside the Mock or Delegate, while others should simply not be filtered.
protected static array $_blackList
Результат array

$_functionCallbacks защищенное статическое свойство

The key is the fully namespaced function name, and the value is the closure to be called.
protected static array $_functionCallbacks
Результат array

$_functionResults защищенное статическое свойство

Results of function calls for later assertion in MockerChain.
protected static array $_functionResults
Результат array

$_methodFilters защищенное статическое свойство

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

$_mockDelegateIngredients защищенное статическое свойство

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

$_mockFunctionIngredients защищенное статическое свойство

List of code to be generated for overwriting php functions.
protected static array $_mockFunctionIngredients
Результат array

$_mockIngredients защищенное статическое свойство

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