I’m using the SonataAdminBundle plus DoctrineORMBundle and let’s say I have a Post/Tags relationship where Tags are many-to-many to Posts.
I’m trying to run a functional test on the Post form. Tags are shown in the Post form throw an widget where the Tag form fields comes from another request (Ajax call) and merged in the Post form by Javascript.
It’s easy to rely on Javascript to do this but when it comes to the functional testing
scenario, using the WebTestCase class, I found a difficulty to simulate such functionality.
Let’s say I’m testing the Create action of the Post and using this code on my test case.
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);
}
The problem with the code above it’s that from thereon I don’t know how to merge the Tag form into the Post form so this way they are submitted together.
Any ideas?
Finally I found how to merge those 2 request contents together. Here is code that I used: