Search code examples
phpsymfonytestingfunctional-testing

Symfony2: Get input value in functional testing


I have this functional test (I'm just showing a fragment):

namespace Just\An\ExampleBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ExampleControllerTest extends WebTestCase
{
  public function testCrearTipus()
  {
    $client = static::createClient(array(), array(
            'PHP_AUTH_USER' => 'login',
            'PHP_AUTH_PW' => 'pa$$sword'
    ));
    $client->followRedirects();
    $url = '/admin/type/new';
    $crawler = $client->request('GET', $url);
    $token = $crawler->filter('input[name="name_of_form[_token]"]')->first()->getValue();
    .....
  }
}

I just cannot get the value of the input defined in my view as:

Any help would be appreciated


Solution

  • If you have a button in your form you can try this instead.

        $crawler = $client->request('GET', $url);
        $buttonCrawlerNode = $crawler->selectButton('Save');
    
        $form = $buttonCrawlerNode->form();
    
        $token = $form->get('name_of_form[_token]')->getValue()
    

    where Save is the text you have in your button