In my Symfony project, I am writing tests. I wrote all tests that are covering my CustomException
. But, I want to write separate test to catch the one with Throwable
. But, as all my cases are covered with CustomException
, I do not know the way how to write a tests that will catch the message in Throwable
.
From Controller:
/**
* @Route("/endpoint", methods={"POST"}, name="endpoint_name")
*/
public function someMethod($uuid)
try {
$this->myService->someServiceMethod($id);
return $this->success("SUCCESS");
} catch (CustomException $e) {
return $this->handleCustomException($e);
} catch (Throwable $e) {
$this->logger->error("FAILED: " . $e->getMessage(), [
$id
]);
return $this->error("FAILED");
}
Any thoughts?
public function testSomeMethod(): void
{
$this->client->request(
Request::METHOD_POST,
self::API_URL . self::POST_ENDPOINT,
[],
[],
[
'CONTENT_TYPE' => 'application/json',
'ACCEPT' => 'application/json',
]
);
$response = $this->client->getResponse();
$responseBody = json_decode($response->getContent(), true);
$this->assertContains('Some message from Custom Exception!', $responseBody['error']);
$this->assertEquals(405, $response->getStatusCode());
}
This is the test for my Custom Exception that is working.
Is there any way I can wrote the same test but to catch the message from Throwable
class.