1 : <?php
2 : /**
3 : * Mock adapter intended for testing
4 : *
5 : * PHP version 5
6 : *
7 : * LICENSE:
8 : *
9 : * Copyright (c) 2008-2011, Alexey Borzov <avb@php.net>
10 : * All rights reserved.
11 : *
12 : * Redistribution and use in source and binary forms, with or without
13 : * modification, are permitted provided that the following conditions
14 : * are met:
15 : *
16 : * * Redistributions of source code must retain the above copyright
17 : * notice, this list of conditions and the following disclaimer.
18 : * * Redistributions in binary form must reproduce the above copyright
19 : * notice, this list of conditions and the following disclaimer in the
20 : * documentation and/or other materials provided with the distribution.
21 : * * The names of the authors may not be used to endorse or promote products
22 : * derived from this software without specific prior written permission.
23 : *
24 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 : * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 : * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 : * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 : * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 : * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 : * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
32 : * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 : * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 : * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 : *
36 : * @category HTTP
37 : * @package HTTP_Request2
38 : * @author Alexey Borzov <avb@php.net>
39 : * @license http://opensource.org/licenses/bsd-license.php New BSD License
40 : * @version SVN: $Id: Mock.php 308322 2011-02-14 13:58:03Z avb $
41 : * @link http://pear.php.net/package/HTTP_Request2
42 : */
43 :
44 : /**
45 : * Base class for HTTP_Request2 adapters
46 : */
47 : require_once 'HTTP/Request2/Adapter.php';
48 :
49 : /**
50 : * Mock adapter intended for testing
51 : *
52 : * Can be used to test applications depending on HTTP_Request2 package without
53 : * actually performing any HTTP requests. This adapter will return responses
54 : * previously added via addResponse()
55 : * <code>
56 : * $mock = new HTTP_Request2_Adapter_Mock();
57 : * $mock->addResponse("HTTP/1.1 ... ");
58 : *
59 : * $request = new HTTP_Request2();
60 : * $request->setAdapter($mock);
61 : *
62 : * // This will return the response set above
63 : * $response = $req->send();
64 : * </code>
65 : *
66 : * @category HTTP
67 : * @package HTTP_Request2
68 : * @author Alexey Borzov <avb@php.net>
69 : * @version Release: 2.0.0
70 : */
71 : class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter
72 : {
73 : /**
74 : * A queue of responses to be returned by sendRequest()
75 : * @var array
76 : */
77 : protected $responses = array();
78 :
79 : /**
80 : * Returns the next response from the queue built by addResponse()
81 : *
82 : * If the queue is empty it will return default empty response with status 400,
83 : * if an Exception object was added to the queue it will be thrown.
84 : *
85 : * @param HTTP_Request2
86 : * @return HTTP_Request2_Response
87 : * @throws Exception
88 : */
89 : public function sendRequest(HTTP_Request2 $request)
90 : {
91 10 : if (count($this->responses) > 0) {
92 10 : $response = array_shift($this->responses);
93 10 : if ($response instanceof HTTP_Request2_Response) {
94 10 : return $response;
95 : } else {
96 : // rethrow the exception
97 0 : $class = get_class($response);
98 0 : $message = $response->getMessage();
99 0 : $code = $response->getCode();
100 0 : throw new $class($message, $code);
101 : }
102 : } else {
103 0 : return self::createResponseFromString("HTTP/1.1 400 Bad Request\r\n\r\n");
104 : }
105 : }
106 :
107 : /**
108 : * Adds response to the queue
109 : *
110 : * @param mixed either a string, a pointer to an open file,
111 : * an instance of HTTP_Request2_Response or Exception
112 : * @throws HTTP_Request2_Exception
113 : */
114 : public function addResponse($response)
115 : {
116 10 : if (is_string($response)) {
117 0 : $response = self::createResponseFromString($response);
118 10 : } elseif (is_resource($response)) {
119 10 : $response = self::createResponseFromFile($response);
120 10 : } elseif (!$response instanceof HTTP_Request2_Response &&
121 : !$response instanceof Exception
122 0 : ) {
123 0 : throw new HTTP_Request2_Exception('Parameter is not a valid response');
124 : }
125 10 : $this->responses[] = $response;
126 10 : }
127 :
128 : /**
129 : * Creates a new HTTP_Request2_Response object from a string
130 : *
131 : * @param string
132 : * @return HTTP_Request2_Response
133 : * @throws HTTP_Request2_Exception
134 : */
135 : public static function createResponseFromString($str)
136 : {
137 0 : $parts = preg_split('!(\r?\n){2}!m', $str, 2);
138 0 : $headerLines = explode("\n", $parts[0]);
139 0 : $response = new HTTP_Request2_Response(array_shift($headerLines));
140 0 : foreach ($headerLines as $headerLine) {
141 0 : $response->parseHeaderLine($headerLine);
142 0 : }
143 0 : $response->parseHeaderLine('');
144 0 : if (isset($parts[1])) {
145 0 : $response->appendBody($parts[1]);
146 0 : }
147 0 : return $response;
148 : }
149 :
150 : /**
151 : * Creates a new HTTP_Request2_Response object from a file
152 : *
153 : * @param resource file pointer returned by fopen()
154 : * @return HTTP_Request2_Response
155 : * @throws HTTP_Request2_Exception
156 : */
157 : public static function createResponseFromFile($fp)
158 : {
159 10 : $response = new HTTP_Request2_Response(fgets($fp));
160 : do {
161 10 : $headerLine = fgets($fp);
162 10 : $response->parseHeaderLine($headerLine);
163 10 : } while ('' != trim($headerLine));
164 :
165 10 : while (!feof($fp)) {
166 10 : $response->appendBody(fread($fp, 8192));
167 10 : }
168 10 : return $response;
169 : }
170 : }
|