Вход Регистрация
Файл: concrete5.7.5.6/concrete/vendor/zendframework/zend-http/src/Client/Adapter/Test.php
Строк: 236
<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace ZendHttpClientAdapter;

use 
Traversable;
use 
ZendHttpResponse;
use 
ZendStdlibArrayUtils;

/**
 * A testing-purposes adapter.
 *
 * Should be used to test all components that rely on ZendHttpClient,
 * without actually performing an HTTP request. You should instantiate this
 * object manually, and then set it as the client's adapter. Then, you can
 * set the expected response using the setResponse() method.
 */
class Test implements AdapterInterface
{
    
/**
     * Parameters array
     *
     * @var array
     */
    
protected $config = array();

    
/**
     * Buffer of responses to be returned by the read() method.  Can be
     * set using setResponse() and addResponse().
     *
     * @var array
     */
    
protected $responses = array("HTTP/1.1 400 Bad Requestrnrn");

    
/**
     * Current position in the response buffer
     *
     * @var int
     */
    
protected $responseIndex 0;

    
/**
     * Whether or not the next request will fail with an exception
     *
     * @var bool
     */
    
protected $nextRequestWillFail false;

    
/**
     * Adapter constructor, currently empty. Config is set using setOptions()
     */
    
public function __construct()
    { }

    
/**
     * Set the nextRequestWillFail flag
     *
     * @param  bool $flag
     * @return ZendHttpClientAdapterTest
     */
    
public function setNextRequestWillFail($flag)
    {
        
$this->nextRequestWillFail = (bool) $flag;

        return 
$this;
    }

    
/**
     * Set the configuration array for the adapter
     *
     * @param  array|Traversable $options
     * @throws ExceptionInvalidArgumentException
     */
    
public function setOptions($options = array())
    {
        if (
$options instanceof Traversable) {
            
$options ArrayUtils::iteratorToArray($options);
        }

        if (! 
is_array($options)) {
            throw new 
ExceptionInvalidArgumentException(
                
'Array or Traversable object expected, got ' gettype($options)
            );
        }

        foreach (
$options as $k => $v) {
            
$this->config[strtolower($k)] = $v;
        }
    }


    
/**
     * Connect to the remote server
     *
     * @param  string $host
     * @param  int    $port
     * @param  bool   $secure
     * @throws ExceptionRuntimeException
     */
    
public function connect($host$port 80$secure false)
    {
        if (
$this->nextRequestWillFail) {
            
$this->nextRequestWillFail false;
            throw new 
ExceptionRuntimeException('Request failed');
        }
    }

    
/**
     * Send request to the remote server
     *
     * @param string        $method
     * @param ZendUriUri $uri
     * @param string        $httpVer
     * @param array         $headers
     * @param string        $body
     * @return string Request as string
     */
    
public function write($method$uri$httpVer '1.1'$headers = array(), $body '')
    {
        
$host $uri->getHost();
            
$host = (strtolower($uri->getScheme()) == 'https' 'sslv2://' $host $host);

        
// Build request headers
        
$path $uri->getPath();
        if (empty(
$path)) {
            
$path '/';
        }
        if (
$uri->getQuery()) $path .= '?' $uri->getQuery();
        
$request "{$method} {$path} HTTP/{$httpVer}rn";
        foreach (
$headers as $k => $v) {
            if (
is_string($k)) $v ucfirst($k) . ": $v";
            
$request .= "$vrn";
        }

        
// Add the request body
        
$request .= "rn" $body;

        
// Do nothing - just return the request as string

        
return $request;
    }

    
/**
     * Return the response set in $this->setResponse()
     *
     * @return string
     */
    
public function read()
    {
        if (
$this->responseIndex >= count($this->responses)) {
            
$this->responseIndex 0;
        }
        return 
$this->responses[$this->responseIndex++];
    }

    
/**
     * Close the connection (dummy)
     *
     */
    
public function close()
    { }

    
/**
     * Set the HTTP response(s) to be returned by this adapter
     *
     * @param ZendHttpResponse|array|string $response
     */
    
public function setResponse($response)
    {
        if (
$response instanceof Response) {
            
$response $response->toString();
        }

        
$this->responses = (array) $response;
        
$this->responseIndex 0;
    }

    
/**
     * Add another response to the response buffer.
     *
     * @param string|Response $response
     */
    
public function addResponse($response)
    {
         if (
$response instanceof Response) {
            
$response $response->toString();
         }

        
$this->responses[] = $response;
    }

    
/**
     * Sets the position of the response buffer.  Selects which
     * response will be returned on the next call to read().
     *
     * @param int $index
     * @throws ExceptionOutOfRangeException
     */
    
public function setResponseIndex($index)
    {
        if (
$index || $index >= count($this->responses)) {
            throw new 
ExceptionOutOfRangeException(
                
'Index out of range of response buffer size');
        }
        
$this->responseIndex $index;
    }
}
Онлайн: 2
Реклама