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