1

データベースからオブジェクトを削除するために Silex のルートを使用しています。オブジェクトが存在しない場合は、404 エラーがスローされます。これはブラウザで正常に機能し、それに応じて応答が受信されます。

これは私のソースです:

$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...
}

これは私のテストケースです:

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

現在、phpunit は次のエラーで失敗します: 1) CrudEntityTest::testDelete Symfony\Component\HttpKernel\Exception\HttpException: Channel with ID 93 not found

404 がスローされたことはメッセージからわかりますが、応答オブジェクトを計画どおりにテストできません。理論的には例外自体をアサートできることはわかっていますが、それは私がやりたいことではありません。(ブラウザーも同様に) 応答を取得し、ステータス コード自体をテストしたいと考えています。

それに到達する方法、またはこれをテストするより良い方法があるかどうか、誰かアイデアはありますか?

ありがとう、ダニエル

4

1 に答える 1

2

これは、Silex 自体のテストで行われている方法です (ここを参照)。

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());
}
于 2012-11-10T17:08:43.453 に答える