Skip to content
Snippets Groups Projects
Commit 9e777cee authored by Andreas Gohr's avatar Andreas Gohr
Browse files

simplified using the TestRequest class

You now can call get() or post() on it and give it all the wanted
input variables
parent 5d0aaf95
No related branches found
No related tags found
No related merge requests found
......@@ -84,4 +84,69 @@ class TestRequest {
return $response;
}
/**
* Set the virtual URI the request works against
*
* This parses the given URI and sets any contained GET variables
* but will not overwrite any previously set ones (eg. set via setGet()).
*
* It initializes the $_SERVER['REQUEST_URI'] and $_SERVER['QUERY_STRING']
* with all set GET variables.
*
* @param string $url end URL to simulate, needs to start with /doku.php currently
*/
public function setUri($uri){
if(substr($uri,0,9) != '/doku.php'){
throw new Exception("only '/doku.php' is supported currently");
}
$params = array();
list($uri, $query) = explode('?',$uri,2);
if($query) parse_str($query, $params);
$this->get = array_merge($params, $this->get);
if(count($this->get)){
$query = '?'.http_build_query($this->get, '', '&');
$query = str_replace(
array('%3A', '%5B', '%5D'),
array(':', '[', ']'),
$query
);
$uri = $uri.$query;
}
$this->setServer('QUERY_STRING', $query);
$this->setServer('REQUEST_URI', $uri);
}
/**
* Simulate a POST request with the given variables
*
* @param array $post all the POST parameters to use
* @param string $url end URL to simulate, needs to start with /doku.php currently
* @param return TestResponse
*/
public function post($post=array(), $uri='/doku.php') {
$this->setUri($uri);
$this->post = array_merge($this->post, $post);
$this->setServer('REQUEST_METHOD', 'POST');
return $this->execute();
}
/**
* Simulate a GET request with the given variables
*
* @param array $GET all the POST parameters to use
* @param string $url end URL to simulate, needs to start with /doku.php currently
* @param return TestResponse
*/
public function get($get=array(), $uri='/doku.php') {
$this->get = array_merge($this->get, $get);
$this->setUri($uri);
$this->setServer('REQUEST_METHOD', 'GET');
return $this->execute();
}
}
......@@ -19,4 +19,87 @@ class InttestsBasicTest extends DokuWikiTest {
'DokuWiki was not a word in the output'
);
}
function testPost() {
$request = new TestRequest();
$input = array(
'string' => 'A string',
'array' => array(1, 2, 3),
'id' => 'wiki:dokuwiki'
);
$response = $request->post($input);
// server var check
$this->assertEquals('POST',$request->getServer('REQUEST_METHOD'));
$this->assertEquals('',$request->getServer('QUERY_STRING'));
$this->assertEquals('/doku.php',$request->getServer('REQUEST_URI'));
// variable setup check
$this->assertEquals('A string', $request->getPost('string'));
$this->assertEquals(array(1, 2, 3), $request->getPost('array'));
$this->assertEquals('wiki:dokuwiki', $request->getPost('id'));
// output check
$this->assertTrue(strpos($response->getContent(), 'Andreas Gohr') >= 0);
}
function testPostGet() {
$request = new TestRequest();
$input = array(
'string' => 'A string',
'array' => array(1, 2, 3),
);
$response = $request->post($input,'/doku.php?id=wiki:dokuwiki');
// server var check
$this->assertEquals('POST',$request->getServer('REQUEST_METHOD'));
$this->assertEquals('?id=wiki:dokuwiki',$request->getServer('QUERY_STRING'));
$this->assertEquals('/doku.php?id=wiki:dokuwiki',$request->getServer('REQUEST_URI'));
// variable setup check
$this->assertEquals('A string', $request->getPost('string'));
$this->assertEquals(array(1, 2, 3), $request->getPost('array'));
$this->assertEquals('wiki:dokuwiki', $request->getGet('id'));
// output check
$this->assertTrue(strpos($response->getContent(), 'Andreas Gohr') >= 0);
}
function testGet() {
$request = new TestRequest();
$input = array(
'string' => 'A string',
'array' => array(1, 2, 3),
'test' => 'bar'
);
$response = $request->get($input,'/doku.php?id=wiki:dokuwiki&test=foo');
// server var check
$this->assertEquals('GET',$request->getServer('REQUEST_METHOD'));
$this->assertEquals(
'?id=wiki:dokuwiki&test=bar&string=A+string&array[0]=1&array[1]=2&array[2]=3',
$request->getServer('QUERY_STRING')
);
$this->assertEquals(
'/doku.php?id=wiki:dokuwiki&test=bar&string=A+string&array[0]=1&array[1]=2&array[2]=3',
$request->getServer('REQUEST_URI')
);
// variable setup check
$this->assertEquals('A string', $request->getGet('string'));
$this->assertEquals(array(1, 2, 3), $request->getGet('array'));
$this->assertEquals('wiki:dokuwiki', $request->getGet('id'));
$this->assertEquals('bar', $request->getGet('test'));
// output check
$this->assertTrue(strpos($response->getContent(), 'Andreas Gohr') >= 0);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment