1
votes

I have an application in Symfony 3 with web app and a REST API.

I am writing a functional test using Symfony\Bundle\FrameworkBundle\Test\WebTestCase

$this->client = static::createClient(array(), array(
        'PHP_AUTH_USER' => 'test',
        'PHP_AUTH_PW' => 'test',
    ));

public function testIndex()
{
    $this->client->request(
        'GET',
        '/titles'
    );
    $response = $this->client->getResponse();
    $this->assertEquals(200, $response->getStatusCode());
}

I have OAuth authentication for the API and form_login for webapp. For my tests, I use http_basic, here's my config_test.yml:

security:
encoders:
        Symfony\Component\Security\Core\User\User: plaintext
firewalls:
    api:
        http_basic: ~
    main:
        http_basic: ~
providers:
    in_memory:
        memory:
            users:
                test:  { password: test, roles: [ 'ROLE_USER' ] }

In my controller, I get User like this:

$this->getUser()->getId()

When I launch phpunit, I get

Error: Call to undefined method Symfony\Component\Security\Core\User\User::getId()

It's not using my User entity for tests, how do I do ?

1

1 Answers

0
votes

It's not using my User entity for tests

This happens because the in memory provider uses the built in UserInterface implementation. InMemoryUserProvider will never use your entity -- it's based on a whole different system than a doctrine user provider.

You have a couple of ways around this.

  1. Use the database and doctrine like normal. This is absolutely what you should do: end to end tests should be truly end to end to be useful. Moreover, if you don't use the database and doctrine in your tests, stuff that's related to the user entity (via foreign keys) will fail.
  2. Create a custom user provider that mimics the behavior of InMemoryUserProvider but uses your custom entity class.