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() ); + } + +}