Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot get user from Security Context in test environment
    primarykey
    data
    text
    <p>I'm trying to test an API call method which is securized using Basic Auth and which needs to retrieve the user from the security context. </p> <p>My config_test.yml file (I'm using in-memory database for test env): </p> <pre><code>imports: - { resource: config_dev.yml } doctrine: dbal: driver: pdo_sqlite path: :memory: memory: true orm: auto_generate_proxy_classes: %kernel.debug% auto_mapping: true framework: test: ~ session: ~ profiler: enabled: false web_profiler: toolbar: false intercept_redirects: false swiftmailer: disable_delivery: true </code></pre> <p>I've created a test where I instantiate a client as follows: </p> <pre><code> $options = array( 'environment' =&gt; 'test', 'debug' =&gt; true, ); $this-&gt;client = static::createClient($options); </code></pre> <p>The test looks like: </p> <pre><code>public function testProfileAction() { $mockUser = new User(); $mockUser-&gt;setUsername("user"); $mockUser-&gt;setEmail("user@user.com"); $mockUser-&gt;setName("User"); $mockUser-&gt;setSurname("User"); $mockUser-&gt;setPlainPassword("1234567890"); $this-&gt;em-&gt;persist($mockUser); $this-&gt;em-&gt;flush(); $crawler = $this-&gt;client-&gt;request('GET', '/api/1/user/profile', array(), array(), array( 'PHP_AUTH_USER' =&gt; 'user', 'PHP_AUTH_PW' =&gt; '1234567890', )); $this-&gt;assertEquals(200, $this-&gt;client-&gt;getResponse()-&gt;getStatusCode()); } </code></pre> <p>And the method needs to get the user from the security context. </p> <pre><code>$user = $this-&gt;get('security.context')-&gt;getToken()-&gt;getUser(); </code></pre> <p>But, running the tests I always get a 401 instead of a 200. Dumping the $user variable in method is always null. </p> <p>I'm using Symfony 2.2 and FOSUserBundle.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload