Skip to content

Commit

Permalink
RequestHandler tests use guzzle HandlerStack
Browse files Browse the repository at this point in the history
Don't write to raw memory, instead use Guzzle's facilities for mocking
handlers and responses.
  • Loading branch information
ceyko committed Jan 23, 2017
1 parent 9991dee commit 1b123b8
Showing 1 changed file with 16 additions and 33 deletions.
49 changes: 16 additions & 33 deletions test/RequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,57 +32,40 @@ public function testRequestThrowsErrorOnMalformedBaseUrl()

/**
* @expectedException Tumblr\API\RequestException
* @expectedExceptionCode 400
* @expectedExceptionMessage Sadface
*/
public function testRequestThrowsOnBadResponse()
{
// Response stream
$stream = fopen('php://memory','r+');
fwrite($stream, '{"meta": {"status": 400, "msg": "Sadface."} }');
rewind($stream);

// Hook up response to all requests
$response = (new GuzzleHttp\Psr7\Response)
->withStatus(400, 'Bad Request')
->withBody(new GuzzleHttp\Psr7\Stream($stream));

$this->guzzle = $this->getMock('\GuzzleHttp\Client', array('request'));
$this->guzzle->expects($this->any())
->method('request')
->will($this->returnValue($response));
// Setup mock handler and response
$mock = new GuzzleHttp\Handler\MockHandler([
new GuzzleHttp\Psr7\Response(400, [], '{"meta": {"status": 400, "msg": "Sadface"} }'),
]);
$stack = GuzzleHttp\HandlerStack::create($mock);
$guzzle = new GuzzleHttp\Client(['handler' => $stack]);

// Attached mocked guzzle client
$client = new Tumblr\API\Client(API_KEY);
$rh = $client->getRequestHandler();
$rh->client = $this->guzzle;
$client->getRequestHandler()->client = $guzzle;

// Throws because it got a 400 back
$client->getBlogInfo('ceyko.tumblr.com');
}

public function testRequestGetsJsonResponseField()
{
// Response stream
$stream = fopen('php://memory','r+');
fwrite($stream, '{"meta": {"status": 200, "msg": "OK"}, "response": "Response Text"}');
rewind($stream);

// Hook up response to all requests
$response = (new GuzzleHttp\Psr7\Response)
->withStatus(200, 'OK')
->withBody(new GuzzleHttp\Psr7\Stream($stream));

$this->guzzle = $this->getMock('\GuzzleHttp\Client', array('request'));
$this->guzzle->expects($this->any())
->method('request')
->will($this->returnValue($response));
// Setup mock handler and response
$mock = new GuzzleHttp\Handler\MockHandler([
new GuzzleHttp\Psr7\Response(200, [], '{"meta": {"status": 200, "msg": "OK"}, "response": "Response Text"}'),
]);
$stack = GuzzleHttp\HandlerStack::create($mock);
$guzzle = new GuzzleHttp\Client(['handler' => $stack]);

// Attached mocked guzzle client
$client = new Tumblr\API\Client(API_KEY);
$rh = $client->getRequestHandler();
$rh->client = $this->guzzle;
$client->getRequestHandler()->client = $guzzle;

// Parses out the `reponse` field in json on success
$this->assertEquals($client->getBlogInfo('ceyko.tumblr.com'), 'Response Text');
}

}

0 comments on commit 1b123b8

Please sign in to comment.