PHPUnit – How to test a controller that throws an error
Let’s say we have a method in a controller like this: /** * @Route(path=”/add”, name=”add”, methods={“POST”}) */ public function add(Request $request,): Response { throw new BadRequestHttpException(‘Error’); } To test that the exception is properly thrown, just write this public function testList(): void { $this->expectException(BadRequestHttpException::class); $client = self::createClient(); $client->catchExceptions (false); $crawler = $client->request( Request::METHOD_POST, ‘/add’ ); … Read more