[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/_test/core/ -> TestResponse.php (source)

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