From 41894ffd3c15c672a3c7c208b0102f3b411a4db4 Mon Sep 17 00:00:00 2001 From: undercloud Date: Sat, 30 Mar 2019 22:05:26 +0400 Subject: [PATCH 01/19] Catch headers send info https://www.php.net/manual/en/function.headers-sent.php --- src/Emitter/SapiEmitterTrait.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Emitter/SapiEmitterTrait.php b/src/Emitter/SapiEmitterTrait.php index 9a4a7be..81e2189 100644 --- a/src/Emitter/SapiEmitterTrait.php +++ b/src/Emitter/SapiEmitterTrait.php @@ -31,8 +31,9 @@ trait SapiEmitterTrait */ private function assertNoPreviousOutput() { - if (headers_sent()) { - throw EmitterException::forHeadersSent(); + $file = $line = null; + if (headers_sent($file, $line)) { + throw EmitterException::forHeadersSent($file, $line); } if (ob_get_level() > 0 && ob_get_length() > 0) { From af4c40ec872b47de26e1dd49e63ad5e10c517adc Mon Sep 17 00:00:00 2001 From: undercloud Date: Sat, 30 Mar 2019 22:10:10 +0400 Subject: [PATCH 02/19] EmitterException, detect output target --- src/Exception/EmitterException.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Exception/EmitterException.php b/src/Exception/EmitterException.php index a00f278..2e2bf07 100644 --- a/src/Exception/EmitterException.php +++ b/src/Exception/EmitterException.php @@ -13,9 +13,15 @@ class EmitterException extends RuntimeException implements ExceptionInterface { - public static function forHeadersSent() : self + public static function forHeadersSent(string $file, int $line) : self { - return new self('Unable to emit response; headers already sent'); + return new self( + sprintf( + 'Unable to emit response; headers already sent, output started at %s:%d', + $file, + $line + ) + ); } public static function forOutputSent() : self From ed0521ea7f092c0bffc1c0c328ed783db00bf8d7 Mon Sep 17 00:00:00 2001 From: undercloud Date: Sun, 31 Mar 2019 22:48:54 +0400 Subject: [PATCH 03/19] Update SapiEmitterTrait.php --- src/Emitter/SapiEmitterTrait.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Emitter/SapiEmitterTrait.php b/src/Emitter/SapiEmitterTrait.php index 81e2189..38735b5 100644 --- a/src/Emitter/SapiEmitterTrait.php +++ b/src/Emitter/SapiEmitterTrait.php @@ -31,7 +31,8 @@ trait SapiEmitterTrait */ private function assertNoPreviousOutput() { - $file = $line = null; + $file = null; + $line = null; if (headers_sent($file, $line)) { throw EmitterException::forHeadersSent($file, $line); } From f32e05d6736888ab7e94f32f0dde25b3fe67dd16 Mon Sep 17 00:00:00 2001 From: undercloud Date: Sun, 31 Mar 2019 23:54:20 +0400 Subject: [PATCH 04/19] Update SapiStreamEmitterTest.php --- test/Emitter/SapiStreamEmitterTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/Emitter/SapiStreamEmitterTest.php b/test/Emitter/SapiStreamEmitterTest.php index cb10f5c..52ec40f 100644 --- a/test/Emitter/SapiStreamEmitterTest.php +++ b/test/Emitter/SapiStreamEmitterTest.php @@ -58,6 +58,24 @@ public function testEmitCallbackStreamResponse() $this->assertSame('it works', ob_get_clean()); } + public function testAssertNoPreviousOutput() + { + $stream = new CallbackStream(function () { + return 'it works'; + }); + $response = (new Response()) + ->withStatus(200) + ->withBody($stream); + ob_start(); + echo 'Unexpected Output'; + try { + $this->emitter->emit($response); + } catch (Exception $e) { + $this->assertTrue($e instanceof EmitterException); + $this->assertEquals('',$e->getMessage()); + } + } + public function testDoesNotInjectContentLengthHeaderIfStreamSizeIsUnknown() { $stream = $this->prophesize(StreamInterface::class); From 3a8c0534d74497d6ad4602b6bce95341df764735 Mon Sep 17 00:00:00 2001 From: undercloud Date: Mon, 1 Apr 2019 00:02:32 +0400 Subject: [PATCH 05/19] Update SapiStreamEmitterTest.php --- test/Emitter/SapiStreamEmitterTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/Emitter/SapiStreamEmitterTest.php b/test/Emitter/SapiStreamEmitterTest.php index 52ec40f..72187ef 100644 --- a/test/Emitter/SapiStreamEmitterTest.php +++ b/test/Emitter/SapiStreamEmitterTest.php @@ -73,6 +73,8 @@ public function testAssertNoPreviousOutput() } catch (Exception $e) { $this->assertTrue($e instanceof EmitterException); $this->assertEquals('',$e->getMessage()); + } finally { + ob_get_clean(); } } From 2588ca32fa817df7c9ca3d4038ee52cb023a7fc2 Mon Sep 17 00:00:00 2001 From: undercloud Date: Mon, 1 Apr 2019 08:23:13 +0400 Subject: [PATCH 06/19] Update SapiStreamEmitterTest.php --- test/Emitter/SapiStreamEmitterTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Emitter/SapiStreamEmitterTest.php b/test/Emitter/SapiStreamEmitterTest.php index 72187ef..a1c74d0 100644 --- a/test/Emitter/SapiStreamEmitterTest.php +++ b/test/Emitter/SapiStreamEmitterTest.php @@ -67,11 +67,11 @@ public function testAssertNoPreviousOutput() ->withStatus(200) ->withBody($stream); ob_start(); - echo 'Unexpected Output'; + echo 'Unexpected Output'; try { $this->emitter->emit($response); - } catch (Exception $e) { - $this->assertTrue($e instanceof EmitterException); + } catch (\Throwable $e) { + $this->assertTrue($e instanceof \EmitterException); $this->assertEquals('',$e->getMessage()); } finally { ob_get_clean(); From 913f553671409bc433048529c18adb84521d7a36 Mon Sep 17 00:00:00 2001 From: undercloud Date: Mon, 1 Apr 2019 08:27:16 +0400 Subject: [PATCH 07/19] Update SapiStreamEmitterTest.php --- test/Emitter/SapiStreamEmitterTest.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/Emitter/SapiStreamEmitterTest.php b/test/Emitter/SapiStreamEmitterTest.php index a1c74d0..5e2b74f 100644 --- a/test/Emitter/SapiStreamEmitterTest.php +++ b/test/Emitter/SapiStreamEmitterTest.php @@ -71,8 +71,12 @@ public function testAssertNoPreviousOutput() try { $this->emitter->emit($response); } catch (\Throwable $e) { - $this->assertTrue($e instanceof \EmitterException); - $this->assertEquals('',$e->getMessage()); + $this->assertTrue($e instanceof EmitterException); + $this->assertEquals($e->getMessage(), sprintf( + 'Unable to emit response; headers already sent, output started at %s:%d', + __FILE__, + __LINE__ - 5 + )); } finally { ob_get_clean(); } From 52829552ee55479ced44dce41e024b54c73b7651 Mon Sep 17 00:00:00 2001 From: undercloud Date: Mon, 1 Apr 2019 08:30:29 +0400 Subject: [PATCH 08/19] Update SapiStreamEmitterTest.php --- test/Emitter/SapiStreamEmitterTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/test/Emitter/SapiStreamEmitterTest.php b/test/Emitter/SapiStreamEmitterTest.php index 5e2b74f..72f5b0e 100644 --- a/test/Emitter/SapiStreamEmitterTest.php +++ b/test/Emitter/SapiStreamEmitterTest.php @@ -71,6 +71,7 @@ public function testAssertNoPreviousOutput() try { $this->emitter->emit($response); } catch (\Throwable $e) { + var_dump($e,$e->getMessage()); $this->assertTrue($e instanceof EmitterException); $this->assertEquals($e->getMessage(), sprintf( 'Unable to emit response; headers already sent, output started at %s:%d', From f66f198135ada0ea6ffd6036fb6ff4039b12dd1b Mon Sep 17 00:00:00 2001 From: undercloud Date: Mon, 1 Apr 2019 08:33:44 +0400 Subject: [PATCH 09/19] Update SapiStreamEmitterTest.php --- test/Emitter/SapiStreamEmitterTest.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/test/Emitter/SapiStreamEmitterTest.php b/test/Emitter/SapiStreamEmitterTest.php index 72f5b0e..4a24f16 100644 --- a/test/Emitter/SapiStreamEmitterTest.php +++ b/test/Emitter/SapiStreamEmitterTest.php @@ -66,20 +66,21 @@ public function testAssertNoPreviousOutput() $response = (new Response()) ->withStatus(200) ->withBody($stream); + $this->expectException(EmitterException::class); + $this->expectExceptionMessage('Unable to emit response; headers already sent'); + $this->expectExceptionMessage(sprintf( + 'output started at %s:%d', + __FILE__, + __LINE__ + 3 + )); ob_start(); - echo 'Unexpected Output'; + echo 'Unexpected Output'; try { $this->emitter->emit($response); } catch (\Throwable $e) { - var_dump($e,$e->getMessage()); - $this->assertTrue($e instanceof EmitterException); - $this->assertEquals($e->getMessage(), sprintf( - 'Unable to emit response; headers already sent, output started at %s:%d', - __FILE__, - __LINE__ - 5 - )); + throw $e; } finally { - ob_get_clean(); + ob_end_clean(); } } From 483676c197fca066d4d6b76b25efe53a82045ced Mon Sep 17 00:00:00 2001 From: undercloud Date: Mon, 1 Apr 2019 10:59:35 +0400 Subject: [PATCH 10/19] Update SapiStreamEmitterTest.php --- test/Emitter/SapiStreamEmitterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Emitter/SapiStreamEmitterTest.php b/test/Emitter/SapiStreamEmitterTest.php index 4a24f16..11ccf77 100644 --- a/test/Emitter/SapiStreamEmitterTest.php +++ b/test/Emitter/SapiStreamEmitterTest.php @@ -66,7 +66,7 @@ public function testAssertNoPreviousOutput() $response = (new Response()) ->withStatus(200) ->withBody($stream); - $this->expectException(EmitterException::class); + $this->expectException('Zend\HttpHandlerRunner\Exception\EmitterException'); $this->expectExceptionMessage('Unable to emit response; headers already sent'); $this->expectExceptionMessage(sprintf( 'output started at %s:%d', From 358e48c69588f13d5d06b4496a97f65b095878cf Mon Sep 17 00:00:00 2001 From: undercloud Date: Mon, 1 Apr 2019 11:02:40 +0400 Subject: [PATCH 11/19] Update SapiStreamEmitterTest.php --- test/Emitter/SapiStreamEmitterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Emitter/SapiStreamEmitterTest.php b/test/Emitter/SapiStreamEmitterTest.php index 11ccf77..16a9043 100644 --- a/test/Emitter/SapiStreamEmitterTest.php +++ b/test/Emitter/SapiStreamEmitterTest.php @@ -67,7 +67,7 @@ public function testAssertNoPreviousOutput() ->withStatus(200) ->withBody($stream); $this->expectException('Zend\HttpHandlerRunner\Exception\EmitterException'); - $this->expectExceptionMessage('Unable to emit response; headers already sent'); + //$this->expectExceptionMessage('Unable to emit response; headers already sent'); $this->expectExceptionMessage(sprintf( 'output started at %s:%d', __FILE__, From 023fbb42c3f494aea0888053fd18076c53002a95 Mon Sep 17 00:00:00 2001 From: undercloud Date: Mon, 1 Apr 2019 11:13:09 +0400 Subject: [PATCH 12/19] Update SapiStreamEmitterTest.php --- test/Emitter/SapiStreamEmitterTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/Emitter/SapiStreamEmitterTest.php b/test/Emitter/SapiStreamEmitterTest.php index 16a9043..0669d27 100644 --- a/test/Emitter/SapiStreamEmitterTest.php +++ b/test/Emitter/SapiStreamEmitterTest.php @@ -67,12 +67,14 @@ public function testAssertNoPreviousOutput() ->withStatus(200) ->withBody($stream); $this->expectException('Zend\HttpHandlerRunner\Exception\EmitterException'); - //$this->expectExceptionMessage('Unable to emit response; headers already sent'); + $this->expectExceptionMessage('Unable to emit response; headers already sent'); + /* $this->expectExceptionMessage(sprintf( 'output started at %s:%d', __FILE__, __LINE__ + 3 )); + */ ob_start(); echo 'Unexpected Output'; try { From b86ffdf9a41011bbeb489173ed180191cda54079 Mon Sep 17 00:00:00 2001 From: undercloud Date: Mon, 1 Apr 2019 11:18:08 +0400 Subject: [PATCH 13/19] Update SapiStreamEmitterTest.php --- test/Emitter/SapiStreamEmitterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Emitter/SapiStreamEmitterTest.php b/test/Emitter/SapiStreamEmitterTest.php index 0669d27..2da33be 100644 --- a/test/Emitter/SapiStreamEmitterTest.php +++ b/test/Emitter/SapiStreamEmitterTest.php @@ -67,7 +67,7 @@ public function testAssertNoPreviousOutput() ->withStatus(200) ->withBody($stream); $this->expectException('Zend\HttpHandlerRunner\Exception\EmitterException'); - $this->expectExceptionMessage('Unable to emit response; headers already sent'); + //$this->expectExceptionMessage('Unable to emit response; headers already sent'); /* $this->expectExceptionMessage(sprintf( 'output started at %s:%d', From 7480c4fdfffc24b160e665d1f166873ff38e2558 Mon Sep 17 00:00:00 2001 From: undercloud Date: Mon, 1 Apr 2019 11:21:34 +0400 Subject: [PATCH 14/19] Update SapiStreamEmitterTest.php --- test/Emitter/SapiStreamEmitterTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/Emitter/SapiStreamEmitterTest.php b/test/Emitter/SapiStreamEmitterTest.php index 2da33be..1c0e2cf 100644 --- a/test/Emitter/SapiStreamEmitterTest.php +++ b/test/Emitter/SapiStreamEmitterTest.php @@ -68,24 +68,24 @@ public function testAssertNoPreviousOutput() ->withBody($stream); $this->expectException('Zend\HttpHandlerRunner\Exception\EmitterException'); //$this->expectExceptionMessage('Unable to emit response; headers already sent'); - /* + $this->expectExceptionMessage(sprintf( 'output started at %s:%d', __FILE__, __LINE__ + 3 )); - */ - ob_start(); + + //ob_start(); echo 'Unexpected Output'; try { $this->emitter->emit($response); } catch (\Throwable $e) { throw $e; } finally { - ob_end_clean(); + //ob_end_clean(); } } - + public function testDoesNotInjectContentLengthHeaderIfStreamSizeIsUnknown() { $stream = $this->prophesize(StreamInterface::class); From 8d0f3ade8d0cb43dce5b3d0305a74bec7ed58dac Mon Sep 17 00:00:00 2001 From: undercloud Date: Mon, 1 Apr 2019 11:23:50 +0400 Subject: [PATCH 15/19] Update SapiStreamEmitterTest.php --- test/Emitter/SapiStreamEmitterTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Emitter/SapiStreamEmitterTest.php b/test/Emitter/SapiStreamEmitterTest.php index 1c0e2cf..1a4eb72 100644 --- a/test/Emitter/SapiStreamEmitterTest.php +++ b/test/Emitter/SapiStreamEmitterTest.php @@ -67,14 +67,14 @@ public function testAssertNoPreviousOutput() ->withStatus(200) ->withBody($stream); $this->expectException('Zend\HttpHandlerRunner\Exception\EmitterException'); - //$this->expectExceptionMessage('Unable to emit response; headers already sent'); - + $this->expectExceptionMessage('Unable to emit response; headers already sent'); + /* $this->expectExceptionMessage(sprintf( 'output started at %s:%d', __FILE__, __LINE__ + 3 )); - + */ //ob_start(); echo 'Unexpected Output'; try { From d8afed71983691762e55884849d19a45babb24eb Mon Sep 17 00:00:00 2001 From: undercloud Date: Mon, 1 Apr 2019 11:29:42 +0400 Subject: [PATCH 16/19] Update SapiStreamEmitterTest.php --- test/Emitter/SapiStreamEmitterTest.php | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/test/Emitter/SapiStreamEmitterTest.php b/test/Emitter/SapiStreamEmitterTest.php index 1a4eb72..f9e7411 100644 --- a/test/Emitter/SapiStreamEmitterTest.php +++ b/test/Emitter/SapiStreamEmitterTest.php @@ -66,23 +66,19 @@ public function testAssertNoPreviousOutput() $response = (new Response()) ->withStatus(200) ->withBody($stream); - $this->expectException('Zend\HttpHandlerRunner\Exception\EmitterException'); - $this->expectExceptionMessage('Unable to emit response; headers already sent'); - /* - $this->expectExceptionMessage(sprintf( - 'output started at %s:%d', - __FILE__, - __LINE__ + 3 - )); - */ - //ob_start(); + ob_start(); echo 'Unexpected Output'; try { $this->emitter->emit($response); } catch (\Throwable $e) { - throw $e; + $this->assertTrue($e instanceof Zend\HttpHandlerRunner\Exception\EmitterException); + $this->assertEquals($e->getMessage(),sprintf( + 'Unable to emit response; headers already sent, output started at %s:%d', + __FILE__, + __LINE__ - 5 + )); } finally { - //ob_end_clean(); + ob_end_clean(); } } From b221bf73eaf650533a89dcc13bb2d739a8a8fc92 Mon Sep 17 00:00:00 2001 From: undercloud Date: Mon, 1 Apr 2019 11:33:15 +0400 Subject: [PATCH 17/19] Update SapiStreamEmitterTest.php --- test/Emitter/SapiStreamEmitterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Emitter/SapiStreamEmitterTest.php b/test/Emitter/SapiStreamEmitterTest.php index f9e7411..267169c 100644 --- a/test/Emitter/SapiStreamEmitterTest.php +++ b/test/Emitter/SapiStreamEmitterTest.php @@ -71,7 +71,7 @@ public function testAssertNoPreviousOutput() try { $this->emitter->emit($response); } catch (\Throwable $e) { - $this->assertTrue($e instanceof Zend\HttpHandlerRunner\Exception\EmitterException); + //$this->assertTrue($e instanceof Zend\HttpHandlerRunner\Exception\EmitterException); $this->assertEquals($e->getMessage(),sprintf( 'Unable to emit response; headers already sent, output started at %s:%d', __FILE__, From fe0eb7011da94bc476d695cf0d3579741472f2b5 Mon Sep 17 00:00:00 2001 From: undercloud Date: Mon, 1 Apr 2019 11:37:26 +0400 Subject: [PATCH 18/19] Update SapiStreamEmitterTest.php --- test/Emitter/SapiStreamEmitterTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/Emitter/SapiStreamEmitterTest.php b/test/Emitter/SapiStreamEmitterTest.php index 267169c..ed0b73d 100644 --- a/test/Emitter/SapiStreamEmitterTest.php +++ b/test/Emitter/SapiStreamEmitterTest.php @@ -72,11 +72,17 @@ public function testAssertNoPreviousOutput() $this->emitter->emit($response); } catch (\Throwable $e) { //$this->assertTrue($e instanceof Zend\HttpHandlerRunner\Exception\EmitterException); + $this->assertEquals( + 'Output has been emitted previously; cannot emit response', + $e->getMessage() + ); + /* $this->assertEquals($e->getMessage(),sprintf( 'Unable to emit response; headers already sent, output started at %s:%d', __FILE__, __LINE__ - 5 )); + */ } finally { ob_end_clean(); } From 2e6df55e3f5a11fcf92d01489ff1f1cda83c42f9 Mon Sep 17 00:00:00 2001 From: undercloud Date: Mon, 1 Apr 2019 11:43:04 +0400 Subject: [PATCH 19/19] Update SapiStreamEmitterTest.php --- test/Emitter/SapiStreamEmitterTest.php | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/test/Emitter/SapiStreamEmitterTest.php b/test/Emitter/SapiStreamEmitterTest.php index ed0b73d..01fb41b 100644 --- a/test/Emitter/SapiStreamEmitterTest.php +++ b/test/Emitter/SapiStreamEmitterTest.php @@ -71,20 +71,32 @@ public function testAssertNoPreviousOutput() try { $this->emitter->emit($response); } catch (\Throwable $e) { - //$this->assertTrue($e instanceof Zend\HttpHandlerRunner\Exception\EmitterException); $this->assertEquals( 'Output has been emitted previously; cannot emit response', $e->getMessage() ); - /* + } finally { + ob_end_clean(); + } + } + + public function testAssertNoPreviousOutputNoBuffer() + { + $stream = new CallbackStream(function () { + return 'it works'; + }); + $response = (new Response()) + ->withStatus(200) + ->withBody($stream); + echo 'Unexpected Output'; + try { + $this->emitter->emit($response); + } catch (\Throwable $e) { $this->assertEquals($e->getMessage(),sprintf( 'Unable to emit response; headers already sent, output started at %s:%d', __FILE__, __LINE__ - 5 )); - */ - } finally { - ob_end_clean(); } }