Update tests for Client and Handler

This commit is contained in:
PJ Dietz 2015-02-21 14:51:42 -05:00
parent 5dacb232ec
commit d4ad282abc
4 changed files with 78 additions and 125 deletions

View File

@ -4,7 +4,7 @@
* pjdietz\WellRESTed\Client
*
* @author PJ Dietz <pj@pjdietz.com>
* @copyright Copyright 2014 by PJ Dietz
* @copyright Copyright 2015 by PJ Dietz
* @license MIT
*/

View File

@ -10,7 +10,7 @@ class ApacheRequestHeadersTest extends \PHPUnit_Framework_TestCase
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testReadApacheRequestHeaders()
public function testReadsApacheRequestHeaders()
{
if (!function_exists('apache_request_headers')) {
function apache_request_headers() {

View File

@ -7,12 +7,15 @@ use pjdietz\ShamServer\ShamServer;
use pjdietz\WellRESTed\Client;
use pjdietz\WellRESTed\Request;
/**
* @covers pjdietz\WellRESTed\Client
*/
class ClientTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider httpMethodProvider
*/
public function testSendHttpMethod($method)
public function testSendsHttpMethod($method)
{
$host = "localhost";
$port = $this->getRandomNumberInRange(getenv("PORT"));
@ -20,22 +23,15 @@ class ClientTest extends \PHPUnit_Framework_TestCase
$server = new ShamServer($host, $port, $script);
$rqst = $this->getMockBuilder('pjdietz\WellRESTed\Interfaces\RequestInterface')->getMock();
$rqst->expects($this->any())
->method("getUri")
->will($this->returnValue("http://$host:$port"));
$rqst->expects($this->any())
->method("getMethod")
->will($this->returnValue($method));
$rqst->expects($this->any())
->method("getPort")
->will($this->returnValue($port));
$rqst->expects($this->any())
->method("getHeaders")
->will($this->returnValue(array()));
$rqst = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
$rqst->getUri()->willReturn("http://$host:$port");
$rqst->getMethod()->willReturn($method);
$rqst->getPort()->willReturn($port);
$rqst->getHeaders()->willReturn([]);
$rqst->getBody()->willReturn(null);
$client = new Client();
$resp = $client->request($rqst);
$resp = $client->request($rqst->reveal());
$body = trim($resp->getBody());
$this->assertEquals($method, $body);
@ -57,7 +53,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider httpHeaderProvider
*/
public function testSendHttpHeaders($headerKey, $headerValue)
public function testSendsHttpHeaders($headerKey, $headerValue)
{
$host = "localhost";
$port = $this->getRandomNumberInRange(getenv("PORT"));
@ -65,22 +61,15 @@ class ClientTest extends \PHPUnit_Framework_TestCase
$server = new ShamServer($host, $port, $script);
$rqst = $this->getMockBuilder('pjdietz\WellRESTed\Interfaces\RequestInterface')->getMock();
$rqst->expects($this->any())
->method("getUri")
->will($this->returnValue("http://$host:$port"));
$rqst->expects($this->any())
->method("getMethod")
->will($this->returnValue("GET"));
$rqst->expects($this->any())
->method("getPort")
->will($this->returnValue($port));
$rqst->expects($this->any())
->method("getHeaders")
->will($this->returnValue(array($headerKey => $headerValue)));
$rqst = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
$rqst->getUri()->willReturn("http://$host:$port");
$rqst->getMethod()->willReturn("GET");
$rqst->getPort()->willReturn($port);
$rqst->getHeaders()->willReturn([$headerKey => $headerValue]);
$rqst->getBody()->willReturn(null);
$client = new Client();
$resp = $client->request($rqst);
$resp = $client->request($rqst->reveal());
$headers = json_decode($resp->getBody());
$this->assertEquals($headerValue, $headers->{$headerKey});
@ -99,32 +88,22 @@ class ClientTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider bodyProvider
*/
public function testSendBody($body)
public function testSendsBody($body)
{
$host = "localhost";
$port = $this->getRandomNumberInRange(getenv("PORT"));
$script = realpath(__DIR__ . "/sham-routers/body.php");
$server = new ShamServer($host, $port, $script);
$rqst = $this->getMockBuilder('pjdietz\WellRESTed\Interfaces\RequestInterface')->getMock();
$rqst->expects($this->any())
->method("getUri")
->will($this->returnValue("http://$host:$port"));
$rqst->expects($this->any())
->method("getMethod")
->will($this->returnValue("POST"));
$rqst->expects($this->any())
->method("getPort")
->will($this->returnValue($port));
$rqst->expects($this->any())
->method("getHeaders")
->will($this->returnValue(array()));
$rqst->expects($this->any())
->method("getBody")
->will($this->returnValue($body));
$rqst = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
$rqst->getUri()->willReturn("http://$host:$port");
$rqst->getMethod()->willReturn("POST");
$rqst->getPort()->willReturn($port);
$rqst->getHeaders()->willReturn([]);
$rqst->getBody()->willReturn($body);
$client = new Client();
$resp = $client->request($rqst);
$resp = $client->request($rqst->reveal());
$this->assertEquals($body, $resp->getBody());
$server->stop();
}
@ -142,7 +121,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider formProvider
*/
public function testSendForm($form)
public function testSendsForm($form)
{
$host = "localhost";
$port = $this->getRandomNumberInRange(getenv("PORT"));
@ -175,60 +154,46 @@ class ClientTest extends \PHPUnit_Framework_TestCase
];
}
public function testSetCustomCurlOptionsOnInstantiation()
public function testSetsCustomCurlOptionsOnInstantiation()
{
$host = "localhost";
$port = $this->getRandomNumberInRange(getenv("PORT"));
$script = realpath(__DIR__ . "/sham-routers/headers.php");
$server = new ShamServer($host, $port, $script);
$rqst = $this->getMockBuilder('pjdietz\WellRESTed\Interfaces\RequestInterface')->getMock();
$rqst->expects($this->any())
->method("getUri")
->will($this->returnValue("http://$host:$port"));
$rqst->expects($this->any())
->method("getMethod")
->will($this->returnValue("GET"));
$rqst->expects($this->any())
->method("getPort")
->will($this->returnValue($port));
$rqst->expects($this->any())
->method("getHeaders")
->will($this->returnValue(array()));
$rqst = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
$rqst->getUri()->willReturn("http://$host:$port");
$rqst->getMethod()->willReturn("GET");
$rqst->getPort()->willReturn($port);
$rqst->getHeaders()->willReturn([]);
$rqst->getBody()->willReturn(null);
$cookieValue = "key=value";
$client = new Client([CURLOPT_COOKIE => $cookieValue]);
$resp = $client->request($rqst);
$resp = $client->request($rqst->reveal());
$headers = json_decode($resp->getBody());
$this->assertEquals($cookieValue, $headers->Cookie);
$server->stop();
}
public function testSetCustomCurlOptionsOnRequest()
public function testSetsCustomCurlOptionsOnRequest()
{
$host = "localhost";
$port = $this->getRandomNumberInRange(getenv("PORT"));
$script = realpath(__DIR__ . "/sham-routers/headers.php");
$server = new ShamServer($host, $port, $script);
$rqst = $this->getMockBuilder('pjdietz\WellRESTed\Interfaces\RequestInterface')->getMock();
$rqst->expects($this->any())
->method("getUri")
->will($this->returnValue("http://$host:$port"));
$rqst->expects($this->any())
->method("getMethod")
->will($this->returnValue("GET"));
$rqst->expects($this->any())
->method("getPort")
->will($this->returnValue($port));
$rqst->expects($this->any())
->method("getHeaders")
->will($this->returnValue(array()));
$rqst = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
$rqst->getUri()->willReturn("http://$host:$port");
$rqst->getMethod()->willReturn("GET");
$rqst->getPort()->willReturn($port);
$rqst->getHeaders()->willReturn([]);
$rqst->getBody()->willReturn(null);
$cookieValue = "key=value";
$client = new Client();
$resp = $client->request($rqst, [CURLOPT_COOKIE => $cookieValue]);
$resp = $client->request($rqst->reveal(), [CURLOPT_COOKIE => $cookieValue]);
$headers = json_decode($resp->getBody());
$this->assertEquals($cookieValue, $headers->Cookie);
@ -239,24 +204,17 @@ class ClientTest extends \PHPUnit_Framework_TestCase
* @dataProvider curlErrorProvider
* @expectedException \pjdietz\WellRESTed\Exceptions\CurlException
*/
public function testFailOnCurlError($uri, $opts)
public function testThrowsCurlException($uri, $opts)
{
$rqst = $this->getMockBuilder('pjdietz\WellRESTed\Interfaces\RequestInterface')->getMock();
$rqst->expects($this->any())
->method("getUri")
->will($this->returnValue($uri));
$rqst->expects($this->any())
->method("getMethod")
->will($this->returnValue("GET"));
$rqst->expects($this->any())
->method("getPort")
->will($this->returnValue(parse_url($uri, PHP_URL_PORT)));
$rqst->expects($this->any())
->method("getHeaders")
->will($this->returnValue(array()));
$rqst = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
$rqst->getUri()->willReturn($uri);
$rqst->getMethod()->willReturn("GET");
$rqst->getPort()->willReturn(parse_url($uri, PHP_URL_PORT));
$rqst->getHeaders()->willReturn([]);
$rqst->getBody()->willReturn(null);
$client = new Client();
$client->request($rqst, $opts);
$client->request($rqst->reveal(), $opts);
}
public function curlErrorProvider()

View File

@ -5,29 +5,29 @@ namespace pjdietz\WellRESTed\Test;
use pjdietz\WellRESTed\Exceptions\HttpExceptions\NotFoundException;
use pjdietz\WellRESTed\Handler;
/**
* @covers pjdietz\WellRESTed\Handler
*/
class HandlerTest extends \PHPUnit_Framework_TestCase
{
public function testGetResponse()
public function testReturnsResponse()
{
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockHandler = $this->getMockForAbstractClass('\pjdietz\WellRESTed\Handler');
/** @var \pjdietz\WellRESTed\Handler $mockHandler */
$this->assertNotNull($mockHandler->getResponse($mockRequest));
$request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
$handler = $this->getMockForAbstractClass("\\pjdietz\\WellRESTed\\Handler");
$response = $handler->getResponse($request->reveal());
$this->assertNotNull($response);
}
/**
* @dataProvider verbProvider
*/
public function testCallMethodForHttpVerb($verb)
public function testCallsMethodForHttpVerb($method)
{
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getMethod')
->will($this->returnValue($verb));
$mockHandler = $this->getMockForAbstractClass('\pjdietz\WellRESTed\Handler');
/** @var \pjdietz\WellRESTed\Handler $mockHandler */
$this->assertNotNull($mockHandler->getResponse($mockRequest));
$request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
$request->getMethod()->willReturn($method);
$handler = $this->getMockForAbstractClass("\\pjdietz\\WellRESTed\\Handler");
$response = $handler->getResponse($request->reveal());
$this->assertNotNull($response);
}
public function verbProvider()
@ -44,29 +44,24 @@ class HandlerTest extends \PHPUnit_Framework_TestCase
];
}
public function testTranslateHttpExceptionToResponse()
public function testTranslatesHttpExceptionToResponse()
{
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getMethod')
->will($this->returnValue("GET"));
$request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
$request->getMethod()->willReturn("GET");
$handler = new ExceptionHandler();
$resp = $handler->getResponse($mockRequest);
$this->assertEquals(404, $resp->getStatusCode());
$handler = new ExceptionHandler();
$response = $handler->getResponse($request->reveal());
$this->assertEquals(404, $response->getStatusCode());
}
public function testReadAllowedMethods()
{
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getMethod')
->will($this->returnValue("OPTIONS"));
$request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
$request->getMethod()->willReturn("OPTIONS");
$handler = new OptionsHandler();
$resp = $handler->getResponse($mockRequest);
$this->assertEquals("GET, POST", $resp->getHeader("Allow"));
$response = $handler->getResponse($request->reveal());
$this->assertEquals("GET, POST", $response->getHeader("Allow"));
}
}