From 5d71c767afbd0cb32eb56df54e33498cb35e7bde Mon Sep 17 00:00:00 2001 From: Jeroen De Dauw Date: Wed, 2 Feb 2022 18:57:14 +0100 Subject: [PATCH] Fix TestResponse::getBody --- README.md | 4 ++++ src/TestResponse.php | 10 +++++----- tests/TestResponseTest.php | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 tests/TestResponseTest.php diff --git a/README.md b/README.md index bd05e52..298e946 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,10 @@ Test doubles ## Release notes +### 1.0.1 (2022-02-02) + +* Fixed behavior of `TestResponse::getBody` + ### 1.0.0 (2022-01-30) Initial release with diff --git a/src/TestResponse.php b/src/TestResponse.php index 9cb2fc5..396ead0 100644 --- a/src/TestResponse.php +++ b/src/TestResponse.php @@ -10,8 +10,6 @@ class TestResponse implements ResponseInterface { - private bool $gotBody = false; - public function __construct( private readonly string $body = '', private readonly int $statusCode = 200 @@ -91,12 +89,14 @@ public function withoutHeader( $name ): self { } public function getBody(): StreamInterface { - return new PumpStream( function() { - if ( $this->gotBody ) { + $gotBody = false; + + return new PumpStream( function() use ( &$gotBody ) { + if ( $gotBody ) { return null; } - $this->gotBody = true; + $gotBody = true; return $this->body; } ); } diff --git a/tests/TestResponseTest.php b/tests/TestResponseTest.php new file mode 100644 index 0000000..a2e5740 --- /dev/null +++ b/tests/TestResponseTest.php @@ -0,0 +1,22 @@ +assertSame( 'foo', $response->getBody()->getContents() ); + $this->assertSame( 'foo', $response->getBody()->getContents() ); + } + +}