PHP Unit Testing Framework 1
A Unit testing framework for PHP.
Public Member Functions | Static Public Member Functions | Protected Member Functions

Assert Class Reference

Assertions that can be made in test cases. More...

Inheritance diagram for Assert:
TestCaseBase TestCase

Public Member Functions

 __destruct ()
 AddMessage ($message)
 Fail ($message)
 Pass ($message)
 AssertEquals ($actual, $expected, $message= '')
 AssertNotEquals ($actual, $expected, $message= '')
 AssertMatches ($actual, $pattern, $message= '')
 AssertNotMatches ($actual, $pattern, $message= '')
 ReStartOutputChecking ()
 AssertOutputEquals ($expected, $message= '')
 AssertOutputDiffers ($expected, $message= '')
 AssertOutputMatches ($pattern, $message= '')
 AssertOutputNotMatches ($pattern, $message= '')

Static Public Member Functions

static DieOnError ($errNo, $errStr, $file, $line)

Protected Member Functions

 __construct ($name, $description, $id)

Detailed Description

Assertions that can be made in test cases.

This class provides the assertions that can be performed in a test case and enables the user to add a commentary to the test report.

Definition at line 865 of file php_unit_test.php.


Constructor & Destructor Documentation

__construct ( name,
description,
id 
) [protected]

Constructs the object by setting an error handler and starting output capture.

Parameters:
[in]string$nameName for the test case.
[in]string$descriptionAnd a description for the test case.
[in]string$idTest case ID.

Reimplemented from TestCaseBase.

Reimplemented in TestCase.

Definition at line 889 of file php_unit_test.php.

__destruct ( )

Destroys the object by stopping output capture.

Definition at line 899 of file php_unit_test.php.


Member Function Documentation

AddMessage ( message)

Adds a user message to the test case results for commentary.

Parameters:
[in]string$messageThe message to add.

Definition at line 908 of file php_unit_test.php.

AssertEquals ( actual,
expected,
message = '' 
)

This function compares the two values together and the comparison is performed as follows:

  • If the two values are of different types an error is reported as they are incomparable. However the function tries to compare them if they are strings, integers, double or null to aid in debugging. You might just have the $expected as the wrong type!
  • If the two values that are either strings, integers, doubles or nulls the '==' operator is used to determine equality.
  • If the two values are resources then an error is reported as resources cannot be compared.
  • If the two values are objects and the $expected value implements the Equality interface, the Equality::Equals function is used to determine equality. Otherwise an error is reported as the two objects are incomparable.

The following code can be used to perform the implementation of the Equality interface without the need to change your implementation of the class under test:

   class MyTestClass extends ClassToBeTested implements Equality
   {
     public function Equals($obj)
     {
       // Return true of false by using ClassToBeTested public/protected functions.
     }     
   }
  • If the two values are arrays then the each of the keys from the $expected are checked in turn. If the corresponding key in $actual does not exist then the arrays are deemed to be incomparable. Otherwise the two values are retrieved from both arrays and are compared using the above description. If the two values are incomparable or not equal the scan is terminated as the result (incomparable or not equal) has been found.

After all the keys are checked and found to be equal then the two arrays are deemed to be equal.

If any of the items in the array is an array then all the values in that array are checked as described above.

After performing this comparison without error and the two values are equal the assert reports are pass or fail otherwise.

Parameters:
[in]mixed$actualThe actual value in the test.
[in]mixed$expectedThe expected value in the test.
[in]string$messageAn optional user defined message (for commentary).

Definition at line 1047 of file php_unit_test.php.

AssertMatches ( actual,
pattern,
message = '' 
)

Checks the $actual parameter matches the regular expression $pattern.

Parameters:
[in]string$actualThe string to compare with.
[in]string$patternA regular expression to use (preg).
[in]string$messageA user defined message (for commentary).

Definition at line 1164 of file php_unit_test.php.

AssertNotEquals ( actual,
expected,
message = '' 
)

This function compares the two values to determine if they are not equal as described above (see Assert::AssertEquals). If not equal the assertion has passed, otherwise it has failed unless the two values are incomparable then an error is reported.

Parameters:
[in]mixed$actualThe actual value in the test.
[in]mixed$expectedThe expected value in the test.
[in]string$messageAn optional user defined message (for commentary).

Definition at line 1107 of file php_unit_test.php.

AssertNotMatches ( actual,
pattern,
message = '' 
)

Checks the $actual parameter does not match the regular expression $pattern.

Parameters:
[in]string$actualThe string to compare with.
[in]string$patternA regular expression to use (preg).
[in]string$messageA user defined message (for commentary).

Definition at line 1191 of file php_unit_test.php.

AssertOutputDiffers ( expected,
message = '' 
)

Checks that the output from the start of the test or the last call to Assert::ReStartOutputChecking is not of the given value.

Parameters:
[in]string$expectedThe output is not expected to be this value.
[in]string$messageA user defined message (for commentary).

Definition at line 1266 of file php_unit_test.php.

AssertOutputEquals ( expected,
message = '' 
)

Checks that the output from the start of the test or the last call to Assert::ReStartOutputChecking is of the given value.

Parameters:
[in]string$expectedThe output is expected to be this value.
[in]string$messageA user defined message (for commentary).

Definition at line 1227 of file php_unit_test.php.

AssertOutputMatches ( pattern,
message = '' 
)

Checks that the output from the start of the test or the last call to Assert::ReStartOutputChecking does match the pattern.

Parameters:
[in]string$patternThe output is expected match this pattern.
[in]string$messageA user defined message (for commentary).

Definition at line 1304 of file php_unit_test.php.

AssertOutputNotMatches ( pattern,
message = '' 
)

Checks that the output from the start of the test or the last call to Assert::ReStartOutputChecking does not match the pattern.

Parameters:
[in]string$patternThe output is not expected to match this pattern.
[in]string$messageA user defined message (for commentary).

Definition at line 1333 of file php_unit_test.php.

static DieOnError ( errNo,
errStr,
file,
line 
) [static]

Call back so that errors/warnings do not go missing (due to output being captured). The program with abort.

Parameters:
[in]integer$errNoThe error number.
[in]string$errStrA string representing the error.
[in]string$fileThe file that the error occurred in.
[in]integer$lineAnd the line number.

Definition at line 877 of file php_unit_test.php.

Fail ( message)

Adds a failure message and fails the test.

Parameters:
[in]string$messageA user defined message (for commentary).

Definition at line 917 of file php_unit_test.php.

Pass ( message)

Adds a pass message to the test results.

Parameters:
[in]string$messageA user defined message (for commentary).

Definition at line 926 of file php_unit_test.php.

ReStartOutputChecking ( )

Restarts output capture so that you can compare the output from this point onwards.

Definition at line 1215 of file php_unit_test.php.


The documentation for this class was generated from the following file:
 All Data Structures Files Functions Variables

© 2011, Ed Heal