In a Symfony 5.2.1 project a simple test calling for an invalid id will throw an NotFoundHttpException exception as expected. Yet using that name (or any of its parents) is deemed unacceptable in the test. So what is the correct expression for the name of the exception?
For example, this test:
public function testNonprofitNotFound()
{
$this->expectException(NotFoundHttpException::class);
$this->client->request('GET', '/nonprofit/view/0');
}
Results in the output:
Failed asserting that exception of type "Symfony\Component\HttpKernel\Exception\NotFoundHttpException" is thrown.
Similar results occur with or without use... statements or higher level (e.g., \RuntimeException) names. Using instead $this->expectExceptionCode(404) also fails whether or not the code is expressed as a string. Asserting the exception message also fails. Calling that URI in the prod environment will render the custom error404.html.twig template. Using annotations like @expectedException ... produce similar results as well.
The controller uses the annotation @ParamConverter, which is quoted in the message provided by Symfony's NotFoundHttpException error page.
/**
* @Route("/view/{npo}", name = "npo_view")
* @ParamConverter("npo", class="App:Nonprofit")
*/
public function view($npo)
{
...
}
In dev mode the error page shows:
App:Nonprofit object not found by the @ParamConverter annotation.
...vendor\sensio\framework-extra-bundle\src\Request\ParamConverter\DoctrineParamConverter.php (line 107)
if (null === $object && false === $configuration->isOptional()) {
$message = sprintf('%s object not found by the @%s annotation.', $class, $this->getAnnotationName($configuration));
if ($errorMessage) {
$message .= ' '.$errorMessage;
}
throw new NotFoundHttpException($message);
}
$request->attributes->set($name, $object);
return true;
Aucun commentaire:
Enregistrer un commentaire