這是我的問題,我添加來自功能測試控制器的會話變量,他們不會彈出測試目標的行動。 。Symfony2功能測試和會話持久性
我做了一個登錄方法和使用cookbook advice登錄我調整了一點,讓2個帳戶登錄:admin和超級管理員
/**
* @param $account string the account to log in with (authorized : superadmin and admin)
*/
protected function logIn($account)
{
$this->session = $this->container->get('session');
// Cookbook try
// $this->session = new Session(new MockFileSessionStorage());
$user = $this->em->getRepository('LCHUserBundle:User')->findOneByUsername($account);
$firewall = 'admin_area';
switch($account) {
case self::SUPER_ADMIN_LOGIN:
$token = new UsernamePasswordToken($user, $account, $firewall, array('ROLE_SUPER_ADMIN'));
$this->client->setServerParameter("SERVER_NAME", SiteControllerTest::ROOT_SITE);
$this->client->setServerParameter("HTTP_HOST", SiteControllerTest::ROOT_SITE);
break;
case self::ADMIN_LOGIN:
$token = new UsernamePasswordToken($user, $account, $firewall, array('ROLE_ADMIN'));
// Session var I wish to have on my controller action tested
$this->session->set('currentSite', $this->em->getRepository('LCHMultisiteBundle:Site')->find(1));
$this->session->save();
// Use to force server canonical name for admin test
$this->client->setServerParameter("SERVER_NAME", SiteControllerTest::ROOT_SITE);
$this->client->setServerParameter("HTTP_HOST", SiteControllerTest::TEST_SITE);
break;
default:
throw new UsernameNotFoundException('Username provided doesn\'t match any authorized account');
}
// Save user in session
$this->session->set('_security_'.$firewall, serialize($token));
$this->session->set('user', $user);
$this->session->save();
// $this->container->set('session', $this->session);
$cookie = new Cookie($this->session->getName(), $this->session->getId());
$this->client->getCookieJar()->set($cookie);
我的設置()方法做到這一點:
/**
* {@inheritDoc}
*/
protected function setUp()
{
// Initiates client
$this->client = static::createClient();
$this->container = $this->client->getContainer();
$this->application = new Application($this->client->getKernel());
$this->application->setAutoExit(false);
$this->translator = $this->container->get('translator');
$this->em = $this->container
->get('doctrine')
->getManager();
}
你可以看到我設置了認證的會話變量。當我從測試操作轉儲會話變量時它們顯示正確,但是如果我添加currentSite會話變量,它似乎不會持續。當我使用客戶端提供的測試容器時,應該傳遞它不應該嗎?
PS:根據this other question,我也重寫了Client類。
我在主題上發現了很多帖子,但都沒有提供任何工作解決方案(this one,this one或that one)。 我也發現這個cookbook article。
更新:感謝Alex Blex的評論,我在這裏澄清我的問題的一些部分。
- 設置()和login()是一個自定義WebTestCase類的兩個部分,在這裏通過嵌入我的應用程序需要的特異性
- 我主要工具(如翻譯...)是設置會話在PARAMS測試控制器和檢索那些在測試動作中的會話參數
感謝您的答覆。我會澄清我的問題。我在帖子中的主要觀點是找出爲什麼測試控制器中的session var initialize在測試操作中消失的原因。我還發現了一個我將盡快發佈的解決方案。 – nbonniot
夠公平了,我已經更新了答案。仍然不相信你需要所有這些設置混亂。 –