A custom PHP TDD Framework. MIT Licensed.
- Version Number:
v3.1.0
- Status: Stable - Production Ready
- License: MIT License
PHPTester currently consists of three major components:
- Interfaces - these provide you with the correct guidelines to implement a custom PHP testing framework shall you want to implement your own (instead of directly using the PHP testing framework provided in this software) and should be extendable
- The
PHPTesterException
class - this is used internally by the main class to generate more human-readable error messages in some cases. It is extendable and can be used in your own code as well (shall you wish to do so). - The main class where everything is implemented -
PHPTester
. Can be extended using theextends
keyword.
Please note that the Documentation is only meant as a simple, brief guide on the methods available in the PHPTester
testing framework and how to use them. In case of doubt, please refer to the demos (in the demo
folder) and if you still don't understand how a particular method works, feel free to contact me.
Class: PHPTester
A string specifying the current version of PHPTester
being used, e.g.
echo PHPTester::VERSION; // v3.1.0
Interface: PHPTesterCore
Used to group a set of test cases to be executed.
Argument Order: $msg
, $fn
Argument Description:
$msg
- Required. Specifies the message to be displayed to the screen which describes the set of tests to be executed.$fn
- Required. A function containing the set of test cases to be executed.
Used to group a subset of test cases to be executed. Must be nested within the describe
method.
Argument Order: $msg
, $fn
Argument Description:
$msg
- Required. Specifies the message to be displayed to the screen which describes the subset of tests to be executed.$fn
- Required. A function containing the subset of test cases to be executed.
The core assertion method in which all other assertion methods are built on (in the main PHP testing framework class). If you plan to implement your own testing framework, you are strongly advised to use this assertion method as a base assertion method.
Argument Order: $passed
, $msg
, $success
Argument Description:
$passed
- Required. Ideally a value of typebool
. If the value is truthy (ortrue
) then the test is passed, else it fails.$msg
- Optional. The message to be displayed if the test fails. However, it is a best practice to provide this argument to aid debugging.$success
- Optional. The message to be displayed upon a successful test. This argument is not required and is usually used internally in other assertion methods.
A basic assertion method that compares two primitive values and passes the test if the two are equal.
NOTE: The inverse assertion of assert_equals
is assert_not_equals
and takes exactly the same arguments in the same order.
Argument Order: $actual
, $expected
, $msg
, $success
Argument Description:
$actual
- Required. Must be a primitive value (e.g.bool
,int
,float
,string
) and must be the value returned by the algorithm being tested, NOT the expected return value. The test is successful if this is equal to the expected return value.$expected
- Required. Must be a primitive value (e.g.bool
,int
,float
,string
) and must be the expected return value, NOT the value returned by the algorithm being tested. The test is successful if the actual return value is equal to this.$msg
- Optional. The message to be shown in case of a failed test. Best practice is to provide it to aid debugging.$success
- Optional. The message to be shown when the test is successful. Not required; usually used internally by other methods.
Interface: PHPTesterErrorAssertions
Runs a function containing the code to be tested and catches any errors thrown in the process. The test is successful if an Error
/ErrorException
/Exception
is thrown and fails otherwise.
NOTE: expect_no_error
is the inverse assertion of this method and takes exactly the same arguments in the same order.
Argument Order: $msg
, $fn
Argument Description:
$msg
- Required. The message to be displayed in case of a failed test.$fn
- Required. The function containing the code to be tested.
Interface: PHPTesterArrayAssertions
Compares two arrays and checks if they are the same recursively. The test is successful if the two arrays are "the same" (in terms of structure) and fails otherwise.
NOTE: The inverse of this assertion is assert_not_similar
and takes exactly the same arguments in the same order.
Argument Order: $actual
, $expected
, $msg
, $success
Argument Description:
$actual
- Required. The actual array returned by the code to be tested.$expected
- Required. The expected array structure (to be returned by the code when it is functioning properly).$msg
- Optional. The message displayed upon failure. Best practice is to provide it to aid debugging.$success
- Optional. The success message when a test is passed. Not required and usually used internally.
Interface: PHPTesterNumberAssertions
Compares two number values (int
or float
) to see if they are approximately equal to each other. The test is successful if they are almost equal and fails otherwise.
NOTE: The inverse of this assertion is assert_not_fuzzy_equals
and takes the exact same arguments in the same order.
Argument Order: $actual
, $expected
, $range
, $msg
, $success
Argument Description:
$actual
- Required. Anint
orfloat
value returned from the algorithm to be tested.$expected
- Required. Anint
orfloat
value which is the expected (approximate) answer.$range
- Optional. A tinyfloat
value that specifies the maximum error as a fraction of the expected value. It is1e-12
by default (which will tolerate almost exact answers) but other suggested values include1e-9
(pretty accurate) and1e-6
(close-ish value).$msg
- Optional. The message displayed upon failure. Best practice is to provide it to aid debugging.$success
- Optional. The success message when a test is passed. Not required and usually used internally.
Interface: PHPTesterPerformanceAssertions
Executes a block of code and times how long it takes (in milliseconds) to execute that code. The test is then passed if the execution duration is shorter than the specified duration (in milliseconds) and fails otherwise.
NOTE: The inverse of this assertion is assert_min_execution_time
and takes the exact same arguments in the exact same order.
Argument Order: $fn
, $ms
, $msg
, $success
Argument Description:
$fn
- Required. The block of code to be executed and timed (in the form of a function).$ms
- Required. The maximum acceptable execution duration in milliseconds.$msg
- Optional. The message displayed upon failure. Best practice is to provide it to aid debugging.$success
- Optional. The success message when a test is passed. Not required and usually used internally.
Interface: PHPTesterRandomTesting
Note: The random testing methods are best used in conjunction with a confirmed working algorithm/solution (as the source of validation) when using the testing framework to test others' code to prevent hard-coded (cheat) solutions.
Returns a random integer in a specified range.
Argument Order: $min
, $max
Argument Description:
$min
- Optional. The minimum possible integer value that can be returned by the method. Defaults to0
if not specified.$max
- Optional. The maximum possible integer value that can be returned by the method. Defaults to100
if not specified.
Returns a randomly generated string of a specified length, containing only lowercase alphabet letters and/or digits.
Argument Order: $length
Argument Description:
$length
- Optional. Specifies the length of the string to be randomly generated.
Receives a non-associative array as its only argument and returns a new array with the order of the elements shuffled.
Argument Order: $array
Argument Description:
$array
- Required. The non-associative array to be shuffled and returned. Please note that this method is clean which means that it does not mutate the original array; rather, it returns a new array with the order of the elements shuffled.
A note regarding implementing your own custom PHP testing framework by implementing the interfaces provided in this software
If you are a senior PHP developer wanting to implement your own PHP testing framework by implementing certain interfaces predefined in this software, it is highly recommended that you implement PHPTesterCore
before implementing any other interfaces. Please also note that if your assert_equals
method already handles arrays properly, it is then not required to implement PHPTesterArrayAssertions
.
Folder: demo
basic.php
- A basic demo regarding how to use the core PHPTester methodserrors.php
- A simple introduction to the error-handling methods in PHPTester and when/how to use themarrays.php
- A simple introduction to array assertion methods in PHPTesternumbers.php
- A simple introduction to number assertions in PHPTesterperformance.php
- A simple introduction to performance assertions in PHPTesterrandom.php
- A simple introduction to random testing methods in PHPTester