Skip to content

Commit

Permalink
Fix TestResponse::getBody
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed Feb 2, 2022
1 parent f5014b6 commit 5d71c76
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/TestResponse.php
Expand Up @@ -10,8 +10,6 @@

class TestResponse implements ResponseInterface {

private bool $gotBody = false;

public function __construct(
private readonly string $body = '',
private readonly int $statusCode = 200
Expand Down Expand Up @@ -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;
} );
}
Expand Down
22 changes: 22 additions & 0 deletions tests/TestResponseTest.php
@@ -0,0 +1,22 @@
<?php

declare( strict_types = 1 );

namespace Jeroen\PostRequestSender\Tests;

use Jeroen\PostRequestSender\TestResponse;
use PHPUnit\Framework\TestCase;

/**
* @covers \Jeroen\PostRequestSender\TestResponse
*/
class TestResponseTest extends TestCase {

public function testGetBodyCanBeCalledMultipleTimes(): void {
$response = new TestResponse( 'foo' );

$this->assertSame( 'foo', $response->getBody()->getContents() );
$this->assertSame( 'foo', $response->getBody()->getContents() );
}

}

0 comments on commit 5d71c76

Please sign in to comment.