您将如何对绑定到实体的表单进行功能测试(而不是单元测试)?
假设您有一个实体“Car”,其中包含一个字段“id”和另一个字段“numberPlate”,以及一个用于编辑汽车数据的页面。
CarController.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()));
}
//...
Run Code Online (Sandbox Code Playgroud)
使用 phpUnit 进行的测试执行以下操作:
创建一个车牌号为“QWE-456”的汽车实体
使用表单加载页面
使用爬虫,将表单中的numberPlate替换为“AZE-123”,然后提交表单
断言我的汽车实体的车牌现在等于“AZE-123”
(以防万一:我自己的代码有点不同,这是我对汽车示例所做的操作)
CarControllerTest.php:
//...
public function SetUp()
{
//start kernel, …Run Code Online (Sandbox Code Playgroud)