doctrine2 - Symfony WebTestCase: File upload not deleted, assertion passing anyway -


thanks this cookbook recipe, have file upload form works great. doesn't test coverage, i've written own assertions, upload image, check image has been uploaded , deletes image , checks image not show on page.

they pass first time, including final assertion check image (or name of image) not exist, doesn't delete , still see on webpage. tests run after fail because crawler finds old entry. why happening?

test

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 session flash message confirms attachment uploaded.     $this->asserttrue($client->getresponse()->issuccessful());     $this->assertequals(1, $crawler->filter('html:contains("file uploaded")')->count());      // delete 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()); } 

controller

/**  * finds , displays 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 find attachment file.');     }      return $this->render('acmemybundle:attachment:show.html.twig', array(         'attachment'  => $attachment,         'delete_form' => $this->createdeleteform($id)->createview()     )); }  /**  * deletes attachment file 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 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 form delete attachment file id.  *  * @param mixed $id attachment id  *  * @return symfony\component\form\form form  */ private function createdeleteform($id) {     return $this->createformbuilder(array('id' => $id))->add('id', 'hidden')->getform(); } 

you're adding flash notice regardless of submitted form being valid.

you're checking existance of "file has been deleted." notice not added inside if-statement.

 if ($form->isvalid()) {      // ...      $this->getrequest()->getsession()->getflashbag()->add('notice', 'file has been deleted.');  } 

you should further wrap $em->remove($entity) in try-catch block because if you're removing file in pre/postremove listener still occur entity valid file can't deleted due i.e. permission problems causing exception.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -