jeudi 10 novembre 2016

symfony WebTestCase client does not send post data

I'm currently writing functionnal tests for my symfony app. I use symfony 3 (3.1.6) with phpunit 5.6.1.

Here is the code i use to test my form :

public function testSaveMediaFromMediaUrl()
{
    $client = static::createClient();
    $crawler = $client->request('GET', '/form');

    $form = $crawler->selectButton('OK')->form();
    $form['mediaUrl'] = 'http://example.com';

    $client->submit($form);
    var_dump($client->getResponse()->getContent());
}

The correct action of my controller is called but there is nothing in the request object when the action is called from the tests suite. using a regular web browser, everything works fine. In the controller, I use $request = Request::createFromGlobals(); to create the request object

I also tried this code to post the data and I get the same result : no POST data is received in the controller.

direct post request without using the form

public function testSaveMediaFromMediaUrl()
{
    $client = static::createClient();
    $crawler = $client->request('POST', '/media', ['mediaUrl' => 'http://example.com']);

    var_dump($crawler->html());
}

adding the data in the submit method

public function testSaveMediaFromMediaUrl()
{
    $client = static::createClient();
    $crawler = $client->request('GET', '/form');

    $form = $crawler->selectButton('OK')->form();

    $client->submit($form, ['mediaUrl' => 'http://example.com']);
    var_dump($client->getResponse()->getContent());
}

Is there something I'm doing wrong ?

Aucun commentaire:

Enregistrer un commentaire