test.php is an included class designed for unit testing
back >
app >
lib >
tests.php
an example test is included when PAX is installed at
front >
html >
pages >
test.html
The run method receives an associative array that outlines the classes and methods that you want to run specific tests.
$app->get('tests')->run([
'class_name'=>[
'method_name1'=>[
'func'=>'is_string',
'func_vars'=>['_val'],
'message'=>'Must return an array'
],
'method_name2'=>[
'message'=>'Must return 1 or 0',
'and'=>[['==',0]],
]
]
]);
Name | Type | Example | Description |
---|---|---|---|
message | String | "Must be greater than 0" | Test description |
vars | Array | ["users",22] | Method Variables |
and | Array | ["and"=>[ ['>',3], ['<',10] ]] | If statements (and) to test returned value |
or | Array | ["or"=>[ ['>',3], ['>',4] ]] | If statements (or) to test returned value |
func | String | "array_key_exists" | Method to test returned value |
func_vars | Array | ["key","_val"] | Method variables |
skip | Integer | 1 | Skip test |
Below is the class that we will be testing. This has 4 basic methods that return a string, number, array, and associative array.
class modelExample {
public function __construct() {}
public function getString($get=[],$post=[]) {
return "Class Called";
}
public function getNumber($get=[],$post=[]) {
return 22;
}
public function getArray($get=[],$post=[]) {
return [1,2,3,4];
}
public function getAssoc($get=[],$post=[]) {
return ["url"=>"test.com","format"=>"JSON","success"=>true];
}
}
Below is the code to run the test. We get the tests class, and specify tests for all 4 methods
$app->get('tests')->run([
'modelExample'=>[
'getString'=>[
'func'=>'is_string',
'func_vars'=>['_val'],
'message'=>'Must return an array'
],
'getNumber'=>[
'message'=>'Must return 1 or 0',
'and'=>[['==',0]],
],
'getArray'=>[
'func'=>'is_array',
'func_vars'=>['_val'],
'message'=>'Must return an array'
],
'getAssoc'=>[
'func'=>'is_array',
'func_vars'=>['_val'],
'message'=>'Must return an array'
]
]
]);
//render test results
echo $app->get('tests')->html;