mercredi 13 janvier 2016

Symfony2 Functional Testing Issue

I have a simple form login using GuardAuthenticator. That works awesome so far.

Now to test this, I setup a simple WebTestCase functional test.

I want to cover login with correct credentials, Login with customers number, Login with wrong credentials and so on. Then check with ``$this->assertContains()` for text in the response.

I am not using Crawler or form submittig at all. Only a post request against /login_check route.

Then I need to follow the redirect and read response. Great so far.

Now, here is the problem: the first test is successful, all after that just return empty response only containing some headers.

It's not about the testcase, it's about the order. When I reorder the test, still the first one is successful. Why?

Here is my code:

<?php

namespace SVB;

use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class UserControllerTest extends WebTestCase
{

/**
 * @var Client
 */
private $client;

public function setUp()
{
    $this->client = $this->createClient();
    $this->client->setServerParameters(
        [
            'HTTP_HOST' => 'vagrant.dev',
        ]
    );

}


public function testLoginFailsWithWrongEmail()
{

    $this->login('mbl', 'xxxxxx');

    $content = $this->client->getResponse()->getContent();
    $this->assertContains('Login fehlgeschlagen', $content, $content);

}

public function testLoginFailsWithWrongPassword()
{
    $this->login('mbl@domain.com', 'WrongPassword');

    $content = $this->client->getResponse()->getContent();
    $this->assertContains('Login fehlgeschlagen', $content, $content);
}



public function testLoginWorksWithCorrectCredentials()
{

    $this->login('mbl@domain.com', 'xxxxxx');

    $content = $this->client->getResponse()->getContent();
    $this->assertContains('In Ihrem Konto haben Sie die Möglichkeit, Ihre persönlichen Daten', $content);

}


private function login($username, $password)
{

    $this->client->request(
        'POST',
        '/login_check',
        [
            '_username' => $username,
            '_password' => $password,
        ]
    );

    if ($this->client->getResponse()->isRedirect()) {
        $this->client->followRedirect();
    }

    }

}

Here is the empty responsse, tests fail:

PHPUnit 4.8.21 by Sebastian Bergmann and contributors.


Failed asserting that '' contains "Login fehlgeschlagen".
 /var/www/svb-shop/tests/SVB/UserControllerTest.php:49


Failed asserting that '' contains "In Ihrem Konto haben Sie die Möglichkeit, Ihre persönlichen Daten".
 /var/www/svb-shop/tests/SVB/UserControllerTest.php:60



Time: 4.13 seconds, Memory: 44.50Mb


FAILURES!
Tests: 3, Assertions: 3, Failures: 2.

Process finished with exit code 1

For login I use GuardAuthenticator. Could that be related? I also set some cookies and no-cache headers in FilterResponseEvent, maybe that breaks BrowserKit's Client?

Aucun commentaire:

Enregistrer un commentaire