handler = $this->prophesize(RequestHandlerInterface::class); $this->em = $this->prophesize(EntityManagerInterface::class); $this->middleware = new CloseDbConnectionMiddleware($this->em->reveal()); } /** * @test */ public function connectionIsClosedWhenMiddlewareIsProcessed() { $req = ServerRequestFactory::fromGlobals(); $resp = new Response(); $conn = $this->prophesize(Connection::class); $closeConn = $conn->close()->will(function () { }); $getConn = $this->em->getConnection()->willReturn($conn->reveal()); $handle = $this->handler->handle($req)->willReturn($resp); $result = $this->middleware->process($req, $this->handler->reveal()); $this->assertSame($result, $resp); $getConn->shouldHaveBeenCalledOnce(); $closeConn->shouldHaveBeenCalledOnce(); $handle->shouldHaveBeenCalledOnce(); } }