Skip to content

Instantly share code, notes, and snippets.

@williamespindola
Created July 11, 2017 02:53
Show Gist options
  • Save williamespindola/afc64db92ebf8a39fc3b58cf9756a7aa to your computer and use it in GitHub Desktop.
Save williamespindola/afc64db92ebf8a39fc3b58cf9756a7aa to your computer and use it in GitHub Desktop.
<?php
/**
* Project name.
*
* PHP version 7
*
* @category PHP
* @package [Project\Namespace]\Test
* @author William Espindola <oi@williamespindola.com.br>
* @copyright [Copy]
* @license [Licence]
* @link [Link]
*/
namespace Project\Namespace\Test;
use Exception;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
use [https://github.com/williamespindola/abstract-http-client-guzzle/blob/master/Client/]\GuzzleClient;
use GuzzleHttp\Exception\ClientException as GuzzleClientException;
use GuzzleHttp\Exception\ServerException as GuzzleServerException;
use GuzzleHttp\Exception\RequestException as GuzzleRequestException;
class TestCaseProcessRequest extends TestCase
{
public $mockClient;
public $mockResponse;
private $spy;
protected function setUp()
{
$this->mockClient = $this->getMockBuilder(GuzzleClient::class)
->disableOriginalConstructor()
->setMethods(['request', 'setOptions'])
->getMock();
$this->mockRequest = $this->getMockBuilder(Request::class)
->disableOriginalConstructor()
->setMethods(['getStatusCode', 'getBody', 'getContents'])
->getMock();
$this->mockRespose = $this->getMockBuilder(Response::class)
->disableOriginalConstructor()
->setMethods(['getStatusCode', 'getBody', 'getContents'])
->getMock();
$this->mockRespose
->expects($this->any())
->method('getStatusCode')
->willReturn(204);
$this->mockRespose
->expects($this->any())
->method('getBody')
->willReturn($this->mockRespose);
}
public function mockClientException(string $message)
{
$this->mockException(
404,
new GuzzleClientException(
$message,
$this->mockRequest,
$this->mockRespose
)
);
}
public function mockRequestException(string $message)
{
$this->mockException(
400,
new GuzzleRequestException(
$message,
$this->mockRequest,
$this->mockRespose
)
);
}
public function mockServerException(string $message)
{
$this->mockException(
500,
new GuzzleServerException(
$message,
$this->mockRequest,
$this->mockRespose
)
);
}
public function mockException(int $code, Exception $exception)
{
$this->mockRespose
->expects($this->any())
->method('getStatusCode')
->willReturn($code);
$this->mockClient
->expects($this->any())
->method('request')
->will($this->throwException($exception));
}
public function mockSuccessRequest(string $contents)
{
$this->mockRespose
->expects($this->any())
->method('getBody')
->willReturn($this->mockRespose);
$this->mockRespose
->expects($this->any())
->method('getContents')
->willReturn($contents);
$this->mockClient
->expects($this->spy = $this->any())
->method('request')
->willReturn($this->mockRespose);
}
public function getSpy()
{
return $this->spy;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment