1
1
<?php
2
+ // FYI .. as is api_class_template passes, even if the curl can't connect to the provided address.
2
3
require_once ('simpletest/autorun.php ' );
3
4
// load baseclass
4
5
require_once ('../APIBaseClass.php ' );
@@ -10,43 +11,80 @@ class TestOfApiClass extends UnitTestCase {
10
11
function testApiConstructs (){
11
12
$ this ->api = new yourApi ();
12
13
// obviously will return false because these are private parameters
14
+ // not sure if these test are useful for the base class.. but they check to see
15
+ // that they are private, and the api_url is available from the called class
13
16
$ this ->check_class_params ('_http _root ' ,false );
14
- $ this ->check_class_params ('url ' ,true );
17
+ $ this ->check_class_params ('api_url ' ,true );
18
+ }
19
+
20
+ function testApiDoQueryObjectType (){
21
+ $ method_name = 'do_query ' ;
22
+ /* tests do query against invalid function syntax calls with arrays, numbers and strings
23
+ do query cannot take anything other than three passed strings should return false
24
+ playing around with different invalid DoQuery requests. This is hard to abstract
25
+ until I figure out how to dynamically add arguments to the method call syntax
26
+ */
27
+
28
+ $ this ->assertFalse ($ this ->api ->$ method_name (1 ,' ' ,array ()));
29
+ // pass three blank arrays
30
+ $ this ->assertFalse ($ this ->api ->$ method_name (array (),array (),array ()));
31
+ // pass three numbers
32
+ $ this ->assertFalse ($ this ->api ->$ method_name (0 ,1 ,0 ));
33
+ $ this ->assertFalse ($ this ->api ->$ method_name (0 ,1 ,array ('value ' )));
34
+ // pass one number
35
+ $ this ->assertFalse ($ this ->api ->$ method_name (' ' ,' ' ,' ' ));
15
36
}
16
- // public function do_query($query_path,$params,$return_param)
17
37
18
- function testApi_do_query ($ mode =TRUE ){
19
- // load a file that can do the tests automagically so we don't have to write them in each time
20
- // inlcude('do_query_conf.php') use a static associtiave array
21
- $ query_path = $ this ->api ->url ;
22
- $ params = array ('param1 ' =>'value1 ' );
23
- $ return_param = 'param1 ' ;
24
- // check for do query config file
25
- if (is_file ('do_query_conf.php ' ) && !class_exists ('do_query_conf ' )){
26
- require ('do_query_conf.php ' );
27
- }
28
- // check for configuration class to do automated testing
38
+ function testApiDoQuery ($ query_path =NULL ,$ params =NULL ,$ return_param =NULL ,$ mode =FALSE ){
39
+ /* this is like above but we test valid method parameters, or invalid method parameters if mode is FALSE
40
+ $query_path is the api URL
41
+ $params is a coded string written as such 'param<=>value param2<=>value2' and so on. Take care to not use spaces except between
42
+ parameter/value pairs
43
+ $return_param is the name of a parameter to return (as per the original do_query function call), this parameter must be in the $params variable
44
+
45
+ or
46
+
47
+ leave the method call blank and let a file called 'do_query_conf.php' be included. This file contains a class with a static variable
48
+ that can be accessed inside and outside of objects without any inheritance, object creation or global definitions.
49
+
50
+ $mode is an optional value that will allow a developer to toggle between 'assertFalse' and 'assertTrue', take care to define all variables
51
+ before $mode with their default 'nulls'
52
+
53
+ check for configuration class file to do automated testing perhaps make this a static public var that dev can set at top of class
54
+
55
+ that class will look like class do_query_conf{ static $_ = array( 'do query method call1','do query method call2') and so on.
56
+ Basically $params in an array
57
+
58
+ */
59
+
60
+ if (!class_exists ('do_query_conf ' ) && pathinfo ('do_query_conf.php ' )) include ('do_query_conf.php ' );
29
61
if (class_exists ('do_query_conf ' )){
30
62
foreach (do_query_conf::$ _ as $ item )
31
63
$ query = explode (' ' , $ item );
32
64
if (count ($ query ) == 3 ){
33
65
if ($ mode == false )
34
- $ this ->assertFalse ($ query [0 ],$ query [1 ],$ query [2 ]);
66
+ $ this ->assertFalse ($ this -> api -> do_query ( $ query [0 ],$ query [1 ],$ query [2 ]) );
35
67
else
36
- $ this ->assertTrue ($ query [0 ],$ query [1 ],$ query [2 ]);
68
+ $ this ->assertTrue ($ this -> api -> do_query ( $ query [0 ],$ query [1 ],$ query [2 ]) );
37
69
}
38
70
else {
39
71
// cause a fail test if your conf file isn't written properly
40
72
if ($ mode == false )
41
73
$ this ->assertFalse (true );
42
- else
74
+ elseif ( $ mode == true )
43
75
$ this ->assertTrue (false );
44
76
}
45
- }else
46
- if ($ mode == false )
77
+ // if no config file present
78
+ }elseif (is_string ($ query_path )&&is_string ($ params )&&is_bool ($ mode )){
79
+ if ($ mode == false )
80
+ // may need to use something other than assertFalse/assertTrue to be more precise
47
81
$ this ->assertFalse ($ this ->api ->do_query ($ query_path ,$ params ,$ return_param ));
48
- else
82
+ elseif ( $ mode == true )
49
83
$ this ->assertTrue ($ this ->api ->do_query ($ query_path ,$ params ,$ return_param ));
84
+ }else {
85
+ // conf file not found, and testDoQuery was not passed valid syntax
86
+ return false ;
87
+ }
50
88
}
51
89
52
90
function check_class_params ($ params =NULL ,$ mode =TRUE ){
@@ -61,12 +99,14 @@ function check_class_params($params=NULL,$mode=TRUE){
61
99
$ api_vars = array_intersect_key ($ api_class_vars ,$ api_vars );
62
100
}
63
101
else
64
- $ api_vars = $ api_class_vars ;
102
+ $ api_vars = $ api_class_vars ;
103
+ // anything that isnt intersected should return false
104
+
65
105
foreach ($ api_vars as $ key =>$ value ){
66
106
if ($ mode == TRUE )
67
- $ this ->assertTrue ($ this -> api -> $ key );
107
+ $ this ->assertTrue (array_key_exists ( $ key, $ api_class_vars ) );
68
108
elseif ($ mode == FALSE )
69
- $ this ->assertFalse ($ this -> api -> $ key );
109
+ $ this ->assertFalse (array_key_exists ( $ key, $ api_class_vars ) );
70
110
}
71
111
}
72
112
}
0 commit comments