Home > database >  Passing parameters to symfony 5.4 form test not working
Passing parameters to symfony 5.4 form test not working

Time:11-19

I have tried for 5 hours already with no success... I have a Symfony 5.3 app and implemented a search page that's not tied to an entity. Works perfectly, however when creating a general availability test the parameters are not passed to the controller. I have tried mocking with the $_POST array, passing an array to the third parameter of $client->request(), but the result always shows a GET method is executed and the parameter is not included into the request.

The form is like this:

<form class="form-search" action="{{ path('search_result') }}" method="post">
    <input name="searchwords" placeholder="{{ 'search.placeholder'|trans }}" type="text" hljs-string">">
    <button type="submit" hljs-string">"><i hljs-string">"></i></button>
</form>

In my controller can access the parameter "searchwords" with the following:

$vars = Request::createFromGlobals();
$temp_searchwords = explode(' ', $vars->get('searchwords'));

Now I'm trying to pass parameters during my General Availability Test

$crawler = $client->request('POST', '/de/search/result', ["searchwords" => "test"]);

The result shows that a GET method is used and the parameters are not passed to the Controller. I have also tried mocking with the $_POST system variable:

$_POST = ["searchwords" => "test strings"]

the Request::createFromGlobals(); gives no parameters in my Controller, perhaps because a GET method is used instead of the 'POST' as requested in my $client->request() call.

Another try taken from the official Symfony 5.3 documentation also failed with the parameter not passed to my controller:

$buttonCrawlerNode = $crawler->selectButton('');
$form = $buttonCrawlerNode->form();
$form['searchwords'] = "test string";
// at this point the $form object contains the parameter "searchwords" and the value "test string"
$crawler = $client->submit($form);

Question: How would I pass a parameter to my controller and force a 'POST' request in my test? As said, the search is working fine in a real browser environment.

CodePudding user response:

Should anyone ever find themselves in this situation:

I have called the post variable "searchword" via the following function.

$vars = Request::createFromGlobals();
$searchwords = explode(' ', $vars->get('searchwords'));

However, this only works in the browser, because the WebTest creates a request object without a diversion via global variables.

The real solution is to Autowire the request object in your controller function and use it for parameter retrieval:

public function searchResultAction($_locale, DuddeSearch $ds, Request $request)
{
    [...]
    $searchwords = explode(' ', $request->get('searchwords'));
}

This problem is a beginner's mistake on my part. Autowiring is explicitly and prominently mentioned in Symfony's "Best Practices". The only difficulty was that my solution worked perfectly in the browser.

  • Related