Skip to content

Commit 6d7ebe1

Browse files
committed
Response: do not send "Possible problem notice in CLI" [Closes #89]
1 parent 094bb8f commit 6d7ebe1

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

src/Http/Response.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,9 @@ public function deleteCookie($name, $path = NULL, $domain = NULL, $secure = NULL
287287

288288
private function checkHeaders()
289289
{
290-
if (headers_sent($file, $line)) {
290+
if (PHP_SAPI === 'cli') {
291+
292+
} elseif (headers_sent($file, $line)) {
291293
throw new Nette\InvalidStateException('Cannot send header after HTTP headers have been sent' . ($file ? " (output started at $file:$line)." : '.'));
292294

293295
} elseif ($this->warnOnBuffer && ob_get_length() && !array_filter(ob_get_status(TRUE), function ($i) { return !$i['chunk_size']; })) {

tests/Http/Response.error.phpt

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ use Tester\Assert;
1010

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

13-
if (PHP_SAPI === 'cli') {
14-
Tester\Environment::skip('Cookies are not available in CLI');
15-
}
16-
1713

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

2622

27-
Assert::error(function () use ($response) {
28-
ob_start(NULL, 4096);
29-
echo ' ';
30-
$response->setHeader('A', 'b');
31-
}, E_USER_NOTICE, 'Possible problem: you are sending a HTTP header while already having some data in output buffer%a%');
32-
33-
34-
$response->warnOnBuffer = FALSE;
35-
$response->setHeader('A', 'b');
36-
37-
38-
Assert::exception(function () use ($response) {
39-
ob_flush();
40-
$response->setHeader('A', 'b');
41-
}, Nette\InvalidStateException::class, 'Cannot send header after HTTP headers have been sent (output started at ' . __FILE__ . ':' . (__LINE__ - 2) . ').');
23+
if (PHP_SAPI === 'cli') {
24+
Assert::noError(function () use ($response) {
25+
ob_start(NULL, 4096);
26+
echo ' ';
27+
$response->setHeader('A', 'b');
28+
});
29+
30+
Assert::error(function () use ($response) {
31+
ob_flush();
32+
$response->setHeader('A', 'b');
33+
}, E_WARNING, 'Cannot modify header information - headers already sent by (output started at ' . __FILE__ . ':' . (__LINE__ - 2) . ')');
34+
35+
} else {
36+
Assert::error(function () use ($response) {
37+
ob_start(NULL, 4096);
38+
echo ' ';
39+
$response->setHeader('A', 'b');
40+
}, E_USER_NOTICE, 'Possible problem: you are sending a HTTP header while already having some data in output buffer%a%');
41+
42+
Assert::noError(function () use ($response) {
43+
$response->warnOnBuffer = FALSE;
44+
$response->setHeader('A', 'b');
45+
});
46+
47+
Assert::exception(function () use ($response) {
48+
ob_flush();
49+
$response->setHeader('A', 'b');
50+
}, Nette\InvalidStateException::class, 'Cannot send header after HTTP headers have been sent (output started at ' . __FILE__ . ':' . (__LINE__ - 2) . ').');
51+
}

0 commit comments

Comments
 (0)