xref: /dokuwiki/_test/core/TestResponse.php (revision 5c967d3d04622c1420313f1a514da5cda99827f3)
1<?php
2/**
3 * holds a copy of all produced outputs of a TestRequest
4 */
5class TestResponse {
6    /**
7     * @var string
8     */
9    private $content;
10
11    /**
12     * @var array
13     */
14    private $headers;
15
16    /**
17     * @var phpQueryObject
18     */
19    private $pq = null;
20
21    /**
22     * @param $content string
23     * @param $headers array
24     */
25    function __construct($content, $headers) {
26        $this->content = $content;
27        $this->headers = $headers;
28    }
29
30    /**
31     * @return string
32     */
33    public function getContent() {
34        return $this->content;
35    }
36
37    /**
38     * @return array
39     */
40    public function getHeaders() {
41        return $this->headers;
42    }
43
44    /**
45     * @param  $name   string, the name of the header without the ':', e.g. 'Content-Type', 'Pragma'
46     * @return mixed   if exactly one header, the header (string); otherwise an array of headers, empty when no headers
47     */
48    public function getHeader($name) {
49        $result = array();
50        foreach ($this->headers as $header) {
51            if (substr($header,0,strlen($name)+1) == $name.':') {
52                $result[] = $header;
53            }
54        }
55
56        return count($result) == 1 ? $result[0] : $result;
57    }
58
59    /**
60     * @return  int  http status code
61     *
62     * in the test environment, only status codes explicitly set by dokuwiki are likely to be returned
63     * this means succcessful status codes (e.g. 200 OK) will not be present, but error codes will be
64     */
65    public function getStatusCode() {
66        $headers = $this->getHeader('Status');
67        $code = null;
68
69        if ($headers) {
70            // if there is more than one status header, use the last one
71            $status = is_array($headers) ? array_pop($headers) : $headers;
72            $matches = array();
73            preg_match('/^Status: ?(\d+)/',$status,$matches);
74            if ($matches){
75                $code = $matches[1];
76            }
77         }
78
79         return $code;
80    }
81
82    /**
83     * Query the response for a JQuery compatible CSS selector
84     *
85     * @link https://code.google.com/p/phpquery/wiki/Selectors
86     * @param $selector string
87     * @return phpQueryObject
88     */
89    public function queryHTML($selector){
90        if(is_null($this->pq)) $this->pq = phpQuery::newDocument($this->content);
91        return $this->pq->find($selector);
92    }
93}
94