Skip to content

Allow to easily extend the Mock client. #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 12 additions & 26 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use Http\Client\Common\HttpAsyncClientEmulator;
use Http\Client\Exception;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\ResponseFactory;
use Psr\Http\Message\RequestInterface;
Expand All @@ -20,39 +18,39 @@
*
* @author David de Boer <[email protected]>
*/
class Client implements HttpClient, HttpAsyncClient
class Client implements ClientInterface
{
use HttpAsyncClientEmulator;

/**
* @var ResponseFactory
*/
private $responseFactory;
protected $responseFactory;

/**
* @var RequestInterface[]
*/
private $requests = [];
protected $requests = [];

/**
* @var ResponseInterface[]
*/
private $responses = [];
protected $responses = [];

/**
* @var ResponseInterface|null
*/
private $defaultResponse;
protected $defaultResponse;

/**
* @var Exception[]
*/
private $exceptions = [];
protected $exceptions = [];

/**
* @var Exception|null
*/
private $defaultException;
protected $defaultException;

/**
* @param ResponseFactory|null $responseFactory
Expand Down Expand Up @@ -90,51 +88,39 @@ public function sendRequest(RequestInterface $request)
}

/**
* Adds an exception that will be thrown.
*
* @param \Exception $exception
* {@inheritdoc}
*/
public function addException(\Exception $exception)
{
$this->exceptions[] = $exception;
}

/**
* Sets the default exception to throw when the list of added exceptions and responses is exhausted.
*
* If both a default exception and a default response are set, the exception will be thrown.
*
* @param \Exception|null $defaultException
* {@inheritdoc}
*/
public function setDefaultException(\Exception $defaultException = null)
{
$this->defaultException = $defaultException;
}

/**
* Adds a response that will be returned.
*
* @param ResponseInterface $response
* {@inheritdoc}
*/
public function addResponse(ResponseInterface $response)
{
$this->responses[] = $response;
}

/**
* Sets the default response to be returned when the list of added exceptions and responses is exhausted.
*
* @param ResponseInterface|null $defaultResponse
* {@inheritdoc}
*/
public function setDefaultResponse(ResponseInterface $defaultResponse = null)
{
$this->defaultResponse = $defaultResponse;
}

/**
* Returns requests that were sent.
*
* @return RequestInterface[]
* {@inheritdoc}
*/
public function getRequests()
{
Expand Down
53 changes: 53 additions & 0 deletions src/ClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Http\Mock;

use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* HTTP Mock Client interface.
*
* @author Dezső Biczó <[email protected]>
*/
interface ClientInterface extends HttpClient, HttpAsyncClient
{
/**
* Adds an exception that will be thrown.
*
* @param \Exception $exception
*/
public function addException(\Exception $exception);

/**
* Sets the default exception to throw when the list of added exceptions and responses is exhausted.
*
* If both a default exception and a default response are set, the exception will be thrown.
*
* @param \Exception|null $defaultException
*/
public function setDefaultException(\Exception $defaultException = null);

/**
* Adds a response that will be returned.
*
* @param ResponseInterface $response
*/
public function addResponse(ResponseInterface $response);

/**
* Sets the default response to be returned when the list of added exceptions and responses is exhausted.
*
* @param ResponseInterface|null $defaultResponse
*/
public function setDefaultResponse(ResponseInterface $defaultResponse = null);

/**
* Returns requests that were sent.
*
* @return RequestInterface[]
*/
public function getRequests();
}