I have read many posts on stackoverflow about this. But most of the methods not useful in Symfony 2.3.
So I have try to log in user manually in test to make some actions in back-end.
Here is my security.yml

```
security:
...
  role_hierarchy:
        ROLE_SILVER: [ROLE_BRONZE]
        ROLE_GOLD: [ROLE_BRONZE, ROLE_SILVER]
        ROLE_PLATINUM: [ROLE_BRONZE, ROLE_SILVER, ROLE_GOLD]
        ROLE_ADMIN: [ROLE_BRONZE, ROLE_SILVER, ROLE_GOLD, ROLE_PLATINUM, ROLE_ALLOWED_TO_SWITCH]

    providers:
        database:
            entity: { class: Fox\PersonBundle\Entity\Person, property: username }

    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        login:
            pattern:  ^/person/login$
            security: false

        main:
            pattern:    ^/
            provider:   database
            form_login:
                check_path: /person/login-check
                login_path: /person/login
                default_target_path: /person/view
                always_use_default_target_path: true
            logout:
                path:   /person/logout
                target: /
            anonymous: true

    access_control:
        - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/person/registration, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/person, roles: ROLE_BRONZE }

```

Here is my test:

```
class ProfileControllerTest extends WebTestCase
{
    public function setUp()
    {
        $kernel = self::getKernelClass();

        self::$kernel = new $kernel('dev', true);
        self::$kernel->boot();
    }

    public function testView()
    {
        $client = static::createClient();

        $person = self::$kernel->getContainer()->get('doctrine')->getRepository('FoxPersonBundle:Person')->findOneByUsername('master');

        $token = new UsernamePasswordToken($person, $person->getPassword(), 'main', $person->getRoles());

        self::$kernel->getContainer()->get('security.context')->setToken($token);

        self::$kernel->getContainer()->get('event_dispatcher')->dispatch(
        AuthenticationEvents::AUTHENTICATION_SUCCESS,
        new AuthenticationEvent($token));

        $crawler = $client->request('GET', '/person/view');
    }

```

And when I run this test, `$person = $this->get(security.context)->getToken()->getUser();` method is not working in testing Controller. Say if in controller call `$person->getId();` I will have an error `Call to a member function getId() on a non-object in...` .

So can you tell the properly way to log in user in functional test in Symfony 2.3?

Thanks!

**EDIT\_1**:
If I change `Symfony/Component/Security/Http/Firewall/ContextListener.php` and comment one string:

```
if (null === $session || null === $token = $session->get('_security_'.$this->contextKey)) {
            // $this->context->setToken(null);

            return;
        }

```

all tests going on without errors.

**EDIT\_2**:
This is reference that i have trying to use:
[first](https://stackoverflow.com/questions/11451969/authentication-in-functional-tests-in-symfony-2-1)
[second](https://stackoverflow.com/questions/8637172/manual-authenticate-user)
[third](https://stackoverflow.com/questions/5886713/automatic-post-registration-user-authentication)
[fourth](https://stackoverflow.com/questions/6535873/how-to-use-an-authenticated-user-in-a-symfony2-functional-test)
[fifth](https://stackoverflow.com/questions/11451969/authentication-in-functional-tests-in-symfony-2-1)
[sixth](https://stackoverflow.com/questions/8455776/how-to-develop-by-faking-login-to-test-acls-in-symfony-2)
[seventh](https://stackoverflow.com/questions/8522432/handling-authentication-in-symfony2-unit-tests)
[eighth](https://stackoverflow.com/questions/8522432/handling-authentication-in-symfony2-unit-tests)
[nineth](https://stackoverflow.com/questions/9550079/how-to-programmatically-login-authenticate-a-user)