Vinicius Dias
Vinicius Dias

Reputation: 689

How do I override a User Provider for testing with Symfony 3?

I have the following security.yml:

security:

    # http://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
    providers:
        in_memory:
            memory: ~
        bd_provider:
            entity:
                class: AppBundle:Usuario
                property: email

    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        main:
            anonymous: ~

            form_login:
                login_path: login
                check_path: login

            provider: bd_provider

            logout:
                path:   /logout
                target: /login

And I'm authenticating for testing porpuses by this way:

protected function createClientWithAuthentication()
{
    /* @var $client Client */
    $client = static::createClient();

    /* @var $user UserInterface */
    $user = $client->getContainer()->get('doctrine')->getRepository('AppBundle:Usuario')->find(5);

    $firewallName = 'main';
    $token = new UsernamePasswordToken($user, $user->getPassword(), $firewallName, $user->getRoles());
    $session = $client->getContainer()->get('session');
    $session->set('_security_' . $firewallName, serialize($token));
    $session->save();

    $cookie = new Cookie($session->getName(), $session->getId());
    $client->getCookieJar()->set($cookie);

    return $client;
}

But I would like to use a diferent provider for testing, like in_memory. So I added the following in my config_test.yml

security:
    providers:
        em_memoria:
            memory:
                users:
                    [email protected]:
                        password: senha
                        roles: 'ROLE_SUPER_ADMIN'

    encoders:
            AppBundle\Entity\Usuario: plaintext

    firewalls:
        main:
            provider: em_memoria

But when I change my code that assemble the user to this:

$user = new Usuario();
$user
    ->setUsername('[email protected]')
    ->setPassword('admin')
    ->setRole('ROLE_USER_ADMIN');

The tests send me to the login page, as if I was using an incorrect user. What am I missing here? Is there anything else I should do?

If any other information is necesssary just tell me and I'll provide here.

Thanks in advance.

Upvotes: 0

Views: 1000

Answers (2)

Vinicius Dias
Vinicius Dias

Reputation: 689

I couldn't make it work, so I configured my database on the test environment to be a SQLite db. I also created some DataFixtures to load initial data, and called theese fixtures on my bootstrap file.

Upvotes: 1

Dmitry Malyshenko
Dmitry Malyshenko

Reputation: 3051

You can use an alias as user provider and refer to different services in different environments.

firewalls:
    main:
        provider: app.user_provider

conf.yml:

services:
    app.user_provider:
       alias: bd_provider

conf_test.yml:

services:
    app.user_provider:
       alias: em_memoria

Upvotes: 0

Related Questions