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