Skip to content

Commit 529fc62

Browse files
committed
modified do query to gracefully return false if a method call is improperly made by checking the variable types, modified TestOfApiClass and added documentation on usage and automated testing; wrote a few tests
1 parent 06b7cad commit 529fc62

File tree

2 files changed

+65
-22
lines changed

2 files changed

+65
-22
lines changed

APIBaseClass.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ private function _apiHelper($path, $params)
6868

6969
public function do_query($query_path,$params,$return_param)
7070
{
71+
// do some syntax cleanup if improperly written method call is made.
72+
if(!is_string($query_path) || (!is_string($query_path) && !is_array($params) || (!is_string($return_param)))) return false;
73+
7174
// query path is location to api query, params is either a string (if only one param) or an
7275
// associtative array, $return_param is the name of the parameter to lookfor and display...
7376
if(!is_array($params)){

testing/TestOfApiClass.php

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
// FYI .. as is api_class_template passes, even if the curl can't connect to the provided address.
23
require_once('simpletest/autorun.php');
34
// load baseclass
45
require_once('../APIBaseClass.php');
@@ -10,43 +11,80 @@ class TestOfApiClass extends UnitTestCase {
1011
function testApiConstructs(){
1112
$this->api = new yourApi();
1213
// 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
1316
$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(' ',' ',' '));
1536
}
16-
// public function do_query($query_path,$params,$return_param)
1737

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');
2961
if(class_exists('do_query_conf')){
3062
foreach(do_query_conf::$_ as $item)
3163
$query = explode (' ', $item);
3264
if(count($query) == 3){
3365
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]));
3567
else
36-
$this->assertTrue($query[0],$query[1],$query[2]);
68+
$this->assertTrue($this->api->do_query($query[0],$query[1],$query[2]));
3769
}
3870
else{
3971
// cause a fail test if your conf file isn't written properly
4072
if($mode == false)
4173
$this->assertFalse(true);
42-
else
74+
elseif($mode == true)
4375
$this->assertTrue(false);
4476
}
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
4781
$this->assertFalse($this->api->do_query($query_path,$params,$return_param));
48-
else
82+
elseif($mode == true)
4983
$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+
}
5088
}
5189

5290
function check_class_params($params=NULL,$mode=TRUE){
@@ -61,12 +99,14 @@ function check_class_params($params=NULL,$mode=TRUE){
6199
$api_vars = array_intersect_key($api_class_vars,$api_vars);
62100
}
63101
else
64-
$api_vars = $api_class_vars;
102+
$api_vars = $api_class_vars;
103+
// anything that isnt intersected should return false
104+
65105
foreach($api_vars as $key=>$value){
66106
if($mode == TRUE)
67-
$this->assertTrue($this->api->$key);
107+
$this->assertTrue(array_key_exists($key,$api_class_vars));
68108
elseif($mode == FALSE)
69-
$this->assertFalse($this->api->$key);
109+
$this->assertFalse(array_key_exists($key,$api_class_vars));
70110
}
71111
}
72112
}

0 commit comments

Comments
 (0)