title: $this->getUser() in Symfony controller functional test returns null
body: <p>I'm trying to test a controller that uses $this->getUser()->getUsername(). It complains that getUsername() is called on null.</p>
  
  <p>Here is my client login code from the test class</p>
  
  <pre><code>protected function logInAsAdmin(Client $client): void
      {
          $session = $client-&gt;getContainer()-&gt;get('session');
  
          $firewallName = 'main';
          $firewallContext = 'main';
  
          $roles = [
              'ROLE_USER',
              'ROLE_ADMIN',
          ];
  
          $token = new UsernamePasswordToken('admin', null, $firewallName, $roles);
          $session-&gt;set('_security_' . $firewallContext, serialize($token));
          $session-&gt;save();
  
          $cookie = new Cookie($session-&gt;getName(), $session-&gt;getId());
          $client-&gt;getCookieJar()-&gt;set($cookie);
      }
  </code></pre>
  
  <p>And here is what the controller does:</p>
  
  <pre><code>public function home(EmployerRepository $employerRepository, AuthorizationCheckerInterface $authorizationChecker): Response
      {
          if ($authorizationChecker-&gt;isGranted('IS_AUTHENTICATED_FULLY')) {
              $jobs = [];
  
              foreach ($employerRepository-&gt;findBy(['owner' =&gt; $this-&gt;getUser()-&gt;getUsername()]) as $employer) {
                  $jobs = array_merge($jobs, $employer-&gt;getJobs()-&gt;toArray());
              }
  
              return $this-&gt;render('home.html.twig', ['jobs' =&gt; $jobs]);
          }
  
          return $this-&gt;redirectToRoute('login');
      }
  </code></pre>
  
  <p>Can anyone tell me why this does not work? I tried instantiating a user object and passing that into the UsernamePasswordToken, but no luck with that either.</p>
  
  <p>using Symfony 4.</p>
  
  <p>The test:</p>
  
  <pre><code>/**
       * @test
       */
      public function indexPageIsRenderedWhenLoggedIn(): void
      {
          $client = static::createClient();
          $this-&gt;logInAsAdmin($client);
          $client-&gt;request('GET', '/');
          $this-&gt;assertEquals(200, $client-&gt;getResponse()-&gt;getStatusCode());
          $this-&gt;assertRegExp('/Your jobs/', $client-&gt;getResponse()-&gt;getContent());
      }
  </code></pre>