File: Mock.php

package info (click to toggle)
php-horde-http 2.1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 208 kB
  • ctags: 256
  • sloc: php: 943; xml: 491; sh: 3; makefile: 2
file content (109 lines) | stat: -rw-r--r-- 3,326 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
 * Copyright 2007-2014 Horde LLC (http://www.horde.org/)
 *
 * @author   Chuck Hagenbuch <chuck@horde.org>
 * @author   Gunnar Wrobel <wrobel@pardus.de>
 * @license  http://www.horde.org/licenses/bsd BSD
 * @category Horde
 * @package  Http
 */

/**
 * @author   Chuck Hagenbuch <chuck@horde.org>
 * @author   Gunnar Wrobel <wrobel@pardus.de>
 * @license  http://www.horde.org/licenses/bsd BSD
 * @category Horde
 * @package  Http
 */
class Horde_Http_Request_Mock extends Horde_Http_Request_Base
{
    /**
     * Mock responses to return.
     *
     * @var array
     */
    protected $_responses = array();

    /**
     * Send this HTTP request
     *
     * @return Horde_Http_Response_Mock|NULL A response object or NULL in case
     *                                       no responses has been set.
     */
    public function send()
    {
        if (empty($this->_responses)) {
            return;
        } elseif (count($this->_responses) > 1) {
            return array_shift($this->_responses);
        } else {
            return $this->_responses[0];
        }
    }

    /**
     * Set the HTTP response(s) to be returned by this adapter. This overwrites
     * any responses set before.
     *
     * @param Horde_Http_Response_Base $response
     */
    public function setResponse(Horde_Http_Response_Base $response)
    {
        $this->_responses = array($response);
    }

    /**
     * Set the HTTP response(s) to be returned by this adapter as an array of strings.
     *
     * @param array $responses The responses to be added to the stack.
     *
     * @return NULL
     */
    public function addResponses($responses)
    {
        foreach ($responses as $response) {
            if (is_string($response)) {
                $this->addResponse($response);
            }
            if (is_array($response)) {
                $this->addResponse(
                    isset($response['body']) ? $response['body'] : '',
                    isset($response['code']) ? $response['code'] : 200,
                    isset($response['uri']) ? $response['uri'] : '',
                    isset($response['headers']) ? $response['headers'] : array()
                );
            }
        }
    }

    /**
     * Adds a response to the stack of responses.
     *
     * @param string|resourse $body    The response body content.
     * @param string          $code    The response code.
     * @param string          $uri     The request uri.
     * @param array           $headers Response headers. This can be one string
     *                                 representing the whole header or an array
     *                                 of strings with one string per header
     *                                 line.
     *
     * @return Horde_Http_Response_Mock The response.
     */
    public function addResponse(
        $body, $code = 200, $uri = '', $headers = array()
    )
    {
        if (is_string($body)) {
            $stream = new Horde_Support_StringStream($body);
            $response = new Horde_Http_Response_Mock(
                $uri, $stream->fopen(), $headers
            );
        } else {
            $response = new Horde_Http_Response_Mock($uri, $body, $headers);
        }
        $response->code = $code;
        $this->_responses[] = $response;
    }

}