1<?php
2/**
3 * Copyright 2007-2016 Horde LLC (http://www.horde.org/)
4 *
5 * @author   Chuck Hagenbuch <chuck@horde.org>
6 * @author   Gunnar Wrobel <wrobel@pardus.de>
7 * @license  http://www.horde.org/licenses/bsd BSD
8 * @category Horde
9 * @package  Http
10 */
11
12/**
13 * @author   Chuck Hagenbuch <chuck@horde.org>
14 * @author   Gunnar Wrobel <wrobel@pardus.de>
15 * @license  http://www.horde.org/licenses/bsd BSD
16 * @category Horde
17 * @package  Http
18 */
19class Horde_Http_Request_Mock extends Horde_Http_Request_Base
20{
21    /**
22     * Mock responses to return.
23     *
24     * @var array
25     */
26    protected $_responses = array();
27
28    /**
29     * Send this HTTP request
30     *
31     * @return Horde_Http_Response_Mock|NULL A response object or NULL in case
32     *                                       no responses has been set.
33     */
34    public function send()
35    {
36        if (empty($this->_responses)) {
37            return;
38        } elseif (count($this->_responses) > 1) {
39            return array_shift($this->_responses);
40        } else {
41            return $this->_responses[0];
42        }
43    }
44
45    /**
46     * Set the HTTP response(s) to be returned by this adapter. This overwrites
47     * any responses set before.
48     *
49     * @param Horde_Http_Response_Base $response
50     */
51    public function setResponse(Horde_Http_Response_Base $response)
52    {
53        $this->_responses = array($response);
54    }
55
56    /**
57     * Set the HTTP response(s) to be returned by this adapter as an array of strings.
58     *
59     * @param array $responses The responses to be added to the stack.
60     *
61     * @return NULL
62     */
63    public function addResponses($responses)
64    {
65        foreach ($responses as $response) {
66            if (is_string($response)) {
67                $this->addResponse($response);
68            }
69            if (is_array($response)) {
70                $this->addResponse(
71                    isset($response['body']) ? $response['body'] : '',
72                    isset($response['code']) ? $response['code'] : 200,
73                    isset($response['uri']) ? $response['uri'] : '',
74                    isset($response['headers']) ? $response['headers'] : array()
75                );
76            }
77        }
78    }
79
80    /**
81     * Adds a response to the stack of responses.
82     *
83     * @param string|resourse $body    The response body content.
84     * @param string          $code    The response code.
85     * @param string          $uri     The request uri.
86     * @param array           $headers Response headers. This can be one string
87     *                                 representing the whole header or an array
88     *                                 of strings with one string per header
89     *                                 line.
90     *
91     * @return Horde_Http_Response_Mock The response.
92     */
93    public function addResponse(
94        $body, $code = 200, $uri = '', $headers = array()
95    )
96    {
97        if (is_string($body)) {
98            $stream = new Horde_Support_StringStream($body);
99            $response = new Horde_Http_Response_Mock(
100                $uri, $stream->fopen(), $headers
101            );
102        } else {
103            $response = new Horde_Http_Response_Mock($uri, $body, $headers);
104        }
105        $response->code = $code;
106        $this->_responses[] = $response;
107    }
108
109}
110