[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

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

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