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