0

私は SonataAdminBundle と DoctrineORMBundle を使用しており、タグが投稿に対して多対多である投稿/タグの関係があるとしましょう。

Post フォームで機能テストを実行しようとしています。投稿フォームに表示されるタグは、タグ フォーム フィールドが別のリクエスト (Ajax 呼び出し) から取得され、Javascript によって投稿フォームにマージされるウィジェットをスローします。

これを行うために Javascript に頼るのは簡単ですが、WebTestCase クラスを使用して機能テストのシナリオになると、そのような機能をシミュレートするのが難しいことがわかりました。

Post の Create アクションをテストしていて、テスト ケースでこのコードを使用しているとします。

public function testCreate() {
    $client = static::createClient();
    $client2 = static::createClient();
    //main request. The Post form
    $crawler = $client->request('GET','/route/to/posts/create');
    //second request (The Tag form) simulating the request made via Ajax
    $crawler2 = $client2->request('GET','/admin/core/append-form-field-element?code=my.bundle.admin.tags);
}

上記のコードの問題は、タグフォームを投稿フォームにマージする方法がわからないため、この方法で一緒に送信されることです。何か案は?

4

1 に答える 1

0

最後に、これら 2 つのリクエストの内容をマージする方法を見つけました。私が使用したコードは次のとおりです。

public function testCreate() {
    $client = static::createClient();
    $client2 = static::createClient();
    //main request. The Post form
    $crawler = $client->request('GET','/route/to/posts/create');
    //second request (The Tag form) simulating the request made via Ajax
    $crawler2 = $client2->request('GET','/admin/core/append-form-field-element?code=my.bundle.admin.tags);
    //first request's form. This is where we'll merge the content.
    $form = $crawler->selectButton('submitButton')->form();
    //let's say we want to merge each input fields from the second request
    foreach ($crawler2->filter('input') as $node) {
       //we use the Crawler\Form::set method with a new InputFormField instance
       //that uses a DOMNode as parameter
       $form->set(new InputFormField($node));
    }

    //now we can test if the form has our merged input field from the ajax call
    $this->assertTrue($form->has('tagName'));
}
于 2012-09-24T13:38:41.580 に答える