readFileCalled = false; $readFile = function (string $fileName): string { $this->readFileCalled = true; return $fileName; }; $this->handler = new NotFoundTemplateHandler($readFile); } /** * @test * @dataProvider provideTemplates */ public function properErrorTemplateIsRendered(ServerRequestInterface $request, string $expectedTemplate): void { $resp = $this->handler->handle($request->withHeader('Accept', 'text/html')); self::assertInstanceOf(Response\HtmlResponse::class, $resp); self::assertStringContainsString($expectedTemplate, (string) $resp->getBody()); self::assertTrue($this->readFileCalled); } public function provideTemplates(): iterable { $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/foo')); yield 'base url' => [$this->withNotFoundType($request, '/foo'), NotFoundTemplateHandler::NOT_FOUND_TEMPLATE]; yield 'regular not found' => [$this->withNotFoundType($request), NotFoundTemplateHandler::NOT_FOUND_TEMPLATE]; yield 'invalid short code' => [ $this->withNotFoundType($request->withAttribute( RouteResult::class, RouteResult::fromRoute( new Route( '', $this->prophesize(MiddlewareInterface::class)->reveal(), ['GET'], RedirectAction::class, ), ), )), NotFoundTemplateHandler::INVALID_SHORT_CODE_TEMPLATE, ]; } private function withNotFoundType(ServerRequestInterface $req, string $baseUrl = ''): ServerRequestInterface { $type = NotFoundType::fromRequest($req, $baseUrl); return $req->withAttribute(NotFoundType::class, $type); } }