3 * base include file for SimpleTest
5 * @subpackage MockObjects
10 * include SimpleTest files
12 require_once(dirname(__FILE__
) . '/compatibility.php');
16 * Stashes an error for later. Useful for constructors
17 * until PHP gets exceptions.
19 * @subpackage WebTester
21 class SimpleStickyError
{
22 var $_error = 'Constructor not chained';
25 * Sets the error to empty.
28 function SimpleStickyError() {
33 * Test for an outstanding error.
34 * @return boolean True if there is an error.
38 return ($this->_error
!= '');
42 * Accessor for an outstanding error.
43 * @return string Empty string if no error otherwise
52 * Sets the internal error.
53 * @param string Error message to stash.
56 function _setError($error) {
57 $this->_error
= $error;
61 * Resets the error state to no error.
64 function _clearError() {
70 * Wrapper for TCP/IP socket.
72 * @subpackage WebTester
74 class SimpleSocket
extends SimpleStickyError
{
76 var $_is_open = false;
81 * Opens a socket for reading and writing.
82 * @param string $host Hostname to send request to.
83 * @param integer $port Port on remote machine to open.
84 * @param integer $timeout Connection timeout in seconds.
85 * @param integer $block_size Size of chunk to read.
88 function SimpleSocket($host, $port, $timeout, $block_size = 255) {
89 $this->SimpleStickyError();
90 if (! ($this->_handle
= $this->_openSocket($host, $port, $error_number, $error, $timeout))) {
91 $this->_setError("Cannot open [$host:$port] with [$error] within [$timeout] seconds");
94 $this->_is_open
= true;
95 $this->_block_size
= $block_size;
96 SimpleTestCompatibility
::setTimeout($this->_handle
, $timeout);
100 * Writes some data to the socket and saves alocal copy.
101 * @param string $message String to send to socket.
102 * @return boolean True if successful.
105 function write($message) {
106 if ($this->isError() ||
! $this->isOpen()) {
109 $count = fwrite($this->_handle
, $message);
111 if ($count === false) {
112 $this->_setError('Cannot write to socket');
117 fflush($this->_handle
);
118 $this->_sent
.= $message;
123 * Reads data from the socket. The error suppresion
124 * is a workaround for PHP4 always throwing a warning
125 * with a secure socket.
126 * @return integer/boolean Incoming bytes. False
131 if ($this->isError() ||
! $this->isOpen()) {
134 $raw = @fread
($this->_handle
, $this->_block_size
);
135 if ($raw === false) {
136 $this->_setError('Cannot read from socket');
143 * Accessor for socket open state.
144 * @return boolean True if open.
148 return $this->_is_open
;
152 * Closes the socket preventing further reads.
153 * Cannot be reopened once closed.
154 * @return boolean True if successful.
158 $this->_is_open
= false;
159 return fclose($this->_handle
);
163 * Accessor for content so far.
164 * @return string Bytes sent only.
172 * Actually opens the low level socket.
173 * @param string $host Host to connect to.
174 * @param integer $port Port on host.
175 * @param integer $error_number Recipient of error code.
176 * @param string $error Recipoent of error message.
177 * @param integer $timeout Maximum time to wait for connection.
180 function _openSocket($host, $port, &$error_number, &$error, $timeout) {
181 return @fsockopen
($host, $port, $error_number, $error, $timeout);
186 * Wrapper for TCP/IP socket over TLS.
187 * @package SimpleTest
188 * @subpackage WebTester
190 class SimpleSecureSocket
extends SimpleSocket
{
193 * Opens a secure socket for reading and writing.
194 * @param string $host Hostname to send request to.
195 * @param integer $port Port on remote machine to open.
196 * @param integer $timeout Connection timeout in seconds.
199 function SimpleSecureSocket($host, $port, $timeout) {
200 $this->SimpleSocket($host, $port, $timeout);
204 * Actually opens the low level socket.
205 * @param string $host Host to connect to.
206 * @param integer $port Port on host.
207 * @param integer $error_number Recipient of error code.
208 * @param string $error Recipient of error message.
209 * @param integer $timeout Maximum time to wait for connection.
212 function _openSocket($host, $port, &$error_number, &$error, $timeout) {
213 return parent
::_openSocket("tls://$host", $port, $error_number, $error, $timeout);