2013-10-07 78 views
1

我想測試我的updateAction,它允許更新我的用戶在數據庫中,但我不知道如何測試它...我已經成功測試了我的createAction,它將用戶添加到我的數據庫中。所以我想獲取我剛剛創建的用戶ID來更新它。如何測試更新操作?

這是我的功能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,並且新ID應該存在,並指定您指定的電子郵件地址,角色和密碼。 –

回答

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() 
    ); 
}