Symfony WebTestCase:文件上载未删除,断言仍在传递
多亏了,我有一个文件上传表单,效果很好。但是它没有测试覆盖率,所以我写了自己的断言,上传一个图像,检查图像是否已上传,然后删除图像,检查图像是否未显示在页面上 它们都是第一次通过的,包括检查图像(或图像名称)是否不存在的最终断言,但它不会删除它,而且我仍然可以在网页上看到它。因此,在此之后运行的任何测试都将失败,因为爬虫程序会找到旧条目。为什么会这样 测试Symfony WebTestCase:文件上载未删除,断言仍在传递,symfony,doctrine-orm,phpunit,symfony-2.3,Symfony,Doctrine Orm,Phpunit,Symfony 2.3,多亏了,我有一个文件上传表单,效果很好。但是它没有测试覆盖率,所以我写了自己的断言,上传一个图像,检查图像是否已上传,然后删除图像,检查图像是否未显示在页面上 它们都是第一次通过的,包括检查图像(或图像名称)是否不存在的最终断言,但它不会删除它,而且我仍然可以在网页上看到它。因此,在此之后运行的任何测试都将失败,因为爬虫程序会找到旧条目。为什么会这样 测试 public function testUploadScenario() { // Upload image $crawle
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();
}
无论提交的表单是否有效,您都将添加flash通知 您正在检查是否存在“文件已被删除”。但通知不会添加到if语句中
if ($form->isValid()) {
// ...
$this->getRequest()->getSession()->getFlashBag()->add('notice', 'File has been deleted.');
}
您应该在try-catch块中进一步包装
$em->remove($entity)
,因为如果您在pre/postRemove侦听器中删除该文件,该实体仍然可能有效,但由于权限问题导致异常,无法删除该文件。干杯。不知道我怎么会错过那个。。。现在看起来很明显;)我肯定也会使用try-catch挡块。