Skip to content

Commit

Permalink
Response: do not send "Possible problem notice in CLI" [Closes #89]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jun 6, 2016
1 parent 094bb8f commit 6d7ebe1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
4 changes: 3 additions & 1 deletion src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ public function deleteCookie($name, $path = NULL, $domain = NULL, $secure = NULL

private function checkHeaders()
{
if (headers_sent($file, $line)) {
if (PHP_SAPI === 'cli') {

} elseif (headers_sent($file, $line)) {
throw new Nette\InvalidStateException('Cannot send header after HTTP headers have been sent' . ($file ? " (output started at $file:$line)." : '.'));

} elseif ($this->warnOnBuffer && ob_get_length() && !array_filter(ob_get_status(TRUE), function ($i) { return !$i['chunk_size']; })) {
Expand Down
48 changes: 29 additions & 19 deletions tests/Http/Response.error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

if (PHP_SAPI === 'cli') {
Tester\Environment::skip('Cookies are not available in CLI');
}


$response = new Http\Response;
$response->setHeader('A', 'b'); // no output
Expand All @@ -24,18 +20,32 @@ $response->setHeader('A', 'b'); // full buffer
ob_end_clean();


Assert::error(function () use ($response) {
ob_start(NULL, 4096);
echo ' ';
$response->setHeader('A', 'b');
}, E_USER_NOTICE, 'Possible problem: you are sending a HTTP header while already having some data in output buffer%a%');


$response->warnOnBuffer = FALSE;
$response->setHeader('A', 'b');


Assert::exception(function () use ($response) {
ob_flush();
$response->setHeader('A', 'b');
}, Nette\InvalidStateException::class, 'Cannot send header after HTTP headers have been sent (output started at ' . __FILE__ . ':' . (__LINE__ - 2) . ').');
if (PHP_SAPI === 'cli') {
Assert::noError(function () use ($response) {
ob_start(NULL, 4096);
echo ' ';
$response->setHeader('A', 'b');
});

Assert::error(function () use ($response) {
ob_flush();
$response->setHeader('A', 'b');
}, E_WARNING, 'Cannot modify header information - headers already sent by (output started at ' . __FILE__ . ':' . (__LINE__ - 2) . ')');

} else {
Assert::error(function () use ($response) {
ob_start(NULL, 4096);
echo ' ';
$response->setHeader('A', 'b');
}, E_USER_NOTICE, 'Possible problem: you are sending a HTTP header while already having some data in output buffer%a%');

Assert::noError(function () use ($response) {
$response->warnOnBuffer = FALSE;
$response->setHeader('A', 'b');
});

Assert::exception(function () use ($response) {
ob_flush();
$response->setHeader('A', 'b');
}, Nette\InvalidStateException::class, 'Cannot send header after HTTP headers have been sent (output started at ' . __FILE__ . ':' . (__LINE__ - 2) . ').');
}

0 comments on commit 6d7ebe1

Please sign in to comment.