Skip to content

Commit

Permalink
Test coverage for callback as a body
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Kotek committed Sep 28, 2016
1 parent 0e79b79 commit 161dd98
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/HTTP/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,45 @@ function testStringBody() {

}

/**
* @expectedException \UnexpectedValueException
*/
function testCallbackBodyAsString() {

$body = $this->createCallback();

$message = new MessageMock();
$message->setBody($body);

$message->getBodyAsString();

}

/**
* @expectedException \UnexpectedValueException
*/
function testCallbackBodyAsStream() {

$body = $this->createCallback();

$message = new MessageMock();
$message->setBody($body);

$message->getBodyAsStream();

}

function testGetBodyWhenCallback() {

$body = $this->createCallback();

$message = new MessageMock();
$message->setBody($body);

$this->assertEquals($body, $message->getBody());

}

/**
* It's possible that streams contains more data than the Content-Length.
*
Expand Down Expand Up @@ -219,6 +258,15 @@ function testHasHeaders() {

}

private function createCallback()
{
return function() {
$fd = fopen('php://output', 'r+');
fwrite($fd, 'foo');
fclose($fd);
};
}

}

class MessageMock extends Message { }
21 changes: 21 additions & 0 deletions tests/HTTP/SapiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,25 @@ function testSendLimitedByContentLengthStream() {

}

/**
* @runInSeparateProcess
* @depends testSend
*/
function testSendWorksWithCallbackAsBody() {
$response = new Response(200, [], function() {
$fd = fopen('php://output', 'r+');
fwrite($fd, 'foo');
fclose($fd);
});

ob_start();

Sapi::sendResponse($response);

$result = ob_get_clean();

$this->assertEquals('foo', $result);

}

}

0 comments on commit 161dd98

Please sign in to comment.