PHP Class SimpleMock, simpletest

@package SimpleTest
Afficher le fichier Open project: simpletest/simpletest Class Usage Examples

Méthodes publiques

Méthode Description
__construct ( ) Creates an empty action list and expectation list.
atTestEnd ( string $test_method, SimpleTestCase &$test ) Receives event from unit test that the current test method has finished.
disableExpectationNameChecks ( ) Disables a name check when setting expectations.
errorAt ( integer $timing, string $method, object $error = 'A mock error', array $args = false, integer $severity = E_USER_ERROR ) Sets up a trigger to throw an error upon a specific method call.
errorOn ( string $method, object $error = 'A mock error', array $args = false, integer $severity = E_USER_ERROR ) Sets up a trigger to throw an error upon the method call.
expect ( string $method, array $args, string $message = '%s' ) Sets up an expected call with a set of expected parameters in that call.
expectAt ( integer $timing, string $method, array $args, string $message = '%s' ) Sets up an expected call with a set of expected parameters in that call.
expectAtLeastOnce ( string $method, array $args = false, string $message = '%s' ) Convenience method for requiring a method call.
expectCallCount ( string $method, integer $count, string $message = '%s' ) Sets an expectation for the number of times a method will be called.
expectMaximumCallCount ( string $method, integer $count, string $message = '%s' ) Sets the number of times a method may be called before a test failure is triggered.
expectMinimumCallCount ( string $method, integer $count, string $message = '%s' ) Sets the number of times to call a method to prevent a failure on the tally.
expectNever ( string $method, string $message = '%s' ) Convenience method for barring a method call.
expectOnce ( string $method, array $args = false, string $message = '%s' ) Convenience method for a single method call.
getCallCount ( string $method ) : integer Fetches the call count of a method so far.
invoke ( string $method, array $args ) : mixed Returns the expected value for the method name and checks expectations.
replaceWildcards ( array $args ) : array Replaces wildcard matches with wildcard expectations in the argument list.
returns ( string $method, mixed $value, array $args = false ) Sets a return for a parameter list that will be passed on by all calls to this method that match.
returnsAt ( integer $timing, string $method, mixed $value, array $args = false ) Sets a return for a parameter list that will be passed only when the required call count is reached.
returnsByReference ( string $method, mixed &$reference, array $args = false ) Sets a return for a parameter list that will be passed by reference for all calls.
returnsByReferenceAt ( integer $timing, string $method, mixed &$reference, array $args = false ) Sets a return for a parameter list that will be passed by value only when the required call count is reached.
returnsByValue ( string $method, mixed $value, array $args = false ) Sets a return for a parameter list that will be passed by value for all calls to this method.
returnsByValueAt ( integer $timing, string $method, mixed $value, array $args = false ) Sets a return for a parameter list that will be passed by value only when the required call count is reached.
throwAt ( integer $timing, string $method, object $exception = false, array $args = false ) Sets up a trigger to throw an exception upon the method call.
throwOn ( string $method, object $exception = false, array $args = false ) Sets up a trigger to throw an exception upon the method call.

Méthodes protégées

Méthode Description
addCall ( string $method, array $args ) Adds one to the call count of a method.
checkArgumentsIsArray ( mixed $args, string $task ) : boolean Die if bad arguments array is passed.
checkExpectations ( string $method, array $args, integer $timing ) Tests the arguments against expectations.
dieOnNoMethod ( string $method, string $task ) Triggers a PHP error if the method is not part of this object.
emulateCall ( string $method, array $args, integer $step ) : mixed Finds the return value matching the incoming arguments.
getCurrentTestCase ( ) : SimpeTestCase Finds currently running test.

Private Methods

Méthode Description
disableEStrict ( ) Our mock has to be able to return anything, including variable references.
restoreEStrict ( integer $was ) Restores the E_STRICT level if it was previously set.

Method Details

__construct() public méthode

All call counts are set to zero.
public __construct ( )

addCall() protected méthode

