lundi 18 décembre 2017

Symfony WebTestCase Method parameter (object)

I want to create a for a controller in Symfony3. The controller method has a signature as follows:

/**
 * @Method("GET")
 * @Route("/v1/entities", name="api_v1_entity_list")
 * @Security("has_role('ROLE_C1') || has_role('ROLE_S1') 
 * @param EntityListRequest $entityListRequest
 * @return Response
 */
public function listAction(EntityListRequest $entityListRequest)

How to i manage to pass such an EntityListRequest into the controller method in a WebTestCase?

I do have the following method in my abstract testing class derived from WebTestCase:

final protected function requestResponse(
    $method,
    $url,
    array $parameters = [],
    array $files = [],
    array $server = [],
    $content = null,
    $changeHistory = true
) {
    $this->client = static::createClient();
    $this->crawler = $this->client->request($method, $url, $parameters, 
             $files, $server, $content, $changeHistory);
    print_r($this->crawler);
    $this->response = $this->client->getResponse();
    return $this->response;
}

In my test case, i want to test the listAction method from the crawler:

echo $this->requestResponse("GET","/api/v1/entities",[...]);

In the productive environment, the EntityListRequest object is provided using the following GET parameters:

     ?page=1&pageSize=5&orderBy=untilDate&order=DESC&filter=has-offers

However, just passing those parameters as an array in the third argument did not work. The method is not called at all, instead Symfony returns an internal Server error.

How do I pass objects as paramters in WebTestCases?

Aucun commentaire:

Enregistrer un commentaire