2017-08-17 85 views
0

我正在使用Symfony 3.2與FOSUserBundle,並且我正在嘗試爲需要特定角色身份驗證的函數編寫功能測試。FOSUserBundle - PHPUnit - 模擬用戶 - 沒有用戶提供程序

我以前張貼的方法,通過@tftd here,但是當我運行一個PHPUnit的測試中,我得到一個500錯誤:There is no user provider for user "Symfony\Component\Security\Core\User\User".

我webTestCase類看起來是這樣的:

abstract class CustomWebTestCase extends WebTestCase 
{ 

    /** 
    * @param array|null $roles 
    * @return \Symfony\Bundle\FrameworkBundle\Client 
    * 
    * https://stackoverflow.com/questions/35565196/fosuserbundle-phpunit-mock-a-user 
    */ 
    protected static function createAuthenticatedClient(array $roles = null) { 
     // Assign default user roles if no roles have been passed. 
     if($roles == null) { 
      $role = new Role('ROLE_SUPER_ADMIN'); 
      $roles = array($role); 
     } else { 
      $tmpRoles = array(); 
      foreach($roles as $role) 
      { 
       $role = new Role($role); 
       $tmpRoles[] = $role; 
      } 
      $roles = $tmpRoles; 
     } 

     $user = new User('test_super_admin', 'passwd', $roles); 

     return self::createAuthentication(static::createClient(), $user); 
    } 

    private static function createAuthentication(Client $client, User $user) { 
     // Read below regarding config_test.yml! 
     $session = $client->getContainer()->get('session'); 

     // Authenticate 
     $firewall = 'main'; // This MUST MATCH the name in your security.firewalls.->user_area<- 
     $token = new UsernamePasswordToken($user, null, $firewall, $user->getRoles()); 
     $session->set('_security_'.$firewall, serialize($token)); 
     $session->save(); 

     // Save authentication 
     $cookie = new Cookie($session->getName(), $session->getId()); 
     $client->getCookieJar()->set($cookie); 

     return $client; 
    } 

而我的測試程序如下所示:

class TryoutAdminControllerTest extends CustomWebTestCase 
{ 
    public function testTryoutListAction() 
    { 
     $authenticatedClient = self::createAuthenticatedClient(array('ROLE_USER')); 
     $crawler = $authenticatedClient->request('GET', '/admin/tryout'); 
     $this->assertEquals(302, $authenticatedClient->getResponse()->getStatusCode(), 'No access allowed!'); 

     $authorizedClient = self::createAuthenticatedClient(array('ROLE_ADMIN')); 
     $crawler = $authorizedClient->request('GET', '/admin/tryout'); 
     $this->assertEquals(200, $authorizedClient->getResponse()->getStatusCode(), 'Access granted!'); 
    } 
} 

security.yml:

security: 
    encoders: 
     FOS\UserBundle\Model\UserInterface: bcrypt 

    role_hierarchy: 
     ROLE_ADMIN:  ROLE_USER 
     ROLE_SUPER_ADMIN: ROLE_ADMIN 

    providers: 
     fos_userbundle: 
      id: fos_user.user_provider.username 

    firewalls: 
     main: 
      pattern: ^/ 
      form_login: 
       provider: fos_userbundle 
       csrf_token_generator: security.csrf.token_manager 

      logout:  true 
      anonymous: true 
      switch_user: true 
      remember_me: 
       secret: '%secret%' 
       lifetime: 604800 # 1 week in seconds 
       path: /
       domain: ~ 
       user_provider: fos_userbundle 

最後config_test.yml

imports: 
    - { resource: config_dev.yml } 

framework: 
    test: ~ 
    session: 
     storage_id: session.storage.mock_file 
    profiler: 
     collect: false 

web_profiler: 
    toolbar: false 
    intercept_redirects: false 

swiftmailer: 
    disable_delivery: true 

如果有人可以讓我知道我錯過了什麼,我會感激!

+0

你不應該在功能測試中嘲笑。嘲笑適合單元測試。只需使用一些裝置,即可加載所需的用戶。然後你可以通過一個簡單的技巧來驗證用戶,就像這個一樣https://github.com/Bee-Lab/BeelabTestBundle/blob/master/Test/WebTestCase.php#L136 –

回答

0

你可能需要實例,從FOS\UserBundle\Model\User而不是Symfony\Component\Security\Core\User\UserWebTestCase延伸,因爲你使用的是FosUserBundle用戶提供自己的用戶類。

考慮使用測試數據庫和數據夾具,因爲您可能需要它來進行功能測試。 LiipFunctionalTestBundle可能會有所幫助。

請記住,在這個測試中沒有任何東西被模擬,正如問題標題中所建議的。嘲笑必須用於單元測試,而不是功能測試。

+0

這就是當然。我有錯誤的使用說明。謝謝! –

相關問題