Adds one to the call count of a method.
protected addCall ( string $method, array $args )
$method string Method called.
$args array Arguments as an array.

atTestEnd() public méthode

Totals up the call counts and triggers a test assertion if a test is present for expected call counts.
public atTestEnd ( string $test_method, SimpleTestCase &$test )
$test_method string Current method name.
$test SimpleTestCase Test to send message to.

checkArgumentsIsArray() protected méthode

Die if bad arguments array is passed.
protected checkArgumentsIsArray ( mixed $args, string $task ) : boolean
$args mixed The arguments value to be checked.
$task string Description of task attempt.
Résultat boolean Valid arguments

checkExpectations() protected méthode

Tests the arguments against expectations.
protected checkExpectations ( string $method, array $args, integer $timing )
$method string Method to check.
$args array Argument list to match.
$timing integer The position of this call in the call history.

dieOnNoMethod() protected méthode

Triggers a PHP error if the method is not part of this object.
protected dieOnNoMethod ( string $method, string $task )
$method string Name of method.
$task string Description of task attempt.

disableExpectationNameChecks() public méthode

This hack is needed for the partial mocks.

emulateCall() protected méthode

If there is no matching value found, then an error is triggered.
protected emulateCall ( string $method, array $args, integer $step ) : mixed
$method string Method name.
$args array Calling arguments.
$step integer Current position in the call history.
Résultat mixed Stored return or other action.

errorAt() public méthode

Sets up a trigger to throw an error upon a specific method call.
public errorAt ( integer $timing, string $method, object $error = 'A mock error', array $args = false, integer $severity = E_USER_ERROR )
$timing integer When to throw the exception. A value of 0 throws immediately. A value of 1 actually allows one call to this method before throwing. 2 will allow two calls before throwing and so on.
$method string Method name to throw on.
$error object Error message to trigger.
$args array Optional argument list filter. If given then the exception will only be thrown if the method call matches the arguments.
$severity integer The PHP severity level. Defaults to E_USER_ERROR.

errorOn() public méthode

Sets up a trigger to throw an error upon the method call.
public errorOn ( string $method, object $error = 'A mock error', array $args = false, integer $severity = E_USER_ERROR )
$method string Method name to throw on.
$error object Error message to trigger.
$args array Optional argument list filter. If given then the exception will only be thrown if the method call matches the arguments.
$severity integer The PHP severity level. Defaults to E_USER_ERROR.

expect() public méthode

All calls will be compared to these expectations regardless of when the call is made.
public expect ( string $method, array $args, string $message = '%s' )
$method string Method call to test.
$args array Expected parameters for the call including wildcards.
$message string Overridden message.

expectAt() public méthode

The expected call count will be adjusted if it is set too low to reach this call.
public expectAt ( integer $timing, string $method, array $args, string $message = '%s' )
$timing integer Number of calls in the future at which to test. Next call is 0.
$method string Method call to test.
$args array Expected parameters for the call including wildcards.
$message string Overridden message.

expectAtLeastOnce() public méthode

Convenience method for requiring a method call.
public expectAtLeastOnce ( string $method, array $args = false, string $message = '%s' )
$method string Method call to track.
$args array Expected argument list or false for any arguments.
$message string Overridden message.

expectCallCount() public méthode

The tally method is used to check this.
public expectCallCount ( string $method, integer $count, string $message = '%s' )
$method string Method call to test.
$count integer Number of times it should have been called at tally.
$message string Overridden message.

expectMaximumCallCount() public méthode

Sets the number of times a method may be called before a test failure is triggered.
public expectMaximumCallCount ( string $method, integer $count, string $message = '%s' )
$method string Method call to test.
$count integer Most number of times it should have been called.
$message string Overridden message.

expectMinimumCallCount() public méthode

Sets the number of times to call a method to prevent a failure on the tally.
public expectMinimumCallCount ( string $method, integer $count, string $message = '%s' )
$method string Method call to test.
$count integer Least number of times it should have been called.
$message string Overridden message.

expectNever() public méthode

Convenience method for barring a method call.
public expectNever ( string $method, string $message = '%s' )
$method string Method call to ban.
$message string Overridden message.

expectOnce() public méthode

Convenience method for a single method call.
public expectOnce ( string $method, array $args = false, string $message = '%s' )
$method string Method call to track.
$args array Expected argument list or false for any arguments.
$message string Overridden message.

getCallCount() public méthode

Fetches the call count of a method so far.
public getCallCount ( string $method ) : integer
$method string Method name called.
Résultat integer Number of calls so far.

getCurrentTestCase() protected méthode

Finds currently running test.
protected getCurrentTestCase ( ) : SimpeTestCase
Résultat SimpeTestCase Current test case.

invoke() public méthode

Will generate anytest assertions as a result of expectations if there is a test present.
public invoke ( string $method, array $args ) : mixed
$method string Name of method to simulate.
$args array Arguments as an array.
Résultat mixed Stored return.

replaceWildcards() public méthode

Replaces wildcard matches with wildcard expectations in the argument list.
public replaceWildcards ( array $args ) : array
$args array Raw argument list.
Résultat array Argument list with expectations.

returns() public méthode

Sets a return for a parameter list that will be passed on by all calls to this method that match.
public returns ( string $method, mixed $value, array $args = false )
$method string Method name.
$value mixed Result of call by value/handle.
$args array List of parameters to match including wildcards.

returnsAt() public méthode

Sets a return for a parameter list that will be passed only when the required call count is reached.
public returnsAt ( integer $timing, string $method, mixed $value, array $args = false )
$timing integer Number of calls in the future to which the result applies. If not set then all calls will return the value.
$method string Method name.
$value mixed Result of call passed.
$args array List of parameters to match including wildcards.

returnsByReference() public méthode

Sets a return for a parameter list that will be passed by reference for all calls.
public returnsByReference ( string $method, mixed &$reference, array $args = false )
$method string Method name.
$reference mixed Result of the call will be this object.
$args array List of parameters to match including wildcards.

returnsByReferenceAt() public méthode

Sets a return for a parameter list that will be passed by value only when the required call count is reached.
public returnsByReferenceAt ( integer $timing, string $method, mixed &$reference, array $args = false )
$timing integer Number of calls in the future to which the result applies. If not set then all calls will return the value.
$method string Method name.
$reference mixed Result of the call will be this object.
$args array List of parameters to match including wildcards.

returnsByValue() public méthode

Sets a return for a parameter list that will be passed by value for all calls to this method.
public returnsByValue ( string $method, mixed $value, array $args = false )
$method string Method name.
$value mixed Result of call passed by value.
$args array List of parameters to match including wildcards.

returnsByValueAt() public méthode

Sets a return for a parameter list that will be passed by value only when the required call count is reached.
public returnsByValueAt ( integer $timing, string $method, mixed $value, array $args = false )
$timing integer Number of calls in the future to which the result applies. If not set then all calls will return the value.
$method string Method name.
$value mixed Result of call passed by value.
$args array List of parameters to match including wildcards.

throwAt() public méthode

Sets up a trigger to throw an exception upon the method call.
public throwAt ( integer $timing, string $method, object $exception = false, array $args = false )
$timing integer When to throw the exception. A value of 0 throws immediately. A value of 1 actually allows one call to this method before throwing. 2 will allow two calls before throwing and so on.
$method string Method name to throw on.
$exception object Exception object to throw. If not given then a simple Exception object is thrown.
$args array Optional argument list filter. If given then the exception will only be thrown if the method call matches the arguments.

throwOn() public méthode

Sets up a trigger to throw an exception upon the method call.
public throwOn ( string $method, object $exception = false, array $args = false )
$method string Method name to throw on.
$exception object Exception object to throw. If not given then a simple Exception object is thrown.
$args array Optional argument list filter. If given then the exception will only be thrown if the method call matches the arguments.