Sto provando a testare un metodo di chiamata API che è protetto utilizzando Basic Auth e che deve recuperare l'utente dal contesto di sicurezza.Impossibile ottenere l'utente dal contesto di sicurezza nell'ambiente di prova
Il mio file config_test.yml (sto usando database in memoria per la prova ENV):
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
ho creato un test in cui ho un'istanza di un client come segue:
$options = array(
'environment' => 'test',
'debug' => true,
);
$this->client = static::createClient($options);
il test si presenta come:
public function testProfileAction()
{
$mockUser = new User();
$mockUser->setUsername("user");
$mockUser->setEmail("[email protected]");
$mockUser->setName("User");
$mockUser->setSurname("User");
$mockUser->setPlainPassword("1234567890");
$this->em->persist($mockUser);
$this->em->flush();
$crawler = $this->client->request('GET', '/api/1/user/profile', array(), array(), array(
'PHP_AUTH_USER' => 'user',
'PHP_AUTH_PW' => '1234567890',
));
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
}
E il metodo ha bisogno di ottenere l'utente dal contesto di protezione.
$user = $this->get('security.context')->getToken()->getUser();
Ma, eseguire i test ho sempre ottenere un 401 anziché 200. di un dumping variabile utente $ nel metodo è sempre nullo.
Sto utilizzando Symfony 2.2 e FOSUserBundle.
Grazie in anticipo.