Skip to content

Commit

Permalink
Update BodyParsingMiddlewareTest
Browse files Browse the repository at this point in the history
Dynamically adding a property to a class is deprecated and so rather
than adding the request to the response from the handler, we now add it
to the handler itself and test it there.
  • Loading branch information
akrabat committed Nov 23, 2023
1 parent fb55626 commit 4c7e0cf
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions tests/Middleware/BodyParsingMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected function createRequestHandler(): RequestHandlerInterface
$response = $this->createResponse();
return new class ($response) implements RequestHandlerInterface {
private $response;
public $request;

public function __construct(ResponseInterface $response)
{
Expand All @@ -39,7 +40,7 @@ public function __construct(ResponseInterface $response)

public function handle(ServerRequestInterface $request): ResponseInterface
{
$this->response->request = $request;
$this->request = $request;
return $this->response;
}
};
Expand Down Expand Up @@ -152,9 +153,10 @@ public function testParsing($contentType, $body, $expected)
$request = $this->createRequestWithBody($contentType, $body);

$middleware = new BodyParsingMiddleware();
$response = $middleware->process($request, $this->createRequestHandler());
$requestHandler = $this->createRequestHandler();
$middleware->process($request, $requestHandler);

$this->assertEquals($expected, $response->request->getParsedBody());
$this->assertEquals($expected, $requestHandler->request->getParsedBody());
}

public function testParsingWithARegisteredParser()
Expand All @@ -167,9 +169,10 @@ public function testParsingWithARegisteredParser()
},
];
$middleware = new BodyParsingMiddleware($parsers);
$response = $middleware->process($request, $this->createRequestHandler());
$requestHandler = $this->createRequestHandler();
$middleware->process($request, $requestHandler);

$this->assertSame(['data' => '{"foo":"bar"}'], $response->request->getParsedBody());
$this->assertSame(['data' => '{"foo":"bar"}'], $requestHandler->request->getParsedBody());
}

public function testParsingFailsWhenAnInvalidTypeIsReturned()
Expand Down

0 comments on commit 4c7e0cf

Please sign in to comment.