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 * pjdietz\WellRESTed\Client
* *
* @author PJ Dietz <pj@pjdietz.com> * @author PJ Dietz <pj@pjdietz.com>
* @copyright Copyright 2014 by PJ Dietz * @copyright Copyright 2015 by PJ Dietz
* @license MIT * @license MIT
*/ */

View File

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

View File

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

View File

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