diff --git a/src/GW2Api.php b/src/GW2Api.php index e6a5737..67dd53b 100644 --- a/src/GW2Api.php +++ b/src/GW2Api.php @@ -51,7 +51,18 @@ protected function getOptions( array $options = [] ) { ] + $options; } + /** + * @param string $handler + */ public function registerHandler( $handler ) { + if( is_null( $handler )) { + throw new \InvalidArgumentException( '$handler can\'t be null' ); + } + + if( !is_string( $handler )) { + throw new \InvalidArgumentException( '$handler has to be a string (class name of a valid ApiHandler)' ); + } + $handlerClass = new \ReflectionClass( $handler ); if( !$handlerClass->isSubclassOf( '\GW2Treasures\GW2Api\V2\ApiHandler' )) { throw new \InvalidArgumentException( '$handler has to be a ApiHandler'); diff --git a/tests/ApiHandlerTest.php b/tests/ApiHandlerTest.php index 066ce18..46e2528 100644 --- a/tests/ApiHandlerTest.php +++ b/tests/ApiHandlerTest.php @@ -39,6 +39,28 @@ public function testAsJson() { $invalidNoContentType = $this->makeResponse( '{"valid":false}', null ); $this->assertNull( $handler->responseAsJson( $invalidNoContentType )); } + + /** + * @expectedException \InvalidArgumentException + */ + public function testRegisterNull() { + $this->api()->registerHandler( null ); + } + + + /** + * @expectedException \InvalidArgumentException + */ + public function testRegisterSubclassOfHandler() { + $this->api()->registerHandler( new stdClass() ); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testRegisterHandler() { + $this->api()->registerHandler( $this->getHandler( $this->getEndpoint() ) ); + } } class TestHandler extends ApiHandler {