我做的Symfony2控制器的功能測試,從這個繼承我的測試類:用戶令牌Symfony2的功能測試
class InsecureWebTestCase extends WebTestCase {
protected $client = null;
public function setUp() {
$this->client = static::createClient();
$session = $this->client->getContainer()->get('session');
$firewall = 'default';
$token = new UsernamePasswordToken('[email protected]', null, $firewall, array('ROLE_USER', 'ROLE_ADMIN'));
// $this->client->getContainer()->get('security.context')->setToken($token);
$session->set("_security_$firewall", serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}
}
如果我使用控制器作爲應用程序的一部分: $this->container->get('security.token_storage')->getToken()->getUser()
和$this->getUser()
是實例我的學說「用戶」實體。
但運行功能測試時: $this->container->get('security.token_storage')->getToken()->getUser()
是包含用戶名稱的字符串和$this->getUser()
是NULL
。
在我的應用程序和功能測試中,爲了使行爲保持一致,我需要做些什麼?
執行您的請求時,試試這個方法:'$客戶 - >請求(」 GET','/ post/12',array(),array(),array( 'PHP_AUTH_USER'=>'username', 'PHP_AUTH_PW'=>'pa $$ word', ));' – smarber