2012-07-17 2 views

답변

-1

짧은 조각은이 값을

echo $session->get('name'); 
+0

작동하지 않습니다 ... CLI에서 오는 요청 변수를 수동으로 만들어야합니다. – greg

+0

테스트에 $ this-> getRequest()가 없습니다. 그리고 요청을하기 전에 $ this-> $ client-> getRequest()를 null에 넣으십시오. 그럼, 작동하지 않습니다 –

1

를 얻기 위해 세션

$session = $this->getRequest()->getSession(); 
$session->set('name', 'Sensorario'); 

의 일부 값과 아주 아주 간단한 예제를 설정합니다 비슷한 질문에 대한 답에 설명 된 것을 할 수 있습니다 : how-can-i-persist-data-with-symfony2s-session-service-during-a-functional-test

과 같이 뭔가 : 당신이 WebTestCase를 사용하는 경우

$client = static::createClient(); 
$container = $client->getContainer(); 
$session = $container->get('session'); 
$session->set('name', 'Sensorario'); 
$session->save(); 
+0

응답을 주셔서 감사합니다, 그 시도하고 그 문서를 읽었지만 여전히 얻을 :'치명적인 오류 : 부재 함수 getContainer() 비 객체에 대한 호출' – greg

+0

테스트 클래스 이것 하나를 확장 https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php? – l3l0

0

, 당신은 "세션"서비스를 검색 할 수 있습니다. 이 서비스를 , 당신은 할 수 있습니다

    가 세션을 시작
  • ,
  • 요청
에 sessionId가 함께 쿠키를 전달,
  • 이 세션을 저장, 세션에 몇 가지 매개 변수를 설정

    코드는 다음과 같습니다.

    use Symfony\Component\BrowserKit\Cookie; 
    .... 
    .... 
    public function testARequestWithSession() 
    { 
        $client = static::createClient(); 
        $session = $client->getContainer()->get('session'); 
        $session->start(); // optional because the ->set() method do the start 
        $session->set('my_session_variable', 'myvar'); // the session is started here if you do not use the ->start() method 
        $session->save(); // important if you want to persist the params 
        $client->getCookieJar()->set(new Cookie($session->getName(), $session->getId())); // important if you want that the request retrieve the session 
    
        $client->request(.... ...