Skip to content

Commit

Permalink
test ApiHandler registration
Browse files Browse the repository at this point in the history
  • Loading branch information
darthmaim committed May 26, 2015
1 parent 4372255 commit c5f986d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/GW2Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
22 changes: 22 additions & 0 deletions tests/ApiHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c5f986d

Please sign in to comment.