Thanks to this cookbook recipe, I have a file upload form that works great. However it doesn't do test coverage, so I've written my own assertions, which upload an image, check that the image has been uploaded and then deletes the image and checks that the image does not show on the page.
They all pass the first time, including the final assertion to check that the image (or the name of the image) does not exist, but it doesn't delete it and I still see it on the webpage. So any tests that run after that will fail because the Crawler finds the old entry. Why is this 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 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());
}
Controller
/**
* 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();
}