From ad1e5a178216f8529c9b078792793633cb591709 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Mon, 14 Jul 2014 01:01:51 -0400 Subject: [PATCH] Add tests for Handler --- test/HandlerTest.php | 92 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 test/HandlerTest.php diff --git a/test/HandlerTest.php b/test/HandlerTest.php new file mode 100644 index 0000000..64a4b9c --- /dev/null +++ b/test/HandlerTest.php @@ -0,0 +1,92 @@ +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"]; + } +}