Created
March 31, 2011 13:27
-
-
Save havvg/896332 to your computer and use it in GitHub Desktop.
An adapter implementation used by the sfWebBrowserPlugin for testing purposes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A simple example test case using the sfTestAdapter. | |
* | |
* @author "Toni Uebernickel" <tuebernickel@gmail.com> | |
*/ | |
class ExampleTest extends PHPUnit_Framework_TestCase | |
{ | |
/** | |
* Returns an empty but valid HTTP response in plain text. | |
* | |
* @param int $code The HTTP status code to use. | |
* | |
* @return string | |
*/ | |
protected function getHttpResponse($code) | |
{ | |
$httpHeaderMessages = array( | |
200 => 'OK', | |
404 => 'Not Found', | |
503 => 'Service Unavailable', | |
); | |
$httpResponse = <<<EOF | |
HTTP/1.1 $code $httpHeaderMessages[$code] | |
Location: http://127.0.0.1/ | |
Content-Type: text/plain; charset=UTF-8 | |
Date: Wed, 30 Mar 2011 11:18:07 GMT | |
Expires: Fri, 29 Apr 2011 11:18:07 GMT | |
Cache-Control: public, max-age=2592000 | |
Server: Apache2 | |
Content-Length: 0 | |
X-XSS-Protection: 1; mode=block | |
EOF; | |
return $httpResponse; | |
} | |
/** | |
* DataProvider for server error responses. | |
* | |
* @return array | |
*/ | |
public function serverErrorProvider() | |
{ | |
return array( | |
array(404), | |
array(503), | |
); | |
} | |
/** | |
* @dataProvider serverErrorProvider | |
*/ | |
public function testServerError($code) | |
{ | |
sfTestAdapter::$response = $this->getHttpResponse($code); | |
// call something using sfWebBrowser | |
// do assertions | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* An adapter implementation used by the sfWebBrowserPlugin for testing purposes. | |
* | |
* This class is not initiating any HTTP request but rather returns the current HTTP response and passes it to the sfWebBrowser. | |
* | |
* @author "Toni Uebernickel" <tuebernickel@gmail.com> | |
*/ | |
class sfTestAdapter | |
{ | |
/** | |
* The response to return an the next call. | |
* | |
* @var string The plain HTTP response to return on the next request. | |
*/ | |
public static $response = null; | |
/** | |
* Submits a request. | |
* | |
* @param sfWebBrowser $browser | |
* @param string The request uri | |
* @param string The request method | |
* @param array The request parameters (associative array) | |
* @param array The request headers (associative array) | |
* | |
* @return sfWebBrowser The current browser object | |
*/ | |
public function call($browser, $uri, $method = 'GET', $parameters = array(), $headers = array()) | |
{ | |
return $this->parseResponse($browser, self::$response); | |
} | |
/** | |
* Parses the HTTP response string and applies properties to the browser. | |
* | |
* Code from sfWebBrowser itself. | |
* | |
* @param sfWebBrowser $browser The browser to retrieve the parsed response. | |
* @param string $response The plain return value of the HTTP Response. | |
* | |
* @return sfWebBrowser The modified browser containing response values. | |
*/ | |
protected function parseResponse($browser, $response) | |
{ | |
// parse response components: status line, headers and body | |
$response_lines = explode("\r\n", $response); | |
// http status line (ie "HTTP 1.1 200 OK") | |
$status_line = array_shift($response_lines); | |
$start_body = false; | |
$response_headers = array(); | |
for($i=0; $i<count($response_lines); $i++) | |
{ | |
// grab body | |
if ($start_body == true) | |
{ | |
// ignore chunked encoding size | |
if (!preg_match('@^[0-9A-Fa-f]+\s*$@', $response_lines[$i])) | |
{ | |
$response_body .= $response_lines[$i]; | |
} | |
} | |
// body starts after first blank line | |
else if ($start_body == false && $response_lines[$i] == '') | |
{ | |
$start_body = true; | |
} | |
// grab headers | |
else | |
{ | |
$response_headers[] = $response_lines[$i]; | |
} | |
} | |
$browser->setResponseHeaders($response_headers); | |
// grab status code | |
preg_match('@(\d{3})@', $status_line, $status_code); | |
if(isset($status_code[1])) | |
{ | |
$browser->setResponseCode($status_code[1]); | |
} | |
$browser->setResponseText(trim($response_body)); | |
return $browser; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment