1f8369d7dSTobias Sarnowski<?php 227c0c399SAndreas Gohr 3*17d97b8fSAndreas Gohruse DOMWrap\Document; 4*17d97b8fSAndreas Gohruse DOMWrap\NodeList; 5*17d97b8fSAndreas Gohr 6f8369d7dSTobias Sarnowski/** 7f8369d7dSTobias Sarnowski * holds a copy of all produced outputs of a TestRequest 8f8369d7dSTobias Sarnowski */ 9f8369d7dSTobias Sarnowskiclass TestResponse { 1027c0c399SAndreas Gohr /** @var string */ 1127c0c399SAndreas Gohr protected $content; 1227c0c399SAndreas Gohr 1327c0c399SAndreas Gohr /** @var array */ 1427c0c399SAndreas Gohr protected $headers; 1527c0c399SAndreas Gohr 16*17d97b8fSAndreas Gohr /** @var Document */ 1727c0c399SAndreas Gohr protected $pq = null; 1827c0c399SAndreas Gohr 1927c0c399SAndreas Gohr /** @var array */ 2027c0c399SAndreas Gohr protected $data = array(); 21f8369d7dSTobias Sarnowski 22f8369d7dSTobias Sarnowski /** 2327c0c399SAndreas Gohr * Constructor 2427c0c399SAndreas Gohr * 2527c0c399SAndreas Gohr * @param $content string the response body 2627c0c399SAndreas Gohr * @param $headers array the headers sent in the response 2727c0c399SAndreas Gohr * @param array $data any optional data passed back to the test system 28f8369d7dSTobias Sarnowski */ 2927c0c399SAndreas Gohr function __construct($content, $headers, $data = array()) { 30f8369d7dSTobias Sarnowski $this->content = $content; 31f8369d7dSTobias Sarnowski $this->headers = $headers; 3227c0c399SAndreas Gohr $this->data = $data; 33f8369d7dSTobias Sarnowski } 34f8369d7dSTobias Sarnowski 35f8369d7dSTobias Sarnowski /** 3627c0c399SAndreas Gohr * Returns the response body 3727c0c399SAndreas Gohr * 38f8369d7dSTobias Sarnowski * @return string 39f8369d7dSTobias Sarnowski */ 40f8369d7dSTobias Sarnowski public function getContent() { 41f8369d7dSTobias Sarnowski return $this->content; 42f8369d7dSTobias Sarnowski } 43f8369d7dSTobias Sarnowski 44f8369d7dSTobias Sarnowski /** 4527c0c399SAndreas Gohr * Returns the headers set in the response 4627c0c399SAndreas Gohr * 47f8369d7dSTobias Sarnowski * @return array 48f8369d7dSTobias Sarnowski */ 49f8369d7dSTobias Sarnowski public function getHeaders() { 50f8369d7dSTobias Sarnowski return $this->headers; 51f8369d7dSTobias Sarnowski } 52f8369d7dSTobias Sarnowski 53f8369d7dSTobias Sarnowski /** 5427c0c399SAndreas Gohr * Return a single header 5527c0c399SAndreas Gohr * 56fb0b3fbfSChristopher Smith * @param $name string, the name of the header without the ':', e.g. 'Content-Type', 'Pragma' 57fb0b3fbfSChristopher Smith * @return mixed if exactly one header, the header (string); otherwise an array of headers, empty when no headers 58fb0b3fbfSChristopher Smith */ 59fb0b3fbfSChristopher Smith public function getHeader($name) { 60fb0b3fbfSChristopher Smith $result = array(); 61fb0b3fbfSChristopher Smith foreach($this->headers as $header) { 62fb0b3fbfSChristopher Smith if(substr($header, 0, strlen($name) + 1) == $name . ':') { 63fb0b3fbfSChristopher Smith $result[] = $header; 64fb0b3fbfSChristopher Smith } 65fb0b3fbfSChristopher Smith } 66fb0b3fbfSChristopher Smith 67fb0b3fbfSChristopher Smith return count($result) == 1 ? $result[0] : $result; 68fb0b3fbfSChristopher Smith } 69fb0b3fbfSChristopher Smith 70fb0b3fbfSChristopher Smith /** 7127c0c399SAndreas Gohr * Access the http status code 72fb0b3fbfSChristopher Smith * 73fb0b3fbfSChristopher Smith * in the test environment, only status codes explicitly set by dokuwiki are likely to be returned 74fb0b3fbfSChristopher Smith * this means succcessful status codes (e.g. 200 OK) will not be present, but error codes will be 7527c0c399SAndreas Gohr * 7627c0c399SAndreas Gohr * @return int http status code 77fb0b3fbfSChristopher Smith */ 78fb0b3fbfSChristopher Smith public function getStatusCode() { 79fb0b3fbfSChristopher Smith $headers = $this->getHeader('Status'); 80fb0b3fbfSChristopher Smith $code = null; 81fb0b3fbfSChristopher Smith 82fb0b3fbfSChristopher Smith if($headers) { 83fb0b3fbfSChristopher Smith // if there is more than one status header, use the last one 84fb0b3fbfSChristopher Smith $status = is_array($headers) ? array_pop($headers) : $headers; 85fb0b3fbfSChristopher Smith $matches = array(); 86fb0b3fbfSChristopher Smith preg_match('/^Status: ?(\d+)/', $status, $matches); 87fb0b3fbfSChristopher Smith if($matches) { 88fb0b3fbfSChristopher Smith $code = $matches[1]; 89fb0b3fbfSChristopher Smith } 90fb0b3fbfSChristopher Smith } 91fb0b3fbfSChristopher Smith 92fb0b3fbfSChristopher Smith return $code; 93fb0b3fbfSChristopher Smith } 94fb0b3fbfSChristopher Smith 95fb0b3fbfSChristopher Smith /** 96*17d97b8fSAndreas Gohr * Query the response for a JQuery like CSS selector 97f8369d7dSTobias Sarnowski * 98f8369d7dSTobias Sarnowski * @param $selector string 99*17d97b8fSAndreas Gohr * @throws Exception 100*17d97b8fSAndreas Gohr * @return NodeList 101f8369d7dSTobias Sarnowski */ 102f8369d7dSTobias Sarnowski public function queryHTML($selector) { 103acdf738aSAndreas Gohr if(is_null($this->pq)) { 104*17d97b8fSAndreas Gohr $this->pq = new Document(); 105acdf738aSAndreas Gohr $this->pq->html($this->content); 106acdf738aSAndreas Gohr } 107f8369d7dSTobias Sarnowski return $this->pq->find($selector); 108f8369d7dSTobias Sarnowski } 109572dc222SLarsDW223 110572dc222SLarsDW223 /** 11127c0c399SAndreas Gohr * Returns all collected data for the given key 11227c0c399SAndreas Gohr * 11327c0c399SAndreas Gohr * @param string $key 114572dc222SLarsDW223 * @return array 115572dc222SLarsDW223 */ 11627c0c399SAndreas Gohr public function getData($key) { 11727c0c399SAndreas Gohr if(!isset($this->data[$key])) return array(); 11827c0c399SAndreas Gohr return $this->data[$key]; 119572dc222SLarsDW223 } 120f8369d7dSTobias Sarnowski} 121