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 /** 22*572dc222SLarsDW223 * @var notifications 23*572dc222SLarsDW223 */ 24*572dc222SLarsDW223 private $notifications = null; 25*572dc222SLarsDW223 26*572dc222SLarsDW223 /** 27f8369d7dSTobias Sarnowski * @param $content string 28f8369d7dSTobias Sarnowski * @param $headers array 29f8369d7dSTobias Sarnowski */ 30f8369d7dSTobias Sarnowski function __construct($content, $headers) { 31f8369d7dSTobias Sarnowski $this->content = $content; 32f8369d7dSTobias Sarnowski $this->headers = $headers; 33f8369d7dSTobias Sarnowski } 34f8369d7dSTobias Sarnowski 35f8369d7dSTobias Sarnowski /** 36f8369d7dSTobias Sarnowski * @return string 37f8369d7dSTobias Sarnowski */ 38f8369d7dSTobias Sarnowski public function getContent() { 39f8369d7dSTobias Sarnowski return $this->content; 40f8369d7dSTobias Sarnowski } 41f8369d7dSTobias Sarnowski 42f8369d7dSTobias Sarnowski /** 43f8369d7dSTobias Sarnowski * @return array 44f8369d7dSTobias Sarnowski */ 45f8369d7dSTobias Sarnowski public function getHeaders() { 46f8369d7dSTobias Sarnowski return $this->headers; 47f8369d7dSTobias Sarnowski } 48f8369d7dSTobias Sarnowski 49f8369d7dSTobias Sarnowski /** 50fb0b3fbfSChristopher Smith * @param $name string, the name of the header without the ':', e.g. 'Content-Type', 'Pragma' 51fb0b3fbfSChristopher Smith * @return mixed if exactly one header, the header (string); otherwise an array of headers, empty when no headers 52fb0b3fbfSChristopher Smith */ 53fb0b3fbfSChristopher Smith public function getHeader($name) { 54fb0b3fbfSChristopher Smith $result = array(); 55fb0b3fbfSChristopher Smith foreach ($this->headers as $header) { 56fb0b3fbfSChristopher Smith if (substr($header,0,strlen($name)+1) == $name.':') { 57fb0b3fbfSChristopher Smith $result[] = $header; 58fb0b3fbfSChristopher Smith } 59fb0b3fbfSChristopher Smith } 60fb0b3fbfSChristopher Smith 61fb0b3fbfSChristopher Smith return count($result) == 1 ? $result[0] : $result; 62fb0b3fbfSChristopher Smith } 63fb0b3fbfSChristopher Smith 64fb0b3fbfSChristopher Smith /** 65fb0b3fbfSChristopher Smith * @return int http status code 66fb0b3fbfSChristopher Smith * 67fb0b3fbfSChristopher Smith * in the test environment, only status codes explicitly set by dokuwiki are likely to be returned 68fb0b3fbfSChristopher Smith * this means succcessful status codes (e.g. 200 OK) will not be present, but error codes will be 69fb0b3fbfSChristopher Smith */ 70fb0b3fbfSChristopher Smith public function getStatusCode() { 71fb0b3fbfSChristopher Smith $headers = $this->getHeader('Status'); 72fb0b3fbfSChristopher Smith $code = null; 73fb0b3fbfSChristopher Smith 74fb0b3fbfSChristopher Smith if ($headers) { 75fb0b3fbfSChristopher Smith // if there is more than one status header, use the last one 76fb0b3fbfSChristopher Smith $status = is_array($headers) ? array_pop($headers) : $headers; 77fb0b3fbfSChristopher Smith $matches = array(); 78fb0b3fbfSChristopher Smith preg_match('/^Status: ?(\d+)/',$status,$matches); 79fb0b3fbfSChristopher Smith if ($matches){ 80fb0b3fbfSChristopher Smith $code = $matches[1]; 81fb0b3fbfSChristopher Smith } 82fb0b3fbfSChristopher Smith } 83fb0b3fbfSChristopher Smith 84fb0b3fbfSChristopher Smith return $code; 85fb0b3fbfSChristopher Smith } 86fb0b3fbfSChristopher Smith 87fb0b3fbfSChristopher Smith /** 88f8369d7dSTobias Sarnowski * Query the response for a JQuery compatible CSS selector 89f8369d7dSTobias Sarnowski * 90f8369d7dSTobias Sarnowski * @link https://code.google.com/p/phpquery/wiki/Selectors 91f8369d7dSTobias Sarnowski * @param $selector string 92f8369d7dSTobias Sarnowski * @return phpQueryObject 93f8369d7dSTobias Sarnowski */ 94f8369d7dSTobias Sarnowski public function queryHTML($selector){ 95f8369d7dSTobias Sarnowski if(is_null($this->pq)) $this->pq = phpQuery::newDocument($this->content); 96f8369d7dSTobias Sarnowski return $this->pq->find($selector); 97f8369d7dSTobias Sarnowski } 98*572dc222SLarsDW223 99*572dc222SLarsDW223 /** 100*572dc222SLarsDW223 * @return array 101*572dc222SLarsDW223 */ 102*572dc222SLarsDW223 public function getNotifications() { 103*572dc222SLarsDW223 return $this->notifications; 104*572dc222SLarsDW223 } 105*572dc222SLarsDW223 106*572dc222SLarsDW223 /** 107*572dc222SLarsDW223 * Set notifications 108*572dc222SLarsDW223 */ 109*572dc222SLarsDW223 public function setNotifications(array $new) { 110*572dc222SLarsDW223 $this->notifications = $new; 111*572dc222SLarsDW223 } 112f8369d7dSTobias Sarnowski} 113