xref: /dokuwiki/_test/core/TestResponse.php (revision acdf738a61532360131323876e96f504db0a2b0a)
1f8369d7dSTobias Sarnowski<?php
227c0c399SAndreas Gohr
3f8369d7dSTobias Sarnowski/**
4f8369d7dSTobias Sarnowski * holds a copy of all produced outputs of a TestRequest
5f8369d7dSTobias Sarnowski */
6f8369d7dSTobias Sarnowskiclass TestResponse {
727c0c399SAndreas Gohr    /** @var string */
827c0c399SAndreas Gohr    protected $content;
927c0c399SAndreas Gohr
1027c0c399SAndreas Gohr    /** @var array */
1127c0c399SAndreas Gohr    protected $headers;
1227c0c399SAndreas Gohr
1327c0c399SAndreas Gohr    /** @var phpQueryObject */
1427c0c399SAndreas Gohr    protected $pq = null;
1527c0c399SAndreas Gohr
1627c0c399SAndreas Gohr    /** @var array */
1727c0c399SAndreas Gohr    protected $data = array();
18f8369d7dSTobias Sarnowski
19f8369d7dSTobias Sarnowski    /**
2027c0c399SAndreas Gohr     * Constructor
2127c0c399SAndreas Gohr     *
2227c0c399SAndreas Gohr     * @param $content string the response body
2327c0c399SAndreas Gohr     * @param $headers array the headers sent in the response
2427c0c399SAndreas Gohr     * @param array $data any optional data passed back to the test system
25f8369d7dSTobias Sarnowski     */
2627c0c399SAndreas Gohr    function __construct($content, $headers, $data = array()) {
27f8369d7dSTobias Sarnowski        $this->content = $content;
28f8369d7dSTobias Sarnowski        $this->headers = $headers;
2927c0c399SAndreas Gohr        $this->data = $data;
30f8369d7dSTobias Sarnowski    }
31f8369d7dSTobias Sarnowski
32f8369d7dSTobias Sarnowski    /**
3327c0c399SAndreas Gohr     * Returns the response body
3427c0c399SAndreas Gohr     *
35f8369d7dSTobias Sarnowski     * @return string
36f8369d7dSTobias Sarnowski     */
37f8369d7dSTobias Sarnowski    public function getContent() {
38f8369d7dSTobias Sarnowski        return $this->content;
39f8369d7dSTobias Sarnowski    }
40f8369d7dSTobias Sarnowski
41f8369d7dSTobias Sarnowski    /**
4227c0c399SAndreas Gohr     * Returns the headers set in the response
4327c0c399SAndreas Gohr     *
44f8369d7dSTobias Sarnowski     * @return array
45f8369d7dSTobias Sarnowski     */
46f8369d7dSTobias Sarnowski    public function getHeaders() {
47f8369d7dSTobias Sarnowski        return $this->headers;
48f8369d7dSTobias Sarnowski    }
49f8369d7dSTobias Sarnowski
50f8369d7dSTobias Sarnowski    /**
5127c0c399SAndreas Gohr     * Return a single header
5227c0c399SAndreas 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    /**
6827c0c399SAndreas 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
7227c0c399SAndreas Gohr     *
7327c0c399SAndreas 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) {
100*acdf738aSAndreas Gohr        if(is_null($this->pq)) {
101*acdf738aSAndreas Gohr            $this->pq = new \DOMWrap\Document();
102*acdf738aSAndreas Gohr            $this->pq->html($this->content);
103*acdf738aSAndreas Gohr        }
104f8369d7dSTobias Sarnowski        return $this->pq->find($selector);
105f8369d7dSTobias Sarnowski    }
106572dc222SLarsDW223
107572dc222SLarsDW223    /**
10827c0c399SAndreas Gohr     * Returns all collected data for the given key
10927c0c399SAndreas Gohr     *
11027c0c399SAndreas Gohr     * @param string $key
111572dc222SLarsDW223     * @return array
112572dc222SLarsDW223     */
11327c0c399SAndreas Gohr    public function getData($key) {
11427c0c399SAndreas Gohr        if(!isset($this->data[$key])) return array();
11527c0c399SAndreas Gohr        return $this->data[$key];
116572dc222SLarsDW223    }
117f8369d7dSTobias Sarnowski}
118