2013-07-31 73 views
2

感謝this cookbook recipe,我有一個文件上傳表單,效果很好。但是它沒有測試覆蓋率,所以我寫了自己的斷言,上傳圖片,檢查圖片是否已經上傳,然後刪除圖片並檢查圖片沒有顯示在頁面上。Symfony WebTestCase:文件上傳沒有刪除,斷言仍然通過

他們都第一次通過,包括最後斷言,檢查圖像(或圖像的名稱)不存在,但它不會刪除它,我仍然可以在網頁上看到它。因此,之後運行的任何測試都將失敗,因爲Crawler會找到舊條目。這是爲什麼發生?

測試

public function testUploadScenario() 
{ 
    // Upload image 
    $crawler = $client->click($crawler->selectLink('Upload')->link()); 
    $this->assertEquals(1, $crawler->filter('html:contains("Upload file attachment")')->count()); 

    $pathToTestUploadFile = static::$kernel->getRootDir().'/../src/Acme/MyBundle/Resources/public/logo.gif'; 

    $uploadForm = $crawler->selectButton('Upload')->form(); 

    $uploadForm['upload[name]'] = "Logo Test"; 
    $uploadForm['upload[file]']->upload($pathToTestUploadFile); 

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

    // Check that the session flash message confirms the attachment was uploaded. 
    $this->assertTrue($client->getResponse()->isSuccessful()); 
    $this->assertEquals(1, $crawler->filter('html:contains("File uploaded")')->count()); 

    // Delete the image 
    $crawler = $client->submit($crawler->selectButton('Delete')->form()); 

    $this->assertEquals(0, $crawler->filter('html:contains("Logo Test")')->count()); 
    $this->assertEquals(1, $crawler->filter('html:contains("File has been deleted")')->count()); 
} 

控制器

/** 
* Finds and displays an attachment. 
* 
* @param integer $id 
*/ 
public function showAction($id) 
{ 
    $em = $this->getDoctrine()->getManager(); 

    $attachment = $em->getRepository('AcmeMyBundle:Attachment')->find($id); 

    if (!$attachment) { 
     throw $this->createNotFoundException('Unable to find attachment file.'); 
    } 

    return $this->render('AcmeMyBundle:Attachment:show.html.twig', array(
     'attachment' => $attachment, 
     'delete_form' => $this->createDeleteForm($id)->createView() 
    )); 
} 

/** 
* Deletes an attachment file from the database. 
* 
* @param Request $request 
* @param integer $id 
* 
* @return Response 
*/ 
public function deleteAction(Request $request, $id) 
{ 
    $form = $this->createDeleteForm($id); 
    $form->bind($request); 

    if ($form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $attachment = $em->getRepository('AcmeMyBundle:Attachment')->find($id); 

     if (!$attachment) { 
      throw $this->createNotFoundException('Unable to find attachment file.'); 
     } 

     $em->remove($attachment); 
     $em->flush(); 
    } 

    $this->getRequest()->getSession()->getFlashBag()->add('notice', 'File has been deleted.'); 

    return $this->redirect($this->generateUrl('attachment_index')); 
} 

/** 
* Creates a form to delete an attachment file by id. 
* 
* @param mixed $id The attachment id 
* 
* @return Symfony\Component\Form\Form The form 
*/ 
private function createDeleteForm($id) 
{ 
    return $this->createFormBuilder(array('id' => $id))->add('id', 'hidden')->getForm(); 
} 

回答

2

您要添加的閃光燈通知不管提交的表單中是有效的。

您正在檢查「文件已被刪除」的存在。但通知不會被添加到if語句中。

if ($form->isValid()) { 
    // ... 
    $this->getRequest()->getSession()->getFlashBag()->add('notice', 'File has been deleted.'); 
} 

你應該在一個try-catch塊包裝進一步$em->remove($entity),因爲如果你在一個前/刪除後監聽刪除文件仍可能發生的實體是有效的,但該文件無法刪除的原因即導致異常的權限問題。

+0

乾杯。不知道我是如何錯過那個......現在看起來真的很明顯;)我肯定會使用try-catch塊。 –