2016-01-22 56 views
2

誰可以幫我在PHP功能測試認證。我做授權和我所有的控制器測試失敗,因爲他們沒有得到localhost /之後什麼也沒有,因爲需要角色ROLE_USER。Symfony2的PHPUnit的測試認證

我不知道我怎麼能它控制器的測試。我的步驟授權看起來是這樣放的輸入形式的電子郵件,如果這封電子郵件分貝存在創建用戶實體的會話和重定向提交輸入。

但是如何模擬測試呢?

例如我測試的一部分。

public function testUnlinkWatcher() 
{ 
    $client = static::createClient(); 
    $client->request('POST', 'project/client/unlink_watcher'); 


    $this->assertTrue($client->getResponse()->isSuccessful()); // this false because access denied 

} 

我試着做2個例子,但我不工作。有什麼可以做的?在登錄方法

private $client = null; 

public function setUp() 
{ 
    $this->client = static::createClient(); 
} 

private function logIn() 
{ 
    $session = $this->client->getContainer()->get('session'); 

    $firewall = 'secured_area'; 
    $token = new UsernamePasswordToken('login', null, $firewall, array('ROLE_USER')); 
    $session->set('_security_'.$firewall, serialize($token)); 
    $session->save(); // It's here object session with roles 
} 

public function testUnlinkWatcher() 
    { 

     $this->logIn();       
     $this->client->request('POST', 'testProject/client/unlink_watcher'); 

     var_dump($client->getResponse()->isSuccessful());die; //false 

我的安全

security: 

providers: 
    in_memory: 
     memory: ~ 

    hwi: 
     entity: { class: Test\testProjectBundle\Entity\User } 

firewalls: 
    dev: 
     pattern: ^/(_(profiler|wdt|error)|css|images|js)/ 
     security: false 

    secured_area: 

     anonymous: ~ 

     oauth: 
      resource_owners: 
       google:    "/login/check-google" 
       facebook:   "/login/check-facebook" 
      login_path:  /
      failure_path: /


      oauth_user_provider: 
       service: testProjectbundle.oauth_provider 

     logout: 
      path: /logout 


access_control: 
    - { path: ^/testProject(.+), roles: ROLE_USER } 

會議轉儲:

Symfony\Component\HttpFoundation\Session\Session {#377 
    #storage: Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage {#376 
    -savePath: "/var/www/integra/app/cache/test/sessions" 
    #id: "e3723c3056f23247565a4abdafd2c3cc964cc6d425e6d51dfd70bc588b4ad78b" 
    #name: "MOCKSESSID" 
    #started: false 
    #closed: false 
    #data: array:3 [ 
     "_sf2_attributes" => &1 array:1 [ 
     "_security_secured_area" => "C:74:"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":227:{a:3:{i:0;N;i:1;s:12:"secured_area";i:2;s:178:"a:4:{i:0;s:5:"login";i:1;b:1;i:2;a:1:{i:0;O:41:"Symfony\Component\Security\Core\Role\Role":1:{s:47:"\x00Symfony\Component\Security\Core\Role\Role\x00role";s:9:"ROLE_USER";}}i:3;a:0:{}}";}}" 
     ] 
     "_sf2_flashes" => &2 [] 
     "_sf2_meta" => &3 array:3 [ 
     "u" => 1453705239 
     "c" => 1453705239 
     "l" => "0" 
     ] 
    ] 

回答