1: <?php
2: /**
3: * Copyright 2007-2012 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: */
19: class 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: * @since Horde_Http 1.1.0
60: *
61: * @param array $responses The responses to be added to the stack.
62: *
63: * @return NULL
64: */
65: public function addResponses($responses)
66: {
67: foreach ($responses as $response) {
68: if (is_string($response)) {
69: $this->addResponse($response);
70: }
71: if (is_array($response)) {
72: $this->addResponse(
73: isset($response['body']) ? $response['body'] : '',
74: isset($response['code']) ? $response['code'] : 200,
75: isset($response['uri']) ? $response['uri'] : '',
76: isset($response['headers']) ? $response['headers'] : array()
77: );
78: }
79: }
80: }
81:
82: /**
83: * Adds a response to the stack of responses.
84: *
85: * @since Horde_Http 1.1.0
86: *
87: * @param string|resourse $body The response body content.
88: * @param string $code The response code.
89: * @param string $uri The request uri.
90: * @param array $headers Response headers. This can be one string
91: * representing the whole header or an array
92: * of strings with one string per header
93: * line.
94: *
95: * @return Horde_Http_Response_Mock The response.
96: */
97: public function addResponse(
98: $body, $code = 200, $uri = '', $headers = array()
99: )
100: {
101: if (is_string($body)) {
102: $stream = new Horde_Support_StringStream($body);
103: $response = new Horde_Http_Response_Mock(
104: $uri, $stream->fopen(), $headers
105: );
106: } else {
107: $response = new Horde_Http_Response_Mock($uri, $body, $headers);
108: }
109: $response->code = $code;
110: $this->_responses[] = $response;
111: }
112:
113: }
114: