Skip to content

Commit

Permalink
test ApiException
Browse files Browse the repository at this point in the history
  • Loading branch information
darthmaim committed May 26, 2015
1 parent 4e56c2b commit 4372255
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions tests/ApiExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use GuzzleHttp\Message\Response;
use GuzzleHttp\Message\ResponseInterface;
use GuzzleHttp\Stream\Stream;
use GW2Treasures\GW2Api\V2\ApiHandler;
use GW2Treasures\GW2Api\V2\IEndpoint;
use Stubs\EndpointStub;

class ApiExceptionTest extends TestCase {
protected function getEndpoint() {
return new EndpointStub( $this->api() );
}


/**
* @expectedException \GW2Treasures\GW2Api\Exception\ApiException
* @expectedExceptionMessage this is the error message.
*/
public function testMessage() {
$this->mockResponse( new Response(
400, [ 'Content-Type' => 'application/json; charset=utf-8' ],
Stream::factory( '{"text":"this is the error message."}' )
));

$this->getEndpoint()->test();
}

public function testResponse() {
$this->mockResponse( new Response(
400, [ 'Content-Type' => 'application/json; charset=utf-8', 'foo' => 'bar' ],
Stream::factory( '{"text":"this is the error message."}' )
));

try {
$this->getEndpoint()->test();
} catch( \GW2Treasures\GW2Api\Exception\ApiException $exception ) {
$this->assertNotNull( $exception->getResponse() );
$this->assertEquals( 'bar', $exception->getResponse()->getHeader('foo') );
$this->assertEquals( 400, $exception->getCode() );
$this->assertEquals( 400, $exception->getResponse()->getStatusCode() );

$this->assertStringStartsWith( $exception->getMessage(), $exception->__toString() );
$this->assertNotFalse( strstr( $exception->__toString(), 'status: 400' ));
}
}
}

0 comments on commit 4372255

Please sign in to comment.