1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 
<?php

namespace SAREhub\Commons\Http;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;

class HttpClientMockBuilder
{

    /** @var array */
    private $responseQueue = [];

    /**
     * @param Response $response
     * @return $this
     */
    public function response(Response $response)
    {
        $this->responseQueue[] = $response;
        return $this;
    }

    /**
     * @param RequestException $exception
     * @return $this
     */
    public function requestException(RequestException $exception)
    {
        $this->responseQueue[] = $exception;
        return $this;
    }

    /**
     * @return array
     */
    public function getResponseQueue()
    {
        return $this->responseQueue;
    }

    /**
     * @return Client
     */
    public function build()
    {
        return new Client([
            'handler' => HandlerStack::create(new MockHandler($this->responseQueue))
        ]);
    }
}