middleware = new BodyParserMiddleware(); } /** * @test * @dataProvider provideIgnoredRequestMethods */ public function requestsFromOtherMethodsJustFallbackToNextMiddleware(string $method): void { $request = $this->createMock(ServerRequestInterface::class); $request->method('getMethod')->willReturn($method); $request->method('getParsedBody')->willReturn([]); $this->assertHandlingRequestJustFallsBackToNext($request); } public function provideIgnoredRequestMethods(): iterable { yield 'GET' => ['GET']; yield 'HEAD' => ['HEAD']; yield 'OPTIONS' => ['OPTIONS']; } /** @test */ public function requestsWithNonEmptyBodyJustFallbackToNextMiddleware(): void { $request = $this->createMock(ServerRequestInterface::class); $request->method('getMethod')->willReturn('POST'); $request->method('getParsedBody')->willReturn(['foo' => 'bar']); $this->assertHandlingRequestJustFallsBackToNext($request); } private function assertHandlingRequestJustFallsBackToNext(MockObject & ServerRequestInterface $request): void { $request->expects($this->never())->method('getHeaderLine'); $nextHandler = $this->createMock(RequestHandlerInterface::class); $nextHandler->expects($this->once())->method('handle')->with($request)->willReturn(new Response()); $this->middleware->process($request, $nextHandler); } /** @test */ public function jsonRequestsAreJsonDecoded(): void { $test = $this; $body = new Stream('php://temp', 'wr'); $body->write('{"foo": "bar", "bar": ["one", 5]}'); $request = (new ServerRequest())->withMethod('PUT') ->withBody($body); $handler = $this->createMock(RequestHandlerInterface::class); $handler->expects($this->once())->method('handle')->with( $this->isInstanceOf(ServerRequestInterface::class), )->willReturnCallback( function (ServerRequestInterface $req) use ($test) { $test->assertEquals([ 'foo' => 'bar', 'bar' => ['one', 5], ], $req->getParsedBody()); return new Response(); }, ); $this->middleware->process($request, $handler); } }