Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Http / Client / Adapter / Test.php
blob1be5b43cae9186e089ee3e8bbdbdeb9f4a166580
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Http\Client\Adapter;
12 use Traversable;
13 use Zend\Http\Response;
14 use Zend\Stdlib\ArrayUtils;
16 /**
17 * A testing-purposes adapter.
19 * Should be used to test all components that rely on Zend\Http\Client,
20 * without actually performing an HTTP request. You should instantiate this
21 * object manually, and then set it as the client's adapter. Then, you can
22 * set the expected response using the setResponse() method.
24 class Test implements AdapterInterface
26 /**
27 * Parameters array
29 * @var array
31 protected $config = array();
33 /**
34 * Buffer of responses to be returned by the read() method. Can be
35 * set using setResponse() and addResponse().
37 * @var array
39 protected $responses = array("HTTP/1.1 400 Bad Request\r\n\r\n");
41 /**
42 * Current position in the response buffer
44 * @var int
46 protected $responseIndex = 0;
48 /**
49 * Whether or not the next request will fail with an exception
51 * @var bool
53 protected $nextRequestWillFail = false;
55 /**
56 * Adapter constructor, currently empty. Config is set using setOptions()
58 public function __construct()
59 { }
61 /**
62 * Set the nextRequestWillFail flag
64 * @param bool $flag
65 * @return \Zend\Http\Client\Adapter\Test
67 public function setNextRequestWillFail($flag)
69 $this->nextRequestWillFail = (bool) $flag;
71 return $this;
74 /**
75 * Set the configuration array for the adapter
77 * @param array|Traversable $options
78 * @throws Exception\InvalidArgumentException
80 public function setOptions($options = array())
82 if ($options instanceof Traversable) {
83 $options = ArrayUtils::iteratorToArray($options);
86 if (! is_array($options)) {
87 throw new Exception\InvalidArgumentException(
88 'Array or Traversable object expected, got ' . gettype($options)
92 foreach ($options as $k => $v) {
93 $this->config[strtolower($k)] = $v;
98 /**
99 * Connect to the remote server
101 * @param string $host
102 * @param int $port
103 * @param bool $secure
104 * @throws Exception\RuntimeException
106 public function connect($host, $port = 80, $secure = false)
108 if ($this->nextRequestWillFail) {
109 $this->nextRequestWillFail = false;
110 throw new Exception\RuntimeException('Request failed');
115 * Send request to the remote server
117 * @param string $method
118 * @param \Zend\Uri\Uri $uri
119 * @param string $httpVer
120 * @param array $headers
121 * @param string $body
122 * @return string Request as string
124 public function write($method, $uri, $httpVer = '1.1', $headers = array(), $body = '')
126 $host = $uri->getHost();
127 $host = (strtolower($uri->getScheme()) == 'https' ? 'sslv2://' . $host : $host);
129 // Build request headers
130 $path = $uri->getPath();
131 if (empty($path)) {
132 $path = '/';
134 if ($uri->getQuery()) $path .= '?' . $uri->getQuery();
135 $request = "{$method} {$path} HTTP/{$httpVer}\r\n";
136 foreach ($headers as $k => $v) {
137 if (is_string($k)) $v = ucfirst($k) . ": $v";
138 $request .= "$v\r\n";
141 // Add the request body
142 $request .= "\r\n" . $body;
144 // Do nothing - just return the request as string
146 return $request;
150 * Return the response set in $this->setResponse()
152 * @return string
154 public function read()
156 if ($this->responseIndex >= count($this->responses)) {
157 $this->responseIndex = 0;
159 return $this->responses[$this->responseIndex++];
163 * Close the connection (dummy)
166 public function close()
170 * Set the HTTP response(s) to be returned by this adapter
172 * @param \Zend\Http\Response|array|string $response
174 public function setResponse($response)
176 if ($response instanceof Response) {
177 $response = $response->toString();
180 $this->responses = (array) $response;
181 $this->responseIndex = 0;
185 * Add another response to the response buffer.
187 * @param string|Response $response
189 public function addResponse($response)
191 if ($response instanceof Response) {
192 $response = $response->toString();
195 $this->responses[] = $response;
199 * Sets the position of the response buffer. Selects which
200 * response will be returned on the next call to read().
202 * @param int $index
203 * @throws Exception\OutOfRangeException
205 public function setResponseIndex($index)
207 if ($index < 0 || $index >= count($this->responses)) {
208 throw new Exception\OutOfRangeException(
209 'Index out of range of response buffer size');
211 $this->responseIndex = $index;