2013-10-07 1 views
1

데이터베이스에서 내 사용자를 업데이트 할 수있는 updateAction을 테스트하려고하는데 어떻게 테스트 할 수 있는지 잘 모르겠습니다 ... 내 데이터베이스에 사용자를 추가하는 createAction을 테스트 해 보았습니다. 그래서 나는 이것을 방치하기 위해 방금 만든 사용자 아이디를 얻고 싶습니다.업데이트 작업을 테스트하는 방법은 무엇입니까?

이 제 기능 testCreate입니다 :

public function testCreate() 
    { 
     $crawler = $this->client->request('GET', '/admin/user/new'); 

     $buttonCrawlerNode = $crawler->selectButton('submit'); 

     $form = $buttonCrawlerNode->form(array(
      'myapp_usertype[email]'   => '[email protected]', 
      'myapp_usertype[role]'    => 'ROLE_USER', 
      'myapp_usertype[password][first]' => 'test', 
      'myapp_usertype[password][second]' => 'test', 
     )); 

     $this->client->submit($form); 
    } 
+0

. 이것은 데이터베이스와의 실제 상호 작용이 아닌 코드를 검사합니다. 다음 단계는 생성하기 전에 사용자 목록을 가져 와서 (모두 선택하고 쿼리를 선택하고) 작성한 다음 새 목록을 가져 오는 것입니다. 목록의 새로운 항목 수는 이전보다 1 많아야하며 지정한 새 이메일 주소, 이메일, 역할 및 암호가 있어야합니다. –

답변

1

그리 건조,하지만 당신은 시도 할 수 있습니다 : 당신은 하드 코딩 결과, 기능 작동을 확인하기 위해 처음 모의 객체를 사용할 수 있습니다

public function testUpdate() 
{ 
    $user = static::$kernel->getContainer()->get('doctrine')->getRepository('YourUserBundle:User')->findOneByEmail('[email protected]'); 

    $crawler = $client->request('GET', '/admin/user/edit/'.$user->getId()); 
    $form = $crawler->selectButton('submit')->form(); 

    $form = $buttonCrawlerNode->form(array(
     'myapp_usertype[email]'   => '[email protected]', 
     'myapp_usertype[role]'    => 'ROLE_ANOTHER', 
     'myapp_usertype[password][first]' => 'test', 
     'myapp_usertype[password][second]' => 'test', 
     )); 

     $crawler = $client->submit($form); 

    $user = static::$kernel->getContainer()->get('doctrine')->getRepository('YourUserBundle:User')->find($user->getId()); 

    $this->assertEquals(
     '[email protected]', 
     $user->getEmail() 
    ); 

    $this->assertEquals(
     'ROLE_ANOTHER', 
     $user->getRole() 
    ); 
}