2016-06-12 61 views
0

我有一个应用程序在Symfony 3与Web应用程序和REST API。Symfony功能测试in_memory用户getId

我写使用的Symfony \包\ FrameworkBundle \测试\功能测试WebTestCase

$this->client = static::createClient(array(), array(
     'PHP_AUTH_USER' => 'test', 
     'PHP_AUTH_PW' => 'test', 
    )); 

public function testIndex() 
{ 
    $this->client->request(
     'GET', 
     '/titles' 
    ); 
    $response = $this->client->getResponse(); 
    $this->assertEquals(200, $response->getStatusCode()); 
} 

我对API和form_login为webapp的OAuth认证。 对于我的测试中,我使用http_basic,这里是我的config_test.yml:

security: 
encoders: 
     Symfony\Component\Security\Core\User\User: plaintext 
firewalls: 
    api: 
     http_basic: ~ 
    main: 
     http_basic: ~ 
providers: 
    in_memory: 
     memory: 
      users: 
       test: { password: test, roles: [ 'ROLE_USER' ] } 

在我的控制,我得到用户是这样的:

$this->getUser()->getId() 

当我启动PHPUnit的,我得到

Error: Call to undefined method Symfony\Component\Security\Core\User\User::getId() 

它没有使用我的用户实体进行测试,我该怎么办?

回答

0

它不使用我的用户实体测试

这是因为在内存供应商uses the built in UserInterface implementation。 InMemoryUserProvider永远不会使用你的实体 - 它基于一个完全不同的系统而不是一个教条用户提供者。

你有几种解决方法。

  1. 使用数据库和原则一样。这绝对是你应该做的:端到端测试应该真正结束并有用。而且,如果你在测试中不使用数据库和原则,那么与用户实体相关的东西(通过外键)将会失败。
  2. 创建一个custom user provider,它模仿InMemoryUserProvider的行为,但使用您的自定义实体类。