diff options
Diffstat (limited to '_test/core/TestRequest.php')
-rw-r--r-- | _test/core/TestRequest.php | 75 |
1 files changed, 74 insertions, 1 deletions
diff --git a/_test/core/TestRequest.php b/_test/core/TestRequest.php index fa3ddec90..172821576 100644 --- a/_test/core/TestRequest.php +++ b/_test/core/TestRequest.php @@ -36,9 +36,10 @@ class TestRequest { /** * Executes the request * + * @param string $url end URL to simulate, needs to start with /doku.php currently * @return TestResponse the resulting output of the request */ - public function execute() { + public function execute($uri='/doku.php') { // save old environment $server = $_SERVER; $session = $_SESSION; @@ -46,6 +47,14 @@ class TestRequest { $post = $_POST; $request = $_REQUEST; + // prepare the right URI + $this->setUri($uri); + + // import all defined globals into the function scope + foreach(array_keys($GLOBALS) as $glb){ + global $$glb; + } + // fake environment global $default_server_vars; $_SERVER = array_merge($default_server_vars, $this->server); @@ -79,4 +88,68 @@ 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 + * @todo make this work with other end points + */ + protected 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->post = array_merge($this->post, $post); + $this->setServer('REQUEST_METHOD', 'POST'); + return $this->execute($uri); + } + + /** + * 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->setServer('REQUEST_METHOD', 'GET'); + return $this->execute($uri); + } + + } |