1<?php
2
3use Sabre\DAV;
4use Sabre\HTTP;
5use dokuwiki\plugin\webdav\core\DAV\Collection;
6use dokuwiki\plugin\webdav\core\Plugin;
7
8class DAVServerTest
9{
10
11    public $server;
12    public $output_buffer;
13
14    public function __construct()
15    {
16        $wiki_collections = [
17            'pages' => new Collection\Pages\Directory(),
18            'media' => new Collection\Media\Directory(),
19        ];
20
21        $this->server = new DAV\Server(new DAV\SimpleCollection('root', [
22            new DAV\SimpleCollection('wiki', $wiki_collections),
23        ]));
24
25        $this->server->setBaseUri('/');
26        $this->server->addPlugin(new Plugin\DokuWiki());
27    }
28
29    /**
30     * Callback for ob_start
31     *
32     * This continues to fill our own buffer, even when some part
33     * of the code askes for flushing the buffers
34     *
35     * @param string $buffer
36     */
37    public function ob_start_callback($buffer)
38    {
39        $this->output_buffer .= $buffer;
40    }
41
42    /**
43     * Makes a request, and returns a response object.
44     *
45     * You can either pass an instance of Sabre\HTTP\Request, or an array,
46     * which will then be used as the _SERVER array.
47     *
48     * @param array|\Sabre\HTTP\Request $request
49     * @return \Sabre\HTTP\Response
50     */
51    public function request($request)
52    {
53        if (is_array($request)) {
54            $request = HTTP\Sapi::createFromServerArray($request);
55        }
56        $response = new ResponseMock();
57
58        $this->server->httpRequest  = $request;
59        $this->server->httpResponse = $response;
60
61        # Capture stdout response
62        ob_start([$this, 'ob_start_callback']);
63        $this->server->exec();
64        ob_end_flush();
65
66        return $this->server->httpResponse;
67    }
68}
69
70class ResponseMock extends HTTP\Response
71{
72    /**
73     * Making these public.
74     */
75    public $body;
76    public $status;
77}
78