[英]Symfony / phpUnit : functional test of a form modifying an entity
How would you do a functional test (not unit tests) of a form binded to an entity ?您将如何对绑定到实体的表单进行功能测试(而不是单元测试)?
Let's say you have an entity "Car", with a field "id" and another field "numberPlate", and a page to edit data about a car.假设您有一个实体“Car”,有一个字段“id”和另一个字段“numberPlate”,以及一个用于编辑汽车数据的页面。
CarController.php :汽车控制器.php :
//...
public function imsiDetailsChangeAction(Request $request)
{
$car_id = $request->get('car_id');
$car = $this->getDoctrine()->getRepository('ClnGsmBundle:Car')->Find($car_id);
if ($simCard != null)
{
$form = $this->createForm(new CarType()), $car);
if($request->isMethod('POST'))
{
$form->bind($request);
if ($form->isValid())
{
$em = $this->getDoctrine()->getManager();
$em->flush();
return $this->redirect($this->generateUrl('car_view', array('car_id' => $car->getId())));
}
}
}
else
{
throw new NotFoundHttpException();
}
return $this->render('SiteBundle:Car:carEdit.html.twig', array('car' => $car, 'form' => $form->createView()));
}
//...
A test using phpUnit doing the following :使用 phpUnit 执行以下操作的测试:
create a Car entity with the numberPlate "QWE-456"创建一个编号为“QWE-456”的 Car 实体
load the page with the form用表单加载页面
using the crawler, replace the numberPlate with "AZE-123" in the form, and submit the form使用爬虫,将表单中的numberPlate替换为“AZE-123”,提交表单
assert that my car entity's numberPlate now equals "AZE-123"断言我的汽车实体的 numberPlate 现在等于“AZE-123”
(just in case: my own code is a bit different, here is what I would do with the car example) (以防万一:我自己的代码有点不同,这是我对汽车示例所做的)
CarControllerTest.php : CarControllerTest.php :
//...
public function SetUp()
{
//start kernel, stores entity manager in $this->em and client in $this->client
}
//...
public function testEditForm()
{
$car = new Car();
$car->setNumberPlate("QWE-456");
$this->entityManager->persist($simCard);
$this->em->flush();
$crawler = $this->client->request('GET', '/fr/Car/edit/'.$car->getId());
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
$formNode = $crawler->filterXpath("//div[@id='main']//form");
$form = $formNode->form(array(
'car[plateNumber]'=>'AZE-123',
));
//var_dump($car->getPlateNumber());
$this->client->submit($form);
//var_dump($car->getPlateNumber());
$this->assertEquals('AZE-123',$car->getPlateNumber);
}
I expect this test to pass, and the second var_dump to print "AZE-123" instead of "QWE-456".我希望这个测试通过,第二个 var_dump 打印“AZE-123”而不是“QWE-456”。 But my entity isn't modified.
但我的实体没有被修改。
How should I do this ?我该怎么做?
You should refresh the data reloading it from the database: the refresh method do it for you, so try this:您应该刷新数据,从数据库重新加载它: refresh 方法为您完成,所以试试这个:
$this->client->submit($form);
$this->em->refresh($car);
$this->assertEquals('AZE-123',$car->getPlateNumber);
I suggest you to check before the HTTP Response in order to verify the correct interaction, as example:我建议您在 HTTP Response 之前检查以验证正确的交互,例如:
$response = $this->client->getResponse();
$this->assertTrue($response->isRedirection());
Hope this help希望这有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.