self-learner
self-learner

Reputation: 33

Could not send form data in PHPUnit?

I am new to PHPUnit with Symfony 2. I have a problem sending form data to client. I have login details needed to log in the page. so i send it as array. Every time i run test i got Error Message as:

There was 1 error:

1) MySite\UserBundle\Tests\Controller\LoginControllerTest::testUser InvalidArgumentException: The current node list is empty.

My Test Code is:

public function testUser()
 {
    $client = static::createClient(array(), array());
    $crawler = $client->request('POST', '/login');
    //submit login data
    $form = $crawler->selectButton('submit')->form();
    $data = array(
    '_username' => '[email protected]',
    '_password' => '123',
    );
    $client->submit($form,$data);
    $client->followRedirect();
    $this->assertTrue($client->getReponse()->isSuccessful());

    //$this->assertTrue($crawler->filter('html:contains("login")'));
    $this->assertTrue($crawler->filter('html:contains("Dashboard")')
      ->count() > 0);
}   

I don't know from where the error came from. I just wanted to validate the user with username and password. And check whether they have successfully accessed to Dashboard or Not. The user is valid user, can be logged from browser. Any help will be highly appreciated !

Thank you in advance.

Upvotes: 1

Views: 1401

Answers (2)

Dric512
Dric512

Reputation: 3729

The normal way would be to write:

$form['_username'] = $username;
$form['_password'] = $password;

Upvotes: 0

Matteo
Matteo

Reputation: 39390

try this code:

        $client = static::createClient(array(), array());
        $crawler = $client->request('GET', '/login');
        $this->assertTrue($crawler->filter("form")->count() > 0, "Login form exist");
        $form = $crawler->filter("form")->form();
        $client->submit($form, array(
            '_username' => $username,
            '_password' => $password,
        ));
        if ($client->getResponse()->isRedirect()) {
            $crawler = $client->followRedirect();
        }
//        die(var_dump($this->client->getResponse()->getContent()));

       $this->assertTrue($crawler->filter('html:contains("Dashboard")')->count() > 0);

Hope this help

Upvotes: 0

Related Questions