| [ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 lt_include( PLOG_CLASS_PATH."class/net/http/httpclient.class.php" ); 4 5 /** 6 * \ingroup Test 7 * 8 * This is a helper class that allows to execute scripts (defined as a PHP array with certain values) 9 * to test user interfaces. Scripts are quite simple and are based on the idea of fetching a URL 10 * (or submitting a form) and expecting to find a certain value in the resulting page. 11 * 12 * Scripts are fairly simple with only a small subsets of properties. The following is an 13 * example of a script that logs in, selects a blog and logs out: 14 * 15 * <pre> 16 * Array( 17 * "login" => Array( 18 * "url" => "http://localhost/lt/admin.php", 19 * "type" => "post", 20 * "params" => Array( 21 * "userName" => "user", 22 * "userPassword" => "password", 23 * "op" => "Login" 24 * ), 25 * "expected" => "Dashboard", 26 * "message" => "The dashboard did not appear when logging in" 27 * ), 28 * "select_blog" => Array( 29 * "url" => "http://localhost/lt/admin.php", 30 * "type" => "get", 31 * "params" => Array( 32 * "op" => "blogSelect", 33 * "blogId" => "1" 34 * ), 35 * "expected" => "New Post", 36 * "message" => "The blog could not be selected after the dashboard" 37 * ), 38 * "logout" => Array( 39 * "url" => "http://localhost/lt/admin.php", 40 * "type" => "get", 41 * "params" => Array( "op" => "Logout" ), 42 * "expected" => "You have been successfully logged out", 43 * "message" => "The logout screen was not displayed correctly" 44 * ) 45 * ); 46 * </pre> 47 * 48 * Handling of HTTP requests is handled via the HttpClient class, and cookies are kept 49 * accross requests. 50 * 51 * In order to incorporate these in your tests scripts, please use the method 52 * LifeTypeTestCase::assertUIScript() 53 */ 54 class UIScriptRunner 55 { 56 var $c; 57 var $failedStep; 58 var $failedStepErrorMessage; 59 var $debug; 60 61 function UIScriptRunner() 62 { 63 $this->c = new HttpClient(); 64 65 $this->debug = false; 66 } 67 68 /** 69 * Runs a UI script from an array 70 */ 71 function run( $script ) 72 { 73 $result = false; 74 foreach( $script as $stepName => $step ) { 75 $result = $this->_runStep( $step ); 76 if( !$result ) { 77 $this->failedStep = "$stepName"; 78 $this->failedStepErrorMessage = $step["message"]; 79 break; 80 } 81 } 82 83 return( $result ); 84 } 85 86 /** 87 * If UIStepRunner::run() returns false, this method will return the name of the 88 * step that failed. 89 * 90 * @return A String 91 */ 92 function getFailedStep() 93 { 94 return( $this->failedStep ); 95 } 96 97 /** 98 * If UIStepRunner::run() returns false, this method will return the name of the 99 * step that failed. 100 * 101 * @return A String 102 */ 103 function getFailedStepErrorMessage() 104 { 105 return( $this->failedStepErrorMessage ); 106 } 107 108 /** 109 * @private 110 */ 111 function _runStep( $step ) 112 { 113 // get the url 114 $url = $step["url"]; 115 116 // build the parameters depending on the type of request 117 $type = $step["type"]; 118 if( $type == "get" ) { 119 if( isset( $step["params"] )) { 120 $params = $step["params"]; 121 $query = ""; 122 foreach( $params as $var => $value ) { 123 $query .= $var."=".urlencode( $value )."&"; 124 } 125 $query = $url."?".$query; 126 } 127 else { 128 $query = $url; 129 } 130 131 // execute the query 132 $result = $this->c->fetch( $query ); 133 } 134 else { 135 $result = $this->c->submit( $url, $step["params"] ); 136 } 137 138 // force the HttpClient to store its own cookies so that we can get some sort of 139 // stateful navigation 140 $this->c->setcookies(); 141 142 // if the request was not possible, return error 143 if( !$result ) 144 return false; 145 146 if( $this->debug ) { 147 print($this->c->results); 148 print("<hr/>"); 149 } 150 151 // if an HTTP response code was specified, check if it matches 152 if( isset( $step["httpcode"] )) { 153 if( $this->c->response_code != $step["httpcode"] ) 154 return false; 155 } 156 157 // and if the response did not contain the expected content, return error too 158 if( isset( $step["expected"] )) { 159 $expected = $step["expected"]; 160 if( is_array( $expected )) { 161 foreach( $expected as $string ) { 162 if( !strstr( $this->c->results, $string )) 163 return false; 164 } 165 } 166 else { 167 if( !strstr( $this->c->results, $expected )) 168 return false; 169 } 170 } 171 172 // additionally, check if there is something that should not be there 173 if( isset( $step["notexpected"] )) { 174 $expected = $step["notexpected"]; 175 if( is_array( $expected )) { 176 foreach( $expected as $string ) { 177 if( strstr( $this->c->results, $string )) 178 return false; 179 } 180 } 181 else { 182 if( strstr( $this->c->results, $expected )) 183 return false; 184 } 185 } 186 187 188 return true; 189 } 190 } 191 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
|