1

I am using a route in Silex to delete an object from the database. If an object does not exist, a 404 error should be thrown. This works fine in the browser, and the response is received accordingly.

This is my source:

$app->delete("/{entity}/{id}", function(\Silex\Application $app, HttpFoundation\Request $request, $entity, $id) {
    // some prep code is here
    $deleteObject = $this->em->getRepository($entityClass)->find($id);
    if (empty($deleteObject))
        $app->abort(404, "$ucEntity with ID $id not found");
    // other code comes here...
}

This is my test case:

// deleting the same object again should not work
$client->request("DELETE", "/ccrud/channel/$id");
$this->assertTrue($response->getStatusCode() == 404);

Now phpunit fails with the following error: 1) CrudEntityTest::testDelete Symfony\Component\HttpKernel\Exception\HttpException: Channel with ID 93 not found

I can see from the message that the 404 was thrown, but I cannot test the response object as planned. I know that in theory I could assert for the exception itself, but that is not what I want to do, I want to get the response (as a browser would do as well) and test for the status code itself.

Anybody any ideas how to reach that or if there is a better way to test this?

Thanks, Daniel

1 Answer 1

2

This is how it is being done in the tests of Silex itself (see here):

public function testErrorHandlerNotFoundNoDebug()
{
    $app = new Application();
    $app['debug'] = false;

    $request = Request::create('/foo');
    $response = $app->handle($request);
    $this->assertContains('<title>Sorry, the page you are looking for could not be found.</title>', $response->getContent());
    $this->assertEquals(404, $response->getStatusCode());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.