Add tests for Handler

This commit is contained in:
PJ Dietz 2014-07-14 01:01:51 -04:00
parent 982e048b4f
commit ad1e5a1782
1 changed files with 92 additions and 0 deletions

92
test/HandlerTest.php Normal file
View File

@ -0,0 +1,92 @@
<?php
namespace pjdietz\WellRESTed\Test;
use pjdietz\WellRESTed\Exceptions\HttpExceptions\HttpException;
use pjdietz\WellRESTed\Exceptions\HttpExceptions\NotFoundException;
use pjdietz\WellRESTed\Handler;
class HandlerTest extends \PHPUnit_Framework_TestCase
{
public function testResponse()
{
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockHandler = $this->getMockForAbstractClass('\pjdietz\WellRESTed\Handler');
$this->assertNotNull($mockHandler->getResponse($mockRequest));
}
/**
* @dataProvider verbProvider
*/
public function testVerb($verb)
{
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getMethod')
->will($this->returnValue($verb));
$mockHandler = $this->getMockForAbstractClass('\pjdietz\WellRESTed\Handler');
$this->assertNotNull($mockHandler->getResponse($mockRequest));
}
public function verbProvider()
{
return [
["GET"],
["POST"],
["PUT"],
["DELETE"],
["HEAD"],
["PATCH"],
["OPTIONS"],
["NOTALLOWED"]
];
}
public function testHttException()
{
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getMethod')
->will($this->returnValue("GET"));
$handler = new ExceptionHandler();
$resp = $handler->getResponse($mockRequest);
$this->assertEquals(404, $resp->getStatusCode());
}
public function testAllowedMethods()
{
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getMethod')
->will($this->returnValue("OPTIONS"));
$mockHandler = $this->getMockForAbstractClass('\pjdietz\WellRESTed\Handler');
$mockHandler->expects($this->any())
->method('getAllowedMethods')
->will($this->returnValue(["GET","POST"]));
$handler = new OptionsHandler();
$resp = $handler->getResponse($mockRequest);
$this->assertEquals("GET, POST", $resp->getHeader("Allow"));
}
}
class ExceptionHandler extends Handler
{
protected function get()
{
throw new NotFoundException();
}
}
class OptionsHandler extends Handler
{
protected function getAllowedMethods()
{
return ["GET","POST"];
}
}