Skip to content

Commit

Permalink
use callable typehint as php 5.6 already support it, add docblocks, a…
Browse files Browse the repository at this point in the history
…nd make expectException* message call immediate call before actual method call

Signed-off-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
  • Loading branch information
samsonasik committed Aug 12, 2020
1 parent e0e3732 commit 77ae913
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 27 deletions.
15 changes: 3 additions & 12 deletions src/PhpEnvironment/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Response extends HttpResponse
protected $contentSent = false;

/**
* @var callable
* @var null|callable
*/
private $headersSentHandler;

Expand Down Expand Up @@ -81,19 +81,10 @@ public function contentSent()
}

/**
* @param callable $handler
* @return void
*/
public function setHeadersSentHandler($handler)
public function setHeadersSentHandler(callable $handler)
{
if (! is_callable($handler)) {
throw new InvalidArgumentException(
sprintf(
'Handler must be callable with passed response object in invokable parameter, received %s',
gettype($handler)
)
);
}

$this->headersSentHandler = $handler;
}

Expand Down
19 changes: 4 additions & 15 deletions test/PhpEnvironment/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,16 @@ public function testSendHeadersHeadersNotAlreadySent()
$this->assertInstanceOf(Response::class, $response->sendHeaders());
}

public function testSendHeadersHeadersAlreadySentPassInvalidHandler()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'Handler must be callable with passed response object in invokable parameter, received string'
);

$response = new Response();
$response->setHeadersSentHandler('foo');
$response->sendHeaders();
}

public function testSendHeadersHeadersAlreadySentPassValidHandler()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Cannot send headers, headers already sent');

$response = new Response();
$response->setHeadersSentHandler(function ($response) {
throw new RuntimeException('Cannot send headers, headers already sent');
});

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Cannot send headers, headers already sent');

$response->sendHeaders();
}
}

0 comments on commit 77ae913

Please sign in to comment.