xref: /dokuwiki/_test/core/TestRequest.php (revision 0189bd8669742c5290a4f6538f954b81554e26d2)
1<?php
2/**
3 * Simulates a full DokuWiki HTTP Request and allows
4 * runtime inspection.
5 */
6
7// output buffering
8$output_buffer = '';
9
10function ob_start_callback($buffer) {
11    global $output_buffer;
12    $output_buffer .= $buffer;
13}
14
15
16/**
17 * Helper class to execute a fake request
18 */
19class TestRequest {
20
21    private $server = array();
22    private $session = array();
23    private $get = array();
24    private $post = array();
25
26    public function getServer($key) { return $this->server[$key]; }
27    public function getSession($key) { return $this->session[$key]; }
28    public function getGet($key) { return $this->get[$key]; }
29    public function getPost($key) { return $this->post[$key]; }
30
31    public function setServer($key, $value) { $this->server[$key] = $value; }
32    public function setSession($key, $value) { $this->session[$key] = $value; }
33    public function setGet($key, $value) { $this->get[$key] = $value; }
34    public function setPost($key, $value) { $this->post[$key] = $value; }
35
36    /**
37     * Executes the request
38     *
39     * @return TestResponse the resulting output of the request
40     */
41    public function execute() {
42        // save old environment
43        $server = $_SERVER;
44        $session = $_SESSION;
45        $get = $_GET;
46        $post = $_POST;
47        $request = $_REQUEST;
48
49        // import all defined globals into the function scope
50        foreach(array_keys($GLOBALS) as $glb){
51            global $$glb;
52        }
53
54        // fake environment
55        global $default_server_vars;
56        $_SERVER = array_merge($default_server_vars, $this->server);
57        $_SESSION = $this->session;
58        $_GET = $this->get;
59        $_POST = $this->post;
60        $_REQUEST = array_merge($_GET, $_POST);
61
62        // reset output buffer
63        global $output_buffer;
64        $output_buffer = '';
65
66        // now execute dokuwiki and grep the output
67        header_remove();
68        ob_start('ob_start_callback');
69        include(DOKU_INC.'doku.php');
70        ob_end_flush();
71
72        // create the response object
73        $response = new TestResponse(
74            $output_buffer,
75            headers_list()
76        );
77
78        // reset environment
79        $_SERVER = $server;
80        $_SESSION = $session;
81        $_GET = $get;
82        $_POST = $post;
83        $_REQUEST = $request;
84
85        return $response;
86    }
87}
88