小编Sei*_*pas的帖子

Symfony / phpUnit:修改实体的表单的功能测试

您将如何对绑定到实体的表单进行功能测试(而不是单元测试)?

语境

假设您有一个实体“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)

php phpunit symfony doctrine-orm

5
推荐指数
1
解决办法
1540
查看次数

标签 统计

doctrine-orm ×1

php ×1

phpunit ×1

symfony ×